登录后可回复主题
您需要 登录 才可以下载或查看,没有帐号?注册账号
x
【 NO.1 检查是否所有 A 都在 B 之前】
. f# y; l* U! t3 ~' a+ ?, G. G7 C
解题思路
9 C! T( ` T2 b签到题。& F- u: y- U+ Z5 j I
& t1 O0 a! }& a- c
代码展示
$ w0 T% L: ~, ^6 s% R- H
8 `& i. u4 } z* P6 Nclass Solution {) z7 b! \5 r: h3 x4 c2 @& E; J
public boolean checkString(String s) {. B, I% v3 [9 [; D+ P1 [
return !s.contains("a") || !s.contains("b") || s.lastIndexOf('a') < s.indexOf('b');
0 G* c/ v4 W/ ^8 a/ w* Q) V l }3 @( c- n2 T: l* [4 L
}! z S4 g9 n( g- `! t! N' J
+ m5 J/ ~* m6 V6 g( I( A, h
【 NO.2 银行中的激光束数量】# e' e, R9 ^8 P; x$ L
8 X: p1 ~4 P0 K0 G' Z2 @) [解题思路
+ T, A8 d& \; G( a统计每行 1 的数量即可,如果没有 1 则跳过。
4 D* D% }" d: l/ e2 X% {9 \9 y
/ x# u/ ~- R; q. j' X代码展示
+ |# n8 i* x3 f& b' X. Q0 |4 { d9 s4 J, j3 n
class Solution {
1 w$ v* y: P6 T8 Z5 O, A. M: C4 x public int numberOfBeams(String[] bank) {# g. w/ {6 R5 V8 J
int result = 0;! F2 x3 Y% ^4 f3 _; S
int last = 0;: G- p& D7 h0 ?5 i
for (var s : bank) {' P' K8 x3 v% @ d5 d3 u ~
int count = count1(s);
@9 z5 V& g( d# Q if (count > 0) {
$ w: Z+ m- p8 ?) X' W result += count * last;
0 A* K" i. H/ c4 s J last = count;
4 c8 \9 `; H! V0 P }5 H6 i+ Q" m1 ^5 Z0 o+ s
}2 U: T3 W3 a% Y. [+ t3 q z# \2 A2 r
return result;0 G' j* f: I) K; ? \
}
" ^8 U _3 ]' A" d; ~: P
5 ?/ E* U3 k4 U8 W private int count1(String s) {: l! _' b8 g5 o0 L( _) k
int r = 0;
$ |- M }0 M6 X3 G# e$ \6 x for (var c : s.toCharArray()) {) M3 V- d" b! n% j# k( e3 S2 V- E
if (c == '1') {- O3 x: s* `7 u: k. ]& E3 i' h
r++;; Z+ `& s5 E# @1 t/ L2 u# X' D/ M+ J' k
}
2 p/ N- B1 C% O }) `9 ~# p' ?3 x6 d9 g
return r;
) i' @& O" C, `" F% E! Q }$ G( v( [6 s" t& E/ M/ ^
}% G0 r1 ^- s0 [
: S0 q! G; b5 |" _5 n
【 NO.3 摧毁小行星】
+ i: R. z0 Z$ T6 B+ C
4 |8 b: n/ n1 Z Y4 f解题思路
1 C) N" p: f$ x3 Q" Y排序即可,注意使用 long。! H6 l* i2 X4 h) T
# E, t9 J) C* ]! G5 x9 `
代码展示/ T, c% l! B" _3 q, G) A* X" u6 {* x
5 g( ~# r H7 v0 lclass Solution {
- U% N7 `' I* f% y& G public boolean asteroidsDestroyed(int mass, int[] asteroids) {% ^# J: Z* @1 G1 [
Arrays.sort(asteroids);7 ^3 a; A5 F- F. w
long m = mass;8 G& @4 C2 E+ o. m# t& f9 H
for (int a : asteroids) {5 ]8 ~ f5 L4 E. f- l, z8 v
if (m >= a) {6 z {5 j4 a! R( o0 C9 P
m += a;6 K1 a5 d$ h* B% I( ~* p" o
continue;* T# d( ?. G0 v, W; o3 {8 U
}
' J5 o, e2 f, |' w4 C return false;% d' e2 O) p3 }2 g7 c: |/ x
}
7 j0 d6 I( L9 J5 i' ]4 y return true;; g! T/ ~$ b u; e
}% R3 S/ b) q7 X& H& x
}
' s7 K; q+ V3 c4 t# P7 J! x
& ]% X+ v8 s5 v6 {2 C【 NO.4 参加会议的最多员工数】
: C9 ^$ z, \2 X( K
" Z* N2 c5 ~' \解题思路) s7 ~: }- {" o1 X) c& ~/ ~
实际参加会议有两种情况:
! o' {3 @% [5 n `& F1 c* H
7 B" A0 W T4 [2 p$ P9 Z刚好是一个环中的人参加会议,这样可以使得每个人都喜欢自己左侧的人。1 T" l, c- x# s& j4 ^
4 o2 h" }* `. I9 V( c/ ^! Z有两个人相互喜欢,然后剩下的人形成一条链,比如 [1,0,0,2,1,4,7,8,9,6,7,10,8],这样不要求首尾相接,可以有多条链。$ g3 ^, z. T' C2 l8 r; x
3 J( j" q4 V9 i1 M7 n8 f) S& {- w2 N) |
代码展示3 A- e# H0 r" ^1 q# i
: F/ y$ Z) V) z5 |+ k8 Z& Sclass Solution {
0 r. ?; J' O+ D0 v! W public int maximumInvitations(int[] favorite) {
# Y+ ~% U( w# r. I // 1 找一个完整的环
5 T1 o! O. J0 g7 k: x2 | // 2 找多条链# m$ @8 F# A4 I( e
return Math.max(maxCircle(favorite), multipleLink(favorite));* d" ?7 i4 d! b
}
+ f7 Y8 |) ?: g0 d" ]/ L* y9 v( U8 E9 g$ _
private int multipleLink(int[] favorite) {
8 D, {9 j9 ]) Q& N! R Map<Integer, List<Integer>> reverse = new HashMap<>();! E, W0 l6 e* j. i6 m, T% G
for (int i = 0; i < favorite.length; i++) {- T) R) y2 u9 b- i% m
int from = favorite[i];, M. K' `+ L: o5 q: H+ a
int to = i;
7 H+ {3 q/ d, J9 {5 L8 J if (favorite[from] == to) {3 t) m; T/ R) P% M# r. A" R
continue;9 B1 G( T: v6 Y& t' J
}* A+ Q% I% I9 G
if (!reverse.containsKey(from)) {7 ~; Y& {! l/ f5 e8 v
reverse.put(from, new ArrayList<>());
/ \" `- d3 l1 A" f; O }
! e- q4 r* ]& ]' S V4 ^ reverse.get(from).add(to);2 S9 p' }! `' ]* m0 ]
}) a% X; R, i* b8 _
int result = 0;
. { H% T9 W" E for (int i = 0; i < favorite.length; i++) {
' i" G* R: j# P if (i < favorite[i] && favorite[favorite[i]] == i) {9 I$ X* f% x& g! Q: K& w) j
result += maxLen(i, reverse) + maxLen(favorite[i], reverse);. i& G7 c- I/ G, c$ v, f
}
8 K: g& X8 I i$ T6 M$ D }
- r2 T8 K3 ]% U: Y6 | return result;
4 ^8 p0 V A$ ~! L0 I) ` }
7 \* t, y( e ~) V/ K6 ?/ @: \& y+ I" h8 |$ f
private int maxLen(int start, Map<Integer, List<Integer>> reverse) {7 {$ x5 O1 r' g! u
if (!reverse.containsKey(start)) {
z9 w' X. U& B return 1;( v: e7 x( g' O3 s' `" @& Z
}+ n6 t/ d; O* B' I6 ?7 t
int max = 0;
0 s6 d( ~3 V7 l+ x( K; i for (int next : reverse.get(start)) {, h4 Z5 P/ |# v, ^# ~
max = Math.max(max, maxLen(next, reverse));' a5 l+ h% R" H+ _+ y
}5 v9 b. j5 U9 p8 M+ e) C0 L" o
return max + 1;
/ N4 ~. f7 m0 T1 A7 I }
+ `% X: N: }0 F; r, t. O0 }$ a
l9 }0 s9 Q6 {, T private int maxCircle(int[] favorite) {
" T. c Z" F- r, Z J* |; T Map<Integer, Integer> step = new HashMap<>();
1 k& u: K& z& Z int[] max = new int[favorite.length];
- c5 S, P" q" [: W# g, [- R int res = 0;9 I9 v6 U2 Y9 f: L% F/ ~- c
for (int i = 0; i < favorite.length; i++) {( ]" [- r; { k. Z9 K
if (favorite[favorite[i]] == i) { R/ ?# y4 W3 o6 O& C( y/ T
max[i] = max[favorite[i]] = 2;9 q7 J1 S0 k- @/ e$ p
}
! y2 ^6 u& s! P7 ?; f$ [5 G- W if (max[i] == 0) {5 F. V2 T0 k, G, v6 S
step.clear();" ], h+ Y4 @" q4 T3 \ F. d2 T
getMaxCircle(0, i, step, max, favorite);" d# c" x3 H& Q; T8 i @' R
}* L7 ^/ P8 `& z5 ]
res = Math.max(res, max[i]);( `" }' T) y- p+ e' x
}
* l. x) _# h, h" u6 o+ i8 Z return res;, M3 t7 w& o5 A- |. a0 V8 Z
}
1 w8 N: b' z4 ~$ c
: O+ K; v5 B5 C* ?- e0 D% n5 D: K private void getMaxCircle(int i, int cur, Map<Integer, Integer> step, int[] max, int[] favorite) {2 N( `3 h2 M/ x+ P
if (step.containsKey(cur)) {- K6 Z4 K" l! ^' X; T& e1 ]. j
max[cur] = i - step.get(cur);- g4 S5 C0 Y8 v H3 Q, N( r( e
return;2 ]1 v& j, f" W9 f Q! X
}5 H' L" Z. G3 X
step.put(cur, i);
R* G" U6 c% L- d7 F9 U* H( _ int nxt = favorite[cur];; r9 H3 _& d' s+ i* M. f9 J
if (max[nxt] > 0) {
- m2 y8 q: x& x" L% s/ o3 u max[cur] = max[nxt];
1 |* {1 @7 H0 L; c% o8 R return;- ^% { t$ X. G$ F0 x7 Q! |
}% L9 x( `# d: q
getMaxCircle(i + 1, nxt, step, max, favorite);$ z8 n; |9 Q/ p/ W2 |
max[cur] = max[nxt];
, N8 N% b6 r8 |' c, E4 L3 ?7 K6 m }6 g+ F/ |+ A0 \' v0 u
} |