找回密码
 注册账号
img_loading
智能检测中
置顶:如何加入2024届新生微信群

[吹水聊天] 上岸算法LeetCode Weekly Contest 285解题报告

上岸算法 回复:0 | 查看:1471 | 发表于 2022-3-24 05:53:38 |阅读模式 |复制链接

UWCSSA提醒您:

警惕网络诈骗与盗号,不要在他人发送的网站中输入密码,换汇或付款时请小心诈骗。

为了避免个人信息泄漏,建议在帖子中使用不常用的邮箱,或使用私信发送联系方式(点击对方的头像,然后“发送消息”)。

帖子通过审核只代表内容不违规,CSSA 不会验证内容的真实性。请谨防诈骗。

登录后可回复主题

您需要 登录 才可以下载或查看,没有帐号?注册账号

x
【 NO.1 统计数组中峰和谷的数量】
- @8 d$ G/ r' W; r1 X5 j! x% f$ B( X% i& Z
解题思路6 h. A, ]# U1 m& q  w& a
先去重,再统计。
1 i5 A- q0 d3 @
$ E: E3 y5 ?, D8 z- i代码展示. U  f) n: k( y: G7 e- |4 I% ^
* I7 X7 Y0 w; i& k& s/ F
class Solution {' l: ]! ]0 N1 R9 e1 z% ~
   public int countHillValley(int[] nums) {
, J5 Q# y9 u+ x  j  ?% i' _       int len = 1;) U/ G4 G" r2 o! [! N
       for (int i = 1; i < nums.length; i++) {- c/ k* C3 n; K! p) Y6 _
           if (nums[i] != nums[len - 1]) {
5 h# s) h, C) B; V. j+ O9 h               nums[len] = nums[i];' X! X& u/ b2 ^, Q
               len++;
4 T' z/ `$ W3 I          }
/ `% j, Z3 A  R( U8 e$ ?      }  J" {: ^# m: Y4 j4 s' g# a! J9 p# z
       int res = 0;
+ q7 f; h5 Y6 Q3 _2 H9 A       for (int i = 1; i < len - 1; i++) {# |" n5 [$ ]" F- O; [1 s7 l2 P2 Y( k
           if (nums[i - 1] < nums[i] == nums[i + 1] < nums[i]) {
" e' p  h* `5 t9 H, O               res++;
2 A1 i" T$ u( ~, i          }& w+ I- ~7 z, F# m" q
      }$ G) e+ l0 k. m2 T  m" S# g
       return res;
2 G$ |: ?& c( U+ w, K  }8 i% O& ?" g+ M8 i, E
}
+ B' y6 w  K& l7 H1 c/ k- |- P8 y* u, G
( D4 `0 v: v. X, N
' X; |) m/ H& g2 Z& T9 u8 t+ C: q【 NO.2 统计道路上的碰撞次数】
" w5 L6 n7 E8 j( F' [
# ]5 \5 C4 l; }  @解题思路
' b4 b; M& \, i- c- E) ?# [从左到右遍历,维护左侧的车的状态,有两种:
) x  Y- S0 _/ i! |2 }: R6 F! Q& w# `) v. C: j
若干个向右行驶的
0 T" T, \6 A! _/ `, c
! h: d8 t& V8 o: _停止
: z* S8 d; g$ b8 v" e! i  c( \. n1 l9 d3 j3 [6 I6 t
代码展示
, D# }* Y+ K$ P4 e/ U1 ]9 O! I3 G8 k1 f/ X
class Solution {
' b2 N) J0 Z+ m# G# |# k   public int countCollisions(String directions) {5 d3 p  G3 `7 D5 a8 {+ x
       int res = 0;8 }* y* z. g8 s
       int S = 0, R = 0;* w. j) f6 x) D6 M: t1 b5 {9 d; I
       for (char c : directions.toCharArray()) {
: J+ p% G0 X4 A  o           if (c == 'L') {% s. }5 ]0 W6 @" i
               if (S + R == 0) {& b: i( w2 a( P" y; U
                   continue;
; D( u# z# W, O0 U7 t8 z9 u              }' z; g: ?8 O# d( r! o' ?
               if (R > 0) {0 C, B3 l4 c% c; ^0 K
                   res += R + 1;
3 x0 L6 w. F1 t& U              } else {- @+ _' L- |8 _6 A& I; K2 a
                   res += S;
) L& B0 y0 a! [7 s              }
/ l* Q' K3 v- d* N" M1 m/ J0 m               S = 1;
- z. c4 a$ g* p  [               R = 0;
& |; d. T2 C. h3 F' N# H& H: x          }
/ z  Q9 x$ R  {$ l5 s6 o' v9 U+ K7 w           if (c == 'R') {8 [& c7 e8 _" o7 k' [' l8 {; C
               S = 0;" z9 Q% x9 J8 p8 }+ E
               R++;
, C  t& J6 D& x) K5 t* Z, q          }
( \( x+ i8 Z2 S1 s3 Q4 o           if (c == 'S') {& d( ^' _& N( P. X/ o6 `
               res += R;
& j9 F# }: a+ M4 E               S = 1;  a' S0 \+ N4 ]' p
               R = 0;2 X4 d8 {, w- E( R' ?" @2 Q
          }
; z# V( a3 m3 X$ t# L' v% E& N3 o, l      }5 i5 Y( u5 P+ [0 R
       return res;8 H+ [6 s* t% u+ _! T
  }
2 t* m6 D4 ]: K, Z4 X# ~$ y}
+ W- F7 ^( _5 c; ?4 G7 u
8 i2 W% T. H5 \5 k( k
4 |, H. {( j/ I% q$ S【 NO.3 射箭比赛中的最大得分】5 M% T3 T: n$ ~: D9 o5 U% V  N( p

' @2 b" q6 v* F0 Q) ?$ s4 Q解题思路: W5 K+ w! K# |
枚举所有的情况即可,实际一共有 11 个得分位置,枚举 Bob 要在哪些位置得分。) J! D0 A9 j2 Q1 v1 q( L' i
) S2 k9 I. N$ U7 e$ f; ?
贪心地,当 Bob 要在位置 i 得分时,只需要花费 aliceArrows[i] + 1 支箭。
$ Q* t, W* \  B7 n9 ?5 u
( n0 b, [4 ~5 S- j' H最后多余的箭可以放到位置 0.
5 l; q1 R+ M8 n' F7 \* Y1 [% h/ N- R% k) A9 X% p
代码展示7 o- ?8 U/ }9 ?4 c0 u: ~1 g8 ^
: a  C: B, m5 e5 V8 A& l
class Solution {- d; h, s4 v) z) @
   public int[] maximumBobPoints(int numArrows, int[] aliceArrows) {/ J: @  H$ z  g$ z0 Y, Y6 @
       int max = 0;
& H- e7 ?7 ]7 D6 ^% Z       int[] res = new int[12];
7 _  ?3 I' \( N2 |) G4 {4 G       for (int i = 0; i < (1 << 11); i++) {( s. V2 T# y8 k) @# `
           int num = 0;1 m/ T, G* ~9 ^: v
           int score = 0;
8 o$ f0 h" f# \  E/ _! A, U           for (int j = 0; j < 11; j++) {' d4 N. K3 B, z4 }: J& C
               if ((i & (1 << j)) > 0) {
* N% n0 \4 x" x% x/ P                   num += aliceArrows[j + 1] + 1;: C9 r% Y+ }; i+ _
                   score += j + 1;/ _9 g2 A1 U' A" K/ A
              }
# _# s/ X& P1 Y  L          }
8 M# @; ]% ~0 c2 p# R' B3 [           if (num > numArrows) {
. E3 b( c7 X% d  i9 ^: a& s% l9 R               continue;/ X/ E; B6 Y) S1 [! a: R
          }
; M. C: e* o2 }( x. ^, G+ q$ J. h8 Z           if (max < score) {
0 @) M% N+ G0 D" y: K3 m. p4 I               max = score;
) N5 K% I% I2 T2 {) d- Y/ Y: H               res[0] = numArrows - num; // 多余的箭
8 i+ W9 F0 X+ h               for (int j = 0; j < 11; j++) {4 ^/ d! h# B3 F: I7 t0 X
                   if ((i & (1 << j)) > 0) {
/ h' r' _3 H/ [2 {                       res[j + 1] = aliceArrows[j + 1] + 1;+ M" h' s* P/ _3 J' S# q( Y7 t
                  } else {6 ~, N$ D2 R) ?
                       res[j + 1] = 0;
0 R' ^# E8 y* Z' V. U6 o                  }8 N$ K8 ~) e' p; M$ t
              }
  A/ N: d$ F9 X+ m          }/ ?( L& `; `  {$ H4 m" K
      }! n! T$ `6 H+ k) k
       return res;
9 K- [. l% F3 U5 _. i  }
+ j" ~3 o) J4 T5 H+ R) B$ z}
' `$ g. G5 I1 }7 q4 r( u* n8 }& ~; j( Z8 {" j* T

4 b! K* s9 y. a& R1 }8 `! Q【 NO.4 由单个字符重复的最长子字符串】0 D8 Z" t' \- x. n! \
. r3 V- e  ~  v# M, S) I" c$ i
解题思路+ i7 s& Y: _8 n; f
灵活运用 TreeSet 即可,详见注释。
; v1 K  E( X# ]' W  u  S: J+ i. R4 K' P2 _& q0 u, ]
代码展示* h7 n" W7 i& ?, i  Z, b, v4 J; V

. s+ C' q& s8 t  g3 ~9 Xclass Solution {
) ~( c0 _5 s; t- C  f4 ~# Q! j- A5 {0 s( z! G/ H, ^6 b0 O
   // 一个 Interval 表示一段连续的字符
' ^' T% @6 t0 t( c2 D9 t+ R+ C   static class Interval implements Comparable<Interval> {* R9 e0 ]9 d+ o' v  M6 X
       int start;& \2 U- r$ [- `/ N& D
       int len;" {* W* {1 T" n1 ^4 i, s
       char c;; L0 s# y! N) ~3 j4 F8 ~7 a+ R
; g; e% t9 G6 x( c
       public Interval(int start, int len, char c) {
( l0 [* h- c2 C- a  C% ]: d           this.start = start;
. Z- J) d. m7 b7 Z+ d# Z           this.len = len;8 D% c3 r2 K: r' t- s
           this.c = c;5 e1 P+ F. }& Z) o. M" Y! `
      }
+ f9 `5 X. e8 U% E9 j' ~. C2 Z/ ^  E. l
       @Override
' i9 k# s. w. S! P2 P. O       public int compareTo(Interval o) {, [- {/ E& r* ?
           if (len != o.len) {5 q7 W  P' c* c& z0 L2 a
               return len - o.len;
5 p5 w: k6 `" S! c! U. I          }
8 [/ W6 Z+ e. o% v8 e7 n           if (start != o.start) {; N0 ~/ x0 V& b" [" i0 y* f; Z! ?8 Q
               return start - o.start;/ t  z9 p# \# U9 M* Y3 E7 O0 |
          }
6 ]9 z0 |1 o# T9 |  @           return c - o.c;; B% a/ w# u$ L$ |
      }
9 X* q0 u" h4 d# U: X! D' k  }
# P( V% e8 ?3 z9 N; g% i; s" }
5 F" q9 R/ x3 V5 B, G; F# a$ m4 j, q   public int[] longestRepeating(String s, String queryCharacters, int[] queryIndices) {' b' g7 R8 ^# Z, h6 |
       // 首尾新增两个其他字符,这样相当于 queryIndices 不会修改字符串首尾,便于后续的处理9 Q- x3 w. z% k/ p  _
       s = "_" + s + "_";
+ o5 _9 G% L/ ^- w$ S# v       // allIntervals 按照 len 排序全部的区间
3 e0 G( ~+ R, N0 d: h       TreeSet<Interval> allIntervals = new TreeSet<>();; O$ I& D1 {. S( L6 [1 k
       // intervals 按照字符聚合区间,即 intervals.get('a') 表示字符 a 的全部区间,并且按照起始位置排序
& O' O5 O/ _0 f9 |2 T9 j* k       Map<Character, TreeSet<Interval>> intervals = new HashMap<>();
! z, i! y9 v" r  \- b& q( w; F       for (char c = 'a'; c <= 'z'; c++) {+ M8 L, P( g; Q2 T& F, V$ m
           intervals.put(c, new TreeSet<>(Comparator.comparingInt(o -> o.start)));% G$ q# j* m, O( W( D+ N  L
      }+ v" P' T8 H9 k% G8 n5 N
       intervals.put('_', new TreeSet<>());
! S1 A4 z3 e% n0 \* O: M, _, V- k; `+ V8 G! \; a5 q- t9 z
       // 遍历 s 维护 intervals 和 allIntervals2 z! ?, W& M2 k! U/ }
       int last = 0;) v; f6 e" e+ b3 {* R) J0 x3 t
       for (int i = 1; i < s.length(); i++) {
$ b3 D! \& [7 |0 t) z           if (s.charAt(i) == s.charAt(last)) {- E, r  n) e9 c
               continue;
& |1 u+ d2 T: ^1 D& \# y7 f0 O          }3 V5 z7 Y$ L# R
           Interval interval = new Interval(last, i - last, s.charAt(last));
3 g, |& Q5 b5 B) y- p           allIntervals.add(interval);
, G& J9 }# W! X4 D8 Z8 W           intervals.get(s.charAt(last)).add(interval);6 V0 X4 T. M0 }1 w$ T
           last = i;
0 T7 z1 |. Z; z      }
4 ^' A2 C' E$ y4 p% H       Interval interval = new Interval(last, s.length() - last, s.charAt(last));
+ x$ w  ^6 K5 o: D! O& r: L       allIntervals.add(interval);" @% Q% u0 y- [. A
       intervals.get(s.charAt(last)).add(interval);
) o  L5 J: e% R; h( L7 h
5 U( M1 R# |& h8 O, I       // 每次 query 调用 maintain 维护 intervals 和 allIntervals
! D" J  D( }/ W% a3 ^) u( b       int[] res = new int[queryIndices.length];  e& F& \1 s1 [& \3 T
       char[] arr = s.toCharArray();
& R! T; [! o+ j       for (int i = 0; i < queryIndices.length; i++) {
4 Q' j; b3 r; M0 R           res[i] = maintain(arr, allIntervals, intervals, queryIndices[i] + 1, queryCharacters.charAt(i));
$ e) @& M1 C3 f6 g+ B" B" Q9 W      }
) I* t1 x9 C& i0 B% \$ y! ~% C9 L       return res;( q" s; j( P  G3 d
  }6 n% u  o, b# V* t
, a+ ?# O  J' @8 X- s5 o7 y
   // 将 arr[idx] 替换为 c: T3 g3 r6 s4 d: P; G1 k) I, u& ~( c$ f
   // 维护 allIntervals 和 intervals
6 `( `5 B, a1 P# X* W! _   // 返回单个字符的最长的子字符串长度
! X5 G  {2 }. H( v   private int maintain(char[] arr, TreeSet<Interval> allIntervals, Map<Character, TreeSet<Interval>> intervals, int idx, char c) {$ z" a' d  S& i3 Q+ y
       if (arr[idx] == c) {$ F  j3 _4 Q* Y6 f% l) X
           return allIntervals.last().len;$ d" o- x' C/ t& W  p) M+ C
      }% @: k$ C: z) x/ }1 }0 [: L

% x/ Y0 P8 S5 I1 l, u, U) F       // 维护原字符 arr[idx] 的 interval
- m7 s9 u2 a; y" w7 Y       var treeSet = intervals.get(arr[idx]);
5 T7 j8 o  Z% z1 c  f  K2 K       Interval origin = treeSet.floor(new Interval(idx, 0, arr[idx])); // 调用 treeSet.floor/ceiling 时,只需要 start 即可# I  n# x! D! L& G+ X  l) x
       treeSet.remove(origin);- y4 W: U% ~  W2 x! K+ Y* [9 u4 V
       allIntervals.remove(origin);* |8 b' V) o( H0 C$ l, W
       if (origin.start < idx) {- |  q3 z  O/ N0 V& p- S8 _: O
           Interval interval = new Interval(origin.start, idx - origin.start, arr[idx]);
. d; }& s2 A5 z6 m# {           treeSet.add(interval);; v4 X) {9 U: d& O, P' w
           allIntervals.add(interval);4 ^' S& A) e8 Z% P8 h" }
      }
! {9 q1 ~( q9 p; N$ e       if (idx + 1 < origin.start + origin.len) {2 I  ]6 P) X6 H  l% ^, y# `
           Interval interval = new Interval(idx + 1, origin.start + origin.len - idx - 1, arr[idx]);; j4 e; `! l0 k+ }
           treeSet.add(interval);
4 p6 Q  R; a6 m; C7 G* i- o  O! [; i           allIntervals.add(interval);, k, u7 c% U/ f9 L
      }1 ~5 S& R: C9 t1 a6 a/ S0 W

6 t  r, Z4 h/ x4 K4 T8 |0 K       // 维护新字符 c 的 interval
8 R2 T9 b, h% o8 H& r       treeSet = intervals.get(c);
5 s  U* `' I, i       if (arr[idx - 1] == c && arr[idx + 1] == c) { // 左右连接
5 K$ i  p, `4 I; y, J           Interval left = treeSet.floor(new Interval(idx - 1, 0, c));/ ]1 |+ Q! D0 Q
           Interval right = treeSet.ceiling(new Interval(idx + 1, 0, c));: T( z1 f9 @" A3 h6 p+ i, _/ \4 e
           treeSet.remove(left);
; `* F3 e8 ]7 [1 [1 U           treeSet.remove(right);. ~4 l7 C* @" V. c/ r" W, i
           allIntervals.remove(left);( O# Y& W" Q* Q% J' f
           allIntervals.remove(right);; z( `4 o4 I! J" p- u+ Z
           Interval interval = new Interval(left.start, left.len + right.len + 1, c);" |$ O7 M$ k8 `, t, v
           treeSet.add(interval);& [( L5 _2 H: f7 e2 Y
           allIntervals.add(interval);
* }6 ~0 {4 j. C6 G      } else if (arr[idx - 1] == c && arr[idx + 1] != c) { // 左连接
- b2 ^( q; O! p+ R/ d           Interval left = treeSet.floor(new Interval(idx - 1, 0, c));
) M# C- p& F$ g& y7 ^* D4 ^           treeSet.remove(left);
! Z! y+ `2 d( ]! }! j$ W  O           allIntervals.remove(left);4 ^4 h4 q9 |! V' V  S: n& [# G
           Interval interval = new Interval(left.start, left.len + 1, c);% b! r6 U" Y4 {/ W3 Y
           treeSet.add(interval);2 i1 U. Y: `" \5 ~2 e" w' J. E+ L
           allIntervals.add(interval);
2 r! j. o" `3 m3 |+ g      } else if (arr[idx - 1] != c && arr[idx + 1] == c) { // 右连接
( W: U7 ~% e3 Z; P1 E           Interval right = treeSet.ceiling(new Interval(idx + 1, 0, c));
% x& [6 m  L4 B& X) K           treeSet.remove(right);
' o0 V  p7 H9 F: P           allIntervals.remove(right);
8 }, B. E6 q: y! u           Interval interval = new Interval(idx, right.len + 1, c);" {+ t# G, y3 O2 ]8 j+ L
           treeSet.add(interval);- A3 P9 x3 F/ L. `) O
           allIntervals.add(interval);
& P& W5 h9 I& C      } else { // 单独一个
  g- }0 C, J/ ~           Interval interval = new Interval(idx, 1, c);" h! o) l" @/ ~7 M9 k/ j5 X4 ?
           treeSet.add(interval);
5 C" K5 X$ Y2 A& k0 M           allIntervals.add(interval);
& e; j& M$ M6 u! R2 }* o      }/ u( ?+ p& \" T) k$ c1 G. T

" k6 Z' m4 P/ U7 o5 S3 |       arr[idx] = c;+ N4 ]% |2 Q0 [2 j9 Y
       return allIntervals.last().len;' G. g9 M+ }8 M2 h9 [+ K
  }& J: V9 c0 w5 h1 d
}
您需要登录后才可以回帖 登录 | 注册账号

本版积分规则

登录 发布 快速回复 返回顶部 返回列表