登录后可回复主题
您需要 登录 才可以下载或查看,没有帐号?注册账号
x
【 NO.1 计算字符串的数字和】
( M o* c& z8 N, e! M( Q5 v7 i6 C3 r+ G) Z
解题思路/ d+ z3 d5 w& H; {
签到题,循环处理即可。6 E0 O/ W3 P$ t4 B& {9 A* R
: t4 ~& H; [- q. C0 E代码展示4 G( b6 p0 N0 N% O& v! }
# D: M1 j5 {- sclass Solution {! g! M9 c: }; O3 e8 n
public String digitSum(String s, int k) {
5 v- p+ t6 n, @" a. M r: l# f while (s.length() > k) {6 U" x" w$ ~4 v- G% I
StringBuilder builder = new StringBuilder();
8 d+ g' q, c8 o for (int i = 0; i < s.length(); i += k) {
2 ]' d/ q2 |0 P+ p0 i/ h2 I int sum = 0;! n& d: z* S4 n: v( g1 u
for (int j = i; j < i + k && j < s.length(); j++) {( Q) x' m. R) v& t4 M; z
sum += s.charAt(j) - '0';
! }$ x$ O" P, D$ k* H! s5 } x: P }% o2 v! X9 x8 }+ ^ R* s+ c" z
builder.append(sum);
- e" T5 v8 @# z+ |7 D2 [ }7 ^% I$ U0 u" z8 @$ a8 c: k
s = builder.toString();
A0 F- F+ j1 i8 M }! q' Q4 a% L2 S; _) G; |) r; @- [
return s;
" {' o/ E/ N4 v$ R# `/ Z( i; y }0 D. c# i+ T# I0 x/ T4 V/ `
}
; u# y9 ^) t. @( N7 y
* c, s2 |2 B' h- M6 ]( _
; I, W, f# j) z* L. M7 d【 NO.2 完成所有任务需要的最少轮数】
* z5 ?& q, l$ I0 O2 I3 b
$ [9 R1 a4 I9 ~( }- Z Y4 i% ?# L# O解题思路
& e* L1 P8 c2 K/ v' n使用 Map 维护每种任务的数量,然后使用 dp 求解。
1 _0 ]; K7 ]) L! c5 U
8 P- ^+ J; S, s1 r, x$ o# [' gdp[i] 表示完成 i 个任务完成所需的最少轮数,于是有 dp[i] = min(dp[i-2], dp[i-3]) + 1。5 q8 w, ~) @% L
* [9 b" H5 ^& `9 S; h, k5 T代码展示
) p7 w& e% k* y6 Y! M
( }; d' b8 Y4 E1 o4 L" O7 e% vclass Solution {
7 K( E* N" i8 b1 n1 I& U# ^ public int minimumRounds(int[] tasks) {
0 T* z* I! E5 W& h2 ` Map<Integer, Integer> count = new HashMap<>();
3 E# C: m5 e5 C" R! O* O for (int task : tasks) {
2 E _$ e6 @1 P: {/ Z count.put(task, count.getOrDefault(task, 0) + 1);
% |4 L3 `% m( H! \. F }
" J% Q5 b/ N: [/ t! O int res = 0;" Z+ T9 G6 W& s
int[] mem = new int[100001];
6 m2 Z6 Z0 B+ N1 K* j5 y mem[2] = mem[3] = 1;9 u" U8 F/ C" |( z
mem[4] = mem[5] = 2;5 i" A, B6 n8 S/ O8 c( b$ i
for (var entry : count.entrySet()) {. u0 `8 d# i1 E
int cnt = entry.getValue();3 T: J+ r" ] d2 b! ^' b
if (cnt == 1) {' y( h! ]$ {3 Z: H
return -1;: g4 ~8 I. s3 o" B8 ~6 g) h L
}1 Y- |" I6 _8 @* F1 B, F
res += dp(cnt, mem);
! ~' K( R6 V/ R+ K6 F4 ? [& O! p7 n }8 K, l) F, g$ Z. Z% @; w
return res;" x9 T0 e% L) [1 E% r6 y' D
}
2 g; c: _4 b+ l. P0 b! g9 N! q: p k4 l1 [. a
private int dp(int cnt, int[] mem) {
& g i4 i+ |+ ? if (mem[cnt] > 0) {2 f' y2 T8 @8 x6 F" [3 U9 ~
return mem[cnt];
, R V5 u+ O4 C8 P4 r% w }
* W# z2 U& v5 ?% o3 l) n6 Q mem[cnt] = Math.min(dp(cnt - 3, mem) + 1, dp(cnt - 2, mem) + 1);
/ U2 E$ Q9 D6 g) e( p0 a return mem[cnt];$ U; Q4 e/ c7 _/ M5 b4 \0 r
}
; T g s5 X' X# a% M3 ~" N: @& Q}
! j- G& W$ B8 d
5 v: k" q$ W( {! X* g* e: Q
! S* [) d c% I【 NO.3 转角路径的乘积中最多能有几个尾随零】5 N: H* H1 t0 n# v
3 H: Z4 b' N. s8 p/ z解题思路2 s% r& O0 d) j/ y
尾随 0 的个数其实就是 MIN(因数 2 的个数, 因数 5 的个数)4 }6 W, `. F; D8 Y$ A* W9 k9 Q4 K9 A
" w" E3 r b h代码虽然很长,但大部分是重复的逻辑,详见注释。* p5 H3 _5 t }6 A0 U: d6 h0 ~
) L' j, h2 a: L" v( v# p代码展示
" Z- w# I4 Z/ q) W7 y& F: t2 I* i6 ^4 V4 K* }
class Solution {
, }; I- @& y0 [: i; ] public int maxTrailingZeros(int[][] grid) {9 T5 n& v. m) q6 J
// up2 表示 grid[i][j] 开始连续往上走最多能累计到因数 2 的个数
( d1 L. N% B/ X$ M! s" p( o6 g0 J+ O // up5 表示 grid[i][j] 开始连续往上走最多能累计到因数 5 的个数
4 `3 ]4 _& k; ~* ]- N- u g0 ` int[][] up2 = new int[grid.length][grid[0].length];( ^( K% y4 p" {6 ?+ z7 l
int[][] up5 = new int[grid.length][grid[0].length];
1 v/ ~5 a7 J1 o. X9 i! a! a V" q for (int i = 0; i < grid.length; i++) {; X0 g+ h( G0 g1 G4 v' C9 p! B( x
for (int j = 0; j < grid[0].length; j++) {( Y ]& F. c0 c( G
up2[i][j] = num(grid[i][j], 2);
9 |! p0 S/ n6 f9 r2 {$ A3 T- E up5[i][j] = num(grid[i][j], 5);
' Q% p9 g1 t4 k3 w) {% n if (i > 0) {
6 r, R, S1 f* R9 b up2[i][j] += up2[i - 1][j];
5 Q* r8 i/ x r Q% C up5[i][j] += up5[i - 1][j];+ y9 h' b! L, T }
}
# k8 Q: F3 L1 M. ^8 h }
, } \/ Y8 ~+ j/ g$ ^4 y# H. D% f }
/ y; L1 T- t) Q2 Y1 ^% D // left2 表示 grid[i][j] 开始连续往左走最多能累计到因数 2 的个数4 j' |, g% M" R- |+ l l: E
// left5 表示 grid[i][j] 开始连续往左走最多能累计到因数 5 的个数2 C% x H/ L3 h5 f" M' B5 |$ _
int[][] left2 = new int[grid.length][grid[0].length];, S( B* W: d* V, l4 ^' G7 S V
int[][] left5 = new int[grid.length][grid[0].length];
+ p5 Y4 u' t/ J7 o for (int i = 0; i < grid.length; i++) {0 u$ N4 H$ x; \) m7 c. b
for (int j = 0; j < grid[0].length; j++) {* d7 L' Y$ H* ^; e" j. q- u) R
left2[i][j] = num(grid[i][j], 2);! L# B% o/ m+ z2 R& T: w
left5[i][j] = num(grid[i][j], 5);
& k' Q, L, G7 N. m if (j > 0) {$ \) ~& u3 M. g
left2[i][j] += left2[i][j - 1];1 b2 I8 S$ y( S
left5[i][j] += left5[i][j - 1];, }8 ~& T7 M' S
} Y9 Y* r9 O" {8 @1 p& O3 m! U& \
}. C" g3 [/ o P7 n9 `; K6 s. b( N0 E
}
% r) o; e' c6 N% a# g t- b0 q8 J% n // down2 表示 grid[i][j] 开始连续往下走最多能累计到因数 2 的个数
! C, B+ S6 i4 c9 o // down5 表示 grid[i][j] 开始连续往下走最多能累计到因数 5 的个数
/ d7 s; m8 n+ }9 T int[][] down2 = new int[grid.length][grid[0].length];
$ K& _3 g1 ^8 p: Q( g5 C int[][] down5 = new int[grid.length][grid[0].length];
! n' P$ C# V0 W# ]7 p/ b- z6 z& l for (int i = grid.length - 1; i >= 0; i--) {. x' z: m5 _/ J5 M$ g& \- J
for (int j = grid[0].length - 1; j >= 0; j--) {
% H. q. ^& _2 l3 A! ?! d& w down2[i][j] = num(grid[i][j], 2);( M& v0 g2 E5 W
down5[i][j] = num(grid[i][j], 5);
! h# i1 \% |( ~& V- ?; Y3 z if (i < grid.length - 1) {7 H. s$ c2 l% _& f4 w& T
down2[i][j] += down2[i + 1][j];( F3 C8 f b9 a k
down5[i][j] += down5[i + 1][j];
" q6 v) w x, p$ P$ R }2 y2 v* y/ B4 M3 i
}( Y% T# ?( I; g" m8 w
}( \2 x. Z# l# y- F7 F. l6 P7 u
// right2 表示 grid[i][j] 开始连续往右走最多能累计到因数 2 的个数3 u+ u: V# u" T* v' n* _
// right5 表示 grid[i][j] 开始连续往右走最多能累计到因数 5 的个数! ]" {0 s. a1 F5 X6 z
int[][] right2 = new int[grid.length][grid[0].length];/ z$ [) ]" D1 ~
int[][] right5 = new int[grid.length][grid[0].length];
' m4 b8 Q4 c) w9 Y) B+ d7 I6 b( D1 @ for (int i = grid.length - 1; i >= 0; i--) {
4 m% N/ g, L3 G7 T for (int j = grid[0].length - 1; j >= 0; j--) {
2 s- [7 Z2 d3 | I9 r right2[i][j] = num(grid[i][j], 2);( Q7 n; y/ ^' v4 Q8 f
right5[i][j] = num(grid[i][j], 5);( h8 O" |) p$ h& A$ g/ n
if (j < grid[0].length - 1) { S; x) M# C4 | f U. k" r7 @' B. l9 K
right2[i][j] += right2[i][j + 1];
, A4 K3 a6 x: O) T. h3 K; z right5[i][j] += right5[i][j + 1];
6 H& X `3 c: B% _ }
6 f4 L; Z' O0 j! G$ i" D }
) W7 ]: B% {8 [# T3 V. e6 z }
* p3 \! m3 }% O. O8 h* h1 ^" `+ y+ L6 c' p/ J7 _
int res = 0;
. ^: ]2 L4 z5 K8 q5 p3 k for (int i = 0; i < grid.length; i++) {
c: c8 Z' O9 o# x for (int j = 0; j < grid[0].length; j++) {
8 f7 [6 ]2 v" o" E! M$ Y l // 有四种转角形态
}' h7 t7 f a6 I6 j // 1. up + left& J3 X8 k4 h `+ @: m: a
if (i > 0) {
7 w. ]) S6 x0 h% { res = Math.max(res, Math.min(up2[i - 1][j] + left2[i][j], up5[i - 1][j] + left5[i][j]));. d! w) g) p' z+ w( u
}4 ?% g( S% B- f I) x
// 2. up + right. y6 c% X* P% x0 _/ |9 m
if (i > 0) {
4 n; o' I3 [0 d% f res = Math.max(res, Math.min(up2[i - 1][j] + right2[i][j], up5[i - 1][j] + right5[i][j]));
, I3 d i$ W& B4 ?9 s* w }6 Z+ S7 [9 R+ p- }) d
// 3. down + left9 d& h0 @+ r {* a i$ @
if (i < grid.length - 1) {+ m2 Q9 g) u" Z0 o% B
res = Math.max(res, Math.min(down2[i + 1][j] + left2[i][j], down5[i + 1][j] + left5[i][j]));* G7 D" d: C) r1 |' Y. e( L: F
}
" h8 l# a7 t+ f // 4. down + right; P, V! ~: n' i5 H
if (i < grid.length - 1) {
f- U! D. Q+ Q |# Z res = Math.max(res, Math.min(down2[i + 1][j] + right2[i][j], down5[i + 1][j] + right5[i][j]));' W4 y7 y% N5 r
}
. }9 h$ d0 r6 e+ d5 m // 不转角
2 I0 V9 s2 I* P; Y8 P // 5. up/down/left/right
' U$ z, h! @. u& [ res = Math.max(res, Math.min(up2[i][j], up5[i][j]));7 @% R; m& I2 y8 ]8 b* T
res = Math.max(res, Math.min(down2[i][j], down5[i][j]));
) M3 S4 J. T) m res = Math.max(res, Math.min(left2[i][j], left5[i][j]));
# a4 v1 B7 M9 j* d, k+ ? res = Math.max(res, Math.min(right2[i][j], right5[i][j]));
, o% Z# i' U; K3 ~+ ?5 U2 u; l }
8 r, _, f- @" H2 I6 v }; G! W0 }% K; d* R" F. B/ X
return res;
; j4 h4 Q1 Z6 G$ b/ w }: J6 V1 i, m1 A) a0 x* R( k/ `
9 G! P: s8 Q5 N- n, r private int num(int x, int y) {1 s' B v5 G, ~
int res = 0;
4 M, K% c& H6 @5 Q. v while (x % y == 0) {
4 y( I, L2 W1 e8 A" c4 G res++;
! W0 u" Q6 z! b- G% v x /= y;& u# X+ H$ L$ ]: e; M0 M5 P
}+ Y5 i' q! t) h$ Y" x/ f4 Y
return res;
; M8 Z, B W, J1 L: m }$ m9 ^! c ~5 r
}
! b Q" w8 g2 _3 m0 D1 T! K9 e$ Z; ]6 Y2 ]+ {
【 NO.4 相邻字符不同的最长路径】
! e. }( t$ ?) Z9 ~
' I9 S$ j) M4 t. g" Y* v解题思路) N4 p) ?: m+ j+ e5 }
求出每个节点向下走能得到的最长路径,在每个节点的所有子节点中,挑出最长的和次长的连接,可以得到经过当前节点的最长路径。4 ]1 S; J$ b, C4 }2 l( T$ |) }
& p# H0 N9 O) N
代码展示4 N+ E! c7 @* ~/ i$ r1 F+ \
+ B1 E( W9 v; B+ x; p8 g: K
class Solution {! z: a1 ~$ d: g2 A u
int res;% ?8 w1 ^3 ^/ v: q5 y
- I# F |, |3 o) q6 W0 y# n public int longestPath(int[] parent, String s) {
- Z; a2 c1 A D' H, v& y Map<Integer, List<Integer>> children = new HashMap<>();
5 R. Y" [3 v+ r- F6 W0 J8 J for (int i = 0; i < parent.length; i++) {
: L3 {, [2 [# X% A if (parent[i] != -1) {
1 t( ^3 q( I6 w. B5 f0 d if (!children.containsKey(parent[i])) {( [' n4 W5 G0 G, m: U3 }9 k
children.put(parent[i], new ArrayList<>());
& T: }/ w# n) `1 U' ] }9 [( Y2 l! ]# E
children.get(parent[i]).add(i);
' h) J8 H$ ^* ?; x }; I$ m- A" @" ^3 k# Z
}
6 n9 a0 n" x. Z9 g: Z4 p
# r+ x$ l3 y* q6 A0 A& ^ int[] maxLength = new int[parent.length];
3 s8 N5 D8 b7 @3 { res = 1;- t1 r6 d! K. v, O; b, M" s
dfs(0, children, maxLength, s);
# m3 Z# O1 ^# _ Q! X return res;/ m; t! S; J3 \0 F6 z; ]
}
# D# G* q% a3 m6 g) r! E. f
8 I# j8 `0 ]- [ private void dfs(int cur, Map<Integer, List<Integer>> children, int[] maxLength, String s) {
; f# A# {% U, d5 D maxLength[cur] = 1;/ i5 W7 n6 o# i& u9 z$ H- ~( ]
if (!children.containsKey(cur)) {7 n4 d( k% Z; |, p! N& [
return;9 D6 t, j" c( a7 X: k7 e
}
3 {8 I4 m$ G) a: ?* }0 i( n; j% k var curChildren = children.get(cur);" J" Y; {/ e& {
int first = 0, second = 0;$ L$ A3 t1 T) }
for (int child : curChildren) {
! x+ H1 y8 Z* j0 A dfs(child, children, maxLength, s);
, ^0 _2 [3 N9 @- J k) a) x6 O. I" ^ if (s.charAt(child) != s.charAt(cur)) {* y- d/ l) q. b+ ]7 ^
maxLength[cur] = Math.max(maxLength[cur], maxLength[child] + 1);9 g; Z6 H. j7 J! Z
if (maxLength[child] >= first) {* r- t9 M' X% t' K% x7 ^; ?
second = first;
; ?. K( P' k, R2 M5 i; |( h0 E first = maxLength[child];
; v s6 q# k; V8 O* \* G$ ^ } else if (maxLength[child] > second) {
8 ?. m- w/ G$ w: Z second = maxLength[child];9 N/ y* l& Q# ?6 C, ?+ z- F
}4 l& R* z7 z; l3 f# f
}9 _. D0 ?, W6 B6 P$ S& O0 W
}
8 u6 f0 R. z6 t8 E res = Math.max(res, maxLength[cur]);
6 p0 ]" }7 J( Q res = Math.max(res, first + second + 1);1 L, C( O" d4 Y7 P- ~6 |4 G
}2 g4 S. x( L, A$ D* J8 F
} |