登录后可回复主题
您需要 登录 才可以下载或查看,没有帐号?注册账号
x
【 NO.1 统计数组中峰和谷的数量】1 ]/ X1 Z& O& d6 g6 x
, o( y8 Z$ r, y6 r解题思路$ N: ? {( r$ v
先去重,再统计。7 N% v$ k5 D4 W7 F0 q& f
( c8 R: g* g# h* H, x6 u, h; m1 B
代码展示$ a+ j6 {+ @; h9 D/ M6 K9 e$ F/ {
$ c$ l: B/ u0 s2 u2 f2 U
class Solution {. f7 Y! J& K) ^& h# ~
public int countHillValley(int[] nums) {
7 k5 n( s" r% ? int len = 1;: Z5 ], m1 s1 l1 {/ N& O
for (int i = 1; i < nums.length; i++) {
7 X( q; p. |0 y) z2 @- P! E if (nums[i] != nums[len - 1]) {
9 ~( y- v* O& X nums[len] = nums[i];5 Q2 ^, D* v0 ?# L3 z9 `8 b
len++;2 M# ~: P. ?" C2 I
}4 n9 ^; \7 ?8 h0 [% K& \3 g3 A' ~1 X
}
; w: a. X. K/ o; M- j l int res = 0;( }# L: g& D: Z- r1 P$ e
for (int i = 1; i < len - 1; i++) {
7 W# ]( `$ ~& e0 y8 A* Y$ A if (nums[i - 1] < nums[i] == nums[i + 1] < nums[i]) {: N& V2 m4 J. Y/ c' U
res++;
2 ^7 Z" T* ^; N1 s1 d8 K' } }3 \$ E% n$ R; T% u
}
) X ^6 c+ N- o return res;
6 g- P/ r. S1 ]# m- N }8 G: B7 {, E% r" g% [
}9 I+ Z( u; w/ [& ~# h) v+ y0 a
2 D% C3 h6 `2 Y: e, p2 c: J0 k$ ]
! N! P/ R* n% ^6 s7 b# b/ m【 NO.2 统计道路上的碰撞次数】4 J6 m* c: g% j
+ {6 Y3 F9 L# N/ P" n解题思路
( i* h Y) z' \: x, h+ R从左到右遍历,维护左侧的车的状态,有两种:
) ^+ F, Z# j$ B$ N4 k! y5 l
# E o0 G O3 p& u% m- O若干个向右行驶的9 G! C3 D1 Z0 X/ o5 P4 C
) {. e7 @- T1 S: d( o2 z2 h
停止4 ~1 J) f0 L2 T6 ` x% ^
% [3 @! P1 }* k3 m6 i4 D代码展示
4 t4 {! P. R* [& m! k# X* o3 Q! E+ U7 b+ j! w0 i2 c
class Solution {
( M/ H; S2 [9 Z" L! v. Z. n public int countCollisions(String directions) {) O* E' C+ [/ Y3 z, l! w/ ~
int res = 0;- E9 u8 o) P& h
int S = 0, R = 0;# c0 ^5 T0 i& [" m$ H
for (char c : directions.toCharArray()) {
2 Y* b( x# Z, \& ?( a2 [1 ?$ n if (c == 'L') {
$ T7 y8 D, o. M! @/ E4 e if (S + R == 0) {
" {: _) w# ?5 Q! C6 D" n' Q' d5 C continue;0 T/ |+ W% y4 t3 h& d6 C
}5 G$ v1 ?! f# ^* M
if (R > 0) {
" e! s# M6 m; a ]& ~3 {0 z res += R + 1;% ~2 o2 S8 z1 O9 j! G N
} else {
( M4 g1 N' }3 {) k5 D! E) | res += S; }- \' N1 v% y' p6 D
}( L; H2 z' x& ?& X' o
S = 1;
, K- u }3 J, ^0 y7 Y! r" A R = 0;
' ~! j) K2 _+ Q2 r+ z/ \5 G }
% A$ g2 g4 l5 j+ ~- s if (c == 'R') {
* A& M# M3 p& b ~% b( i S = 0;
" H! O! L$ w6 |! T" a& F' l R++;* Y1 e0 Q0 b4 [8 I s, ]9 Z
}" s6 d1 \5 p6 k& X# I+ H4 e$ h; @
if (c == 'S') {
" ~$ |9 [$ i, v$ d res += R;+ b* ~' d# x8 D1 R3 v) k
S = 1;
* o. }" I! E6 Y, M R = 0;& H3 V f0 @# x+ N1 \
}
! q) @: b7 y; `& r" _ }
3 H' |+ @! l D return res;
* ?+ E) a1 N5 K: i5 x }
( Y& f# U& x+ J/ D2 Y$ |. B9 r8 j}' @ y& h, L( @- B- S, f2 M) I- O
# w1 p9 y' |( N ^/ L' Y% w; m
5 u( r$ [( p: W, H- c【 NO.3 射箭比赛中的最大得分】7 J8 D1 x- L3 \. v3 z
q) m; h8 q9 R; ?! ^3 @+ E解题思路8 J" ]7 w4 ^& `1 m- L3 h3 n
枚举所有的情况即可,实际一共有 11 个得分位置,枚举 Bob 要在哪些位置得分。4 s/ [6 P( n& Y
4 t; h0 q8 _$ t! u" H
贪心地,当 Bob 要在位置 i 得分时,只需要花费 aliceArrows[i] + 1 支箭。/ K- |& I0 c5 D0 A/ o8 ^; E
a2 x. p& A* w; |( O# d; Z
最后多余的箭可以放到位置 0.
! b5 w0 b, \. h
0 |: m- D% |$ w) F7 F9 }( u代码展示& ~$ Z X3 I4 |' p+ Q' l. [3 [
6 P1 P/ B0 ~, A4 ]class Solution {
* F& F+ }( t% Z% r6 B9 \ public int[] maximumBobPoints(int numArrows, int[] aliceArrows) {
& t: W$ q6 Q$ P int max = 0;( {) m% s( f. V' g9 [% D9 q
int[] res = new int[12];
$ F, t. ?* G' `) M; W; A# _ for (int i = 0; i < (1 << 11); i++) {
. X, x7 o$ h* P; G% ` int num = 0;
, Y' u! ~& o. S k int score = 0;4 c: S d" Z* s0 T; d% y
for (int j = 0; j < 11; j++) {
* K/ Z. U( W# V if ((i & (1 << j)) > 0) {
; m/ C7 |+ A$ j, U. q3 G num += aliceArrows[j + 1] + 1;, q @# E8 J' h
score += j + 1;
$ g# C. S* F( l0 Y0 V" G/ U' l) t( p }8 Z# r- C$ P; w7 [; B" V$ \/ q; p
}
3 q/ c8 i1 G+ \3 P$ f if (num > numArrows) {
2 |( i5 l9 K+ l continue;
I t- W8 _+ T* ?0 n# a }
8 Z2 i1 q& @4 L) U3 c. H+ S# i# k0 _- m6 e if (max < score) {
0 {) c1 G, }$ L3 V max = score;
$ x! U2 y- c" |- ~( {4 A res[0] = numArrows - num; // 多余的箭
8 J3 ~1 ?/ P- l7 z* w x for (int j = 0; j < 11; j++) {
- N& G6 u1 R5 W0 I. E. s if ((i & (1 << j)) > 0) {& q1 q8 ]2 E" ], ?9 C1 s8 i
res[j + 1] = aliceArrows[j + 1] + 1;
- G8 O1 Z) z- s6 Z* M1 y+ L, c } else {7 Q5 b n1 o8 G
res[j + 1] = 0;
) l2 q5 B; `$ ^+ i9 v3 _' f. g/ p }' W. j2 L8 w2 E9 n
}3 y3 Q" M [8 _* M$ C5 N
}
. @) x$ b4 @9 @6 R+ ^( K+ S }
/ Y% ]0 L5 `. \+ h4 c return res;7 }! N# q9 c/ ?6 L5 M' O
}
- D" p& ]& N% Y. X+ b}
7 Q# {, O( L1 C+ G5 `
4 K% T; a1 C l% [" m4 g4 ^7 N5 |/ ^4 t7 O
【 NO.4 由单个字符重复的最长子字符串】: y% U: p: N6 w3 M% B- G
4 o2 R N6 O! w* H; k/ K解题思路
; H% ?$ d8 q5 h2 V灵活运用 TreeSet 即可,详见注释。
$ Z& p: ^* p+ ]8 ?2 ^ f& o
7 t8 I% Z- m, P7 C9 ]代码展示
0 a4 c& R8 f; i D8 l+ ^ V \" M6 t5 n% L; d; G0 F
class Solution {3 ]' p) C# N# B4 [' D
. x: n. l8 c( {- I% F1 _3 O
// 一个 Interval 表示一段连续的字符
! Y; a( p9 I! u' E1 b# b* \+ f static class Interval implements Comparable<Interval> {
4 m8 l9 b* ~% v T X' `& { int start;
! U5 i- Q2 i9 R2 k, i C G* h int len;) k0 m* t! o5 S7 R
char c;
! y! v3 k* I( @6 `2 q: q- |- r; v: H1 y' X, c! ?; N
public Interval(int start, int len, char c) {: v' [1 ~3 D, H0 k, U& {
this.start = start;
- [+ \ _$ W* I4 f) e/ A c this.len = len;' _0 k2 Y4 |2 `# S" ?
this.c = c;
4 r! b1 i8 U/ C$ [- F# {, y }6 A7 U- B1 q( [) O! q
1 M7 U) p3 p; r: k: x& e* D. {( {- s
@Override
# {1 ?- n( l- n* x1 N9 ~ public int compareTo(Interval o) {
/ \: s6 \, m" l) [& W- p if (len != o.len) {; m4 H2 j7 w+ B$ F7 [
return len - o.len;
. @$ ~% j7 X% Y }
3 w; C5 @3 M1 { if (start != o.start) {( g+ l& B6 X" }; J: Y
return start - o.start;: I5 K p; K5 r8 `" F* `0 d7 w: \
}* C# d4 F! D+ V; Q2 U
return c - o.c;
3 q% W! M/ O# ]8 y p }9 m0 O8 g/ @1 x% R+ N9 I+ J
}$ S: T* `; {3 A* p: h4 ~. i4 R
8 R9 U4 }" @- d
public int[] longestRepeating(String s, String queryCharacters, int[] queryIndices) {; @3 A, r. N4 U g7 z+ b# O! E
// 首尾新增两个其他字符,这样相当于 queryIndices 不会修改字符串首尾,便于后续的处理1 Q! `& K* c y5 |+ ^1 e
s = "_" + s + "_"; X* D/ Q% |' @" Z9 I6 c
// allIntervals 按照 len 排序全部的区间9 e3 `& I6 H+ Z `
TreeSet<Interval> allIntervals = new TreeSet<>();
2 p5 C3 B. B { h# @. v% C4 f // intervals 按照字符聚合区间,即 intervals.get('a') 表示字符 a 的全部区间,并且按照起始位置排序
# ^0 d9 P w2 N0 H# W Map<Character, TreeSet<Interval>> intervals = new HashMap<>();
6 V& C, M A# I1 P$ V for (char c = 'a'; c <= 'z'; c++) { f# E" S) {$ E' ]7 `
intervals.put(c, new TreeSet<>(Comparator.comparingInt(o -> o.start)));6 l' i! }! h4 w& B
}* ~' i/ U! m- a4 z, J
intervals.put('_', new TreeSet<>());
& C+ a3 S6 [! T
' i/ P% r$ ]3 O // 遍历 s 维护 intervals 和 allIntervals
1 i" [$ d4 f! Z/ n5 i int last = 0;6 j/ ]+ M l1 R1 l5 I
for (int i = 1; i < s.length(); i++) {
6 Z9 m- c. [9 e2 I/ l* _ if (s.charAt(i) == s.charAt(last)) {
% L1 y+ m9 y( u1 n continue;! V; B! z! Z) P7 J8 u/ S* b3 q
}, q. v9 `* y/ O2 H
Interval interval = new Interval(last, i - last, s.charAt(last));
4 Y% M* e0 |% N allIntervals.add(interval);1 D: H( A2 D0 p
intervals.get(s.charAt(last)).add(interval);
. K1 |( |6 F- m& B9 d5 U last = i;. X% x3 `5 l' p0 R3 G
}
9 o$ ]0 d {7 \% t! i7 x y; m' \8 L; J3 Y Interval interval = new Interval(last, s.length() - last, s.charAt(last));
- J3 l; i4 \) D' V1 r/ b" @ allIntervals.add(interval);
0 N [+ D4 P$ v+ t V- c. a intervals.get(s.charAt(last)).add(interval);0 q% ?) r6 t7 _
+ G0 z% s& S$ V, O ~1 a // 每次 query 调用 maintain 维护 intervals 和 allIntervals
. q" h1 p9 {4 i# [- ~8 k int[] res = new int[queryIndices.length]; C3 t* J( V {: H
char[] arr = s.toCharArray();; ?/ l, m1 [$ G& m/ A* b5 D" L
for (int i = 0; i < queryIndices.length; i++) {
5 w1 y H$ n/ Z6 K res[i] = maintain(arr, allIntervals, intervals, queryIndices[i] + 1, queryCharacters.charAt(i));
- v3 ~+ u7 f; C& X* W! x* h- p1 j }
# R8 A, u' m' S& v) k4 w& F( x- j/ E return res;
0 C M- O' C5 D }
/ ^' y2 n. L3 X9 u3 R" R+ }# \+ P: k9 ]/ m- w
// 将 arr[idx] 替换为 c
+ j# n; I5 E2 F( Q& C2 D // 维护 allIntervals 和 intervals
. i2 s4 {; w( z // 返回单个字符的最长的子字符串长度/ p4 Z9 u) ~, A; [
private int maintain(char[] arr, TreeSet<Interval> allIntervals, Map<Character, TreeSet<Interval>> intervals, int idx, char c) {1 T. f. l; [6 f: B* a, p) z
if (arr[idx] == c) {
8 x6 N1 p* i, _1 _) f3 [ return allIntervals.last().len;. s- Q# Y3 X; [. S4 m; p
}
7 \( ^" ]! V% l9 i. V" A& H2 ?$ F+ y
, J% {- A# m+ e% K; P& E- b7 n0 a // 维护原字符 arr[idx] 的 interval9 i( F4 g9 I5 Q4 K. F* [* J
var treeSet = intervals.get(arr[idx]);
* E1 `& j* H- R Interval origin = treeSet.floor(new Interval(idx, 0, arr[idx])); // 调用 treeSet.floor/ceiling 时,只需要 start 即可1 l8 ]& V9 C' u: c( k" F
treeSet.remove(origin);7 j. I! `9 ]% j* H
allIntervals.remove(origin);
s4 p/ W! W/ T8 T- U& u2 @ if (origin.start < idx) {
1 V+ d: ~1 U. k8 A! K" n Interval interval = new Interval(origin.start, idx - origin.start, arr[idx]);6 O& b6 @3 s+ x2 {8 H- t+ D6 R8 o7 _
treeSet.add(interval);: r! C) l. @( ~: M, g- b0 F1 Q' X
allIntervals.add(interval);
; {: A: U, B0 _" C; S2 O }- s( I9 T' S+ O" _# q
if (idx + 1 < origin.start + origin.len) {- w+ Q2 y" ~$ f8 }7 ~7 j0 u
Interval interval = new Interval(idx + 1, origin.start + origin.len - idx - 1, arr[idx]);2 t) J5 ?6 t0 i9 W6 U$ C7 A
treeSet.add(interval);" M; E2 {3 o7 ~; `4 |7 u
allIntervals.add(interval);
6 {) V' g7 w! B8 x }
1 w# } x9 W' b+ I9 P2 }7 I
p9 _% l5 X, C! g& z1 | // 维护新字符 c 的 interval) B/ Z5 O7 }6 Z# k8 i+ X @
treeSet = intervals.get(c);
# z+ s( b+ [( `% W& X if (arr[idx - 1] == c && arr[idx + 1] == c) { // 左右连接4 Y+ @+ q( |! F+ H0 ~
Interval left = treeSet.floor(new Interval(idx - 1, 0, c));
7 z' W9 s; ^9 z/ o Interval right = treeSet.ceiling(new Interval(idx + 1, 0, c));
; N# i0 Z& @. d. R treeSet.remove(left);
3 ]: ], x2 o) s! e4 @ treeSet.remove(right);
; e" J- q9 z$ ^/ c- b' { allIntervals.remove(left);6 P ?) L& o$ t) g' s
allIntervals.remove(right);: ?+ P/ i' f- `: }+ K
Interval interval = new Interval(left.start, left.len + right.len + 1, c);1 I$ l# K+ _% a- A, E3 A
treeSet.add(interval);- A: n/ C' F) b/ f6 x# p9 e+ j
allIntervals.add(interval);) N4 [+ M4 `' D/ L L
} else if (arr[idx - 1] == c && arr[idx + 1] != c) { // 左连接
6 q) n2 f& Z N1 T5 S( P l" y/ L Interval left = treeSet.floor(new Interval(idx - 1, 0, c));
. f1 g' e% Q8 o: n2 |' E treeSet.remove(left);& H" g* j4 J7 e0 S
allIntervals.remove(left);
1 z2 l: u( U, o1 X Interval interval = new Interval(left.start, left.len + 1, c);& q' Z( l" R" V- W
treeSet.add(interval);* y. d2 N. R, |# P+ E
allIntervals.add(interval);
7 ?/ [8 V; Y; ^7 B } else if (arr[idx - 1] != c && arr[idx + 1] == c) { // 右连接
, U/ {! L1 D% \ Interval right = treeSet.ceiling(new Interval(idx + 1, 0, c));" z9 j* w$ j* |" P5 q) F
treeSet.remove(right);
& S M: ^ D# v& ?7 O* @# s allIntervals.remove(right);9 K9 d# s( A* F9 R
Interval interval = new Interval(idx, right.len + 1, c);# s' s% y/ l" S7 x8 V. S- A0 Z# g' h% b
treeSet.add(interval);
% [6 X; l$ e; D; `4 I" x0 x2 Y allIntervals.add(interval);4 ]; V6 r3 A$ V& g: \) J
} else { // 单独一个) L2 g/ H% S" g
Interval interval = new Interval(idx, 1, c);
- R9 [" N( {- J- w6 t/ F treeSet.add(interval);6 B0 g. ]6 V; z8 s6 J3 b
allIntervals.add(interval); q4 c& n" |, ~* r1 X
}
8 H3 @9 h3 E% G
8 Z' @* x9 l" M) Q& W arr[idx] = c;
6 \0 }" u* m5 t0 R3 Q; B return allIntervals.last().len;
- E- g) z; H: j8 f }% Q, [: z* _1 [ o/ R6 S
} |