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

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

上岸算法 回复:0 | 查看:1507 | 发表于 2022-1-3 21:30:37 |阅读模式 |复制链接

UWCSSA提醒您:

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

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

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

登录后可回复主题

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

x
【 NO.1 检查是否所有 A 都在 B 之前】8 w. V4 K* U& K$ v5 m$ D

* @+ H, M. c" h3 f, S解题思路. l& G, i( g& n% d
签到题。
! z2 l" j/ z" y, s! G+ y/ l7 m. C/ F5 i! I8 Y7 @2 d$ L* U8 E
代码展示
6 f3 n: n" z: H- b7 M2 n; ~
# \' ~- [, z0 E+ Gclass Solution {
- ]# ^( M, _# D   public boolean checkString(String s) {% `  x1 s5 Z% B$ @: [% i# J; Q* R
       return !s.contains("a") || !s.contains("b") || s.lastIndexOf('a') < s.indexOf('b');5 c/ S  w6 U1 _  i
  }
, Y: {& Y: d' d: W& X) F# q}
0 z$ ^6 `0 e4 W: V1 |- ]- {) d1 g5 H3 ?& d, O& c  x* M
【 NO.2 银行中的激光束数量】
$ g9 \* x- k8 c1 O1 p9 d9 o3 V7 @9 @6 ~6 ]# I; Y5 p* J9 _
解题思路' {& k+ Y6 m" ~0 R' Y. v
统计每行 1 的数量即可,如果没有 1 则跳过。) O; I  {' p# L" l

# S4 \! z' L: u/ K代码展示
6 M8 ~3 Z4 }8 p& r0 O" P5 C+ L* c' L# o5 Y
class Solution {
. _  ^8 l3 Y' H9 T  z9 @: g   public int numberOfBeams(String[] bank) {8 y) q% k6 b/ E3 N: R
       int result = 0;, ]5 n, l7 W$ j5 h3 ]( A+ f  W0 |
       int last = 0;0 d  o7 A; f1 w
       for (var s : bank) {! ?- g- d# s' r( b  @3 [6 \* w
           int count = count1(s);( o0 f$ t; [: T7 [
           if (count > 0) {' i( Z! D4 f8 d6 i% w
               result += count * last;
$ r" H2 |0 L8 c1 g. M               last = count;
) i( g, Q, f3 R5 }/ ^0 h          }
- D& C" B$ Z, d9 I      }! F& {9 n2 m+ ^( N  V
       return result;
# y7 i- R- a# v) E* y7 ~) J- M  }# c" w  [3 z1 c

4 R* u* d& N7 I! m* k) p5 ]   private int count1(String s) {
& j9 k* v; [- k5 k       int r = 0;. w  p. k& V4 {* B1 }2 J! f
       for (var c : s.toCharArray()) {. ^3 x& B; I3 H4 I
           if (c == '1') {. G$ m) i! u2 J1 S+ y4 ~3 w$ e3 V
               r++;; {/ P5 u; }! c* W9 U
          }, v8 c: b/ T( E. t  c5 m; J" T
      }
: v9 J; x$ r) l! o' Z7 o       return r;( M9 m6 i+ i5 g
  }
! ^+ e6 z' |$ |4 U}, d  Z9 x' d5 _! Q( m* I
; f) Z. O! n7 G# Q0 r
【 NO.3 摧毁小行星】
: w( S. `* k) a0 \/ D; e7 Z( x
& }4 `  Z0 C+ r' Y$ h解题思路
+ M% h% p$ g$ Z0 Z排序即可,注意使用 long。
1 D# g/ k5 {; a+ R# c+ C+ p" o5 s0 S- \
代码展示; P: k2 n5 ~* S& i. H
, }5 ?* Z: x3 h
class Solution {
" K6 A. C" b/ G% h# T   public boolean asteroidsDestroyed(int mass, int[] asteroids) {
, _+ l8 h7 n* e/ {       Arrays.sort(asteroids);
, x" h- @" ^- ]; N       long m = mass;
4 r( }1 |0 b, T& i( G- C2 c8 p6 A       for (int a : asteroids) {
  V- W$ L4 X2 Y: E6 g( Q           if (m >= a) {3 g6 ], J! ~3 F5 E8 b9 v
               m += a;; t) O! f( t' G$ J
               continue;4 ~/ S3 |2 n- k( a; x+ q
          }
4 q$ I9 ]3 {" i, I$ v  F2 a/ }2 l           return false;
, [  ]; Z) e/ E+ ?      }+ o8 x' [- v% v5 m
       return true;- S& ^" `  ^7 y4 s
  }
  I; w- ]' o$ X; q' t}
9 f, Q* M0 I7 ]4 G
$ }; w, L) g3 W1 \【 NO.4 参加会议的最多员工数】
9 g- V7 P' ^: y0 W) [' W
5 p. n9 A6 R' Q+ H解题思路
, i7 K+ {" E' T% ]7 y& C+ n实际参加会议有两种情况:; Q& a& z1 x4 n) E

3 T# w- \1 g. R1 S: K刚好是一个环中的人参加会议,这样可以使得每个人都喜欢自己左侧的人。1 Y& ]1 q6 Q2 l

+ B; Q0 s& @6 ~) r% b6 t% y有两个人相互喜欢,然后剩下的人形成一条链,比如 [1,0,0,2,1,4,7,8,9,6,7,10,8],这样不要求首尾相接,可以有多条链。) s. b4 z# U/ }5 x: T! R8 G1 f0 G

7 R  `! E% I6 b' l; Q( _5 d$ v
2 \3 D" g6 M4 W+ |代码展示
7 x8 M0 k2 H- o% z3 U6 ~# ?- E3 Q/ R+ s* |' r( @$ y, T2 [
class Solution {. e6 |8 {' u6 U7 ^
   public int maximumInvitations(int[] favorite) {
7 [' X$ N4 e0 V4 D4 d8 q$ Y, g/ O       // 1 找一个完整的环
. }& k8 |  p0 }5 j5 c+ H5 f0 e) z       // 2 找多条链# z4 Z* z. Z" s& P! q- V' e, O
       return Math.max(maxCircle(favorite), multipleLink(favorite));- \6 Q) B$ Z4 i, g* ~; X
  }
1 k, s5 G: A' X4 r
3 }* ?* {4 t% Z% S% a" L8 Z   private int multipleLink(int[] favorite) {
2 N( x4 p$ k+ D$ Y3 C: r( L. }       Map<Integer, List<Integer>> reverse = new HashMap<>();$ a  U9 n; n, B; w5 A8 i
       for (int i = 0; i < favorite.length; i++) {
6 G, ?# ]2 a" R" C" ]           int from = favorite[i];% {! `% _+ y3 ^' X( n
           int to = i;
; c2 b2 Y2 h. Y; j0 N           if (favorite[from] == to) {
9 h" x, ^" E) U" G  M               continue;1 C% }" o( \9 z9 H; O' y
          }$ A+ Z: ^) Y% h& U: K' T6 c8 J
           if (!reverse.containsKey(from)) {. C. `; j' y& F- q/ P
               reverse.put(from, new ArrayList<>());7 {" k1 Q) C" ]8 G2 Q; \
          }9 j' o" W- s# y
           reverse.get(from).add(to);0 C: c: F2 r% U" S/ y/ B
      }3 s/ g1 ?& X' O
       int result = 0;6 q5 ^" @' T$ X  |* `
       for (int i = 0; i < favorite.length; i++) {
9 i' _9 l' L  Q- ~0 C% s0 x           if (i < favorite[i] && favorite[favorite[i]] == i) {# Z& ^4 P5 W8 A. f8 q. Z3 x
               result += maxLen(i, reverse) + maxLen(favorite[i], reverse);, g7 X- u: W, e- ?6 s0 u& o+ \
          }5 u5 e+ s% c7 V
      }
6 Q1 z! m1 M9 ]7 E% D4 H, Y       return result;+ L0 @& b, T, f8 q$ y" w) o
  }
9 [/ S) g6 r* s% z9 r3 i% O# F( `
   private int maxLen(int start, Map<Integer, List<Integer>> reverse) {+ {/ M# Q6 Y+ Q6 ^# x3 ?* P$ b
       if (!reverse.containsKey(start)) {* j9 w$ I" ]9 X3 f, z- H
           return 1;, J$ h! m3 |- M9 G1 H: L2 o4 L' d
      }
: x8 l& _+ [* j# J& {( N+ ?+ h' [9 ]       int max = 0;" R# c- ]! n5 s, s0 c3 B# D9 K  p
       for (int next : reverse.get(start)) {
) N' u" e/ e4 V1 e: q           max = Math.max(max, maxLen(next, reverse));- C" z* O  m% h
      }
% @2 R& U+ c8 N" l# o6 U       return max + 1;
' h' Y2 D$ q) k  }
1 [" }$ U8 m- s
; P& s" w. w0 V' z   private int maxCircle(int[] favorite) {. ^, I' y# c# q" `, R6 r
       Map<Integer, Integer> step = new HashMap<>();
+ a% p2 }$ F6 N7 e6 y% e  T* P       int[] max = new int[favorite.length];
# M. J; G3 p( n       int res = 0;4 l. ~8 c. [: t# O
       for (int i = 0; i < favorite.length; i++) {9 f) B: s9 `4 r- I9 w* U
           if (favorite[favorite[i]] == i) {- _. R% v8 S9 e1 Q/ p$ m9 u0 J4 @3 z' Y6 _
               max[i] = max[favorite[i]] = 2;
4 K7 [2 t  B9 e5 }0 ~" X          }
  F( ?5 {. T4 t$ o" Z7 G. c           if (max[i] == 0) {7 R* f. C6 d$ h- H
               step.clear();
, Q  q& ~, C) q3 }+ o2 F               getMaxCircle(0, i, step, max, favorite);
8 z. ]) Y5 _; ^# S# U* {* |& c3 ]          }
! X$ A( l! t. [9 I9 W9 ]           res = Math.max(res, max[i]);
/ D  A6 A1 V+ x' Y$ n      }$ A* {7 A, j0 a* {: F$ a
       return res;& R; ~2 z3 J! Y6 X; n
  }
% k! ~6 u9 x. P& B8 D) [" v" N
- z4 \8 ?% S1 a" c   private void getMaxCircle(int i, int cur, Map<Integer, Integer> step, int[] max, int[] favorite) {
3 Q8 M! j9 [6 O0 g       if (step.containsKey(cur)) {$ [  i- [4 i  k, e) [) y" g
           max[cur] = i - step.get(cur);
9 A8 R0 X- _2 [           return;
% w- J" Q( e7 A0 W. R# n      }
: i2 B' v& N( t* T% ]2 o       step.put(cur, i);" [& O1 T2 \4 f0 T" E
       int nxt = favorite[cur];. L2 \! y3 O% c  [
       if (max[nxt] > 0) {
  {4 k7 ]9 m: U  C           max[cur] = max[nxt];
$ @; S! Z) x% G* @           return;
: a# A( K% ^( C% J+ n% M      }5 m+ i; |* v6 ^$ p: R( G8 ~
       getMaxCircle(i + 1, nxt, step, max, favorite);
5 S. [7 e' w5 Z3 w       max[cur] = max[nxt];- m3 z/ H/ F3 f$ T' ~# E
  }  _8 w/ E: W4 u! r$ a3 ~
}
您需要登录后才可以回帖 登录 | 注册账号

本版积分规则

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