找回密码
 注册账号
置顶:如何加入2024届新生微信群

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

上岸算法 回复:0 | 查看:1658 | 发表于 2021-10-24 17:33:11 |阅读模式 |复制链接

UWCSSA提醒您:

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

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

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

登录后可回复主题

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

x
【 NO.1 句子中的有效单词数】
" B# m+ b7 g3 i! `, h" a$ \  o8 k1 _) c: v+ S1 ]# t& ^
解题思路
1 m; p1 J. l* o+ k* f$ W- {; ]签到题。
' J8 U- W% K; J+ D$ j  Y9 B+ A* J
4 t/ m( z+ r  a. P) a/ |代码展示
, Y7 Q" k0 C5 ]% u" s" r
% R. k2 \4 j8 b4 y7 }. pclass Solution {
* ]* Y8 K1 l+ b% B0 U4 E) w1 N   public int countValidWords(String sentence) {
  g/ N2 B& B4 U- P6 N$ {- u0 E       String[] words = sentence.split(" ");
1 A# ?: b) n: Q9 Q/ V$ w) W# y       int count = 0;
, v2 ?$ l* E3 d# o       for (var word : words) {
+ \1 O! S# A9 T, `, L) _           char[] chars = word.trim().toCharArray();& L0 U; i7 h$ q7 F+ N7 }- v* p
           boolean invalid = false;
' S( @  @4 Z' p) y           int index = -1;
# P0 ^# M: M# q8 H1 v" x8 O           for (int i = 0; i < chars.length && !invalid; i++) {
* p* c. p0 u/ G1 r3 z% U) B               char c = chars[i];
& w2 `- \" G  E" P+ r5 J; B               if ('0' <= c && c <= '9') {
; N2 u5 \7 j8 J; ?7 l5 D                   invalid = true;
3 n" p6 ~/ b' B+ k' A              } else if (c == '-') {
. Q3 @; e. [+ p* `$ o$ b- S                   if (index == -1) {
; o8 Q( [5 ~% r! z- [6 y- Q( a* p                       index = i;
9 c; @3 J* u! Z+ D  M  [! g( l                  } else {7 o, a+ }6 L: Z3 w: P" B. o/ z
                       invalid = true;/ _" x. E! ^& U+ V; S
                  }
2 m1 H) B* ]( z3 j              } else if (isNotAlpha(c) && i != chars.length - 1) {# R& Z8 I$ D( p7 x3 l  F9 Q3 j3 D9 Y7 n
                   invalid = true;( e8 u( V8 X) g
              }
! y9 Q0 {) |9 v- v  @* W          }
* R/ z1 j& L4 x' z           if (invalid || index == 0 || index == chars.length - 1) {* e- o7 O  ^- V: K$ b( f1 g& P
               continue;
8 Z. i  e4 b" [6 ]+ s          }
# e+ I( L# k( L) i# @           if (index > 0 && (isNotAlpha(chars[index - 1]) || isNotAlpha(chars[index + 1]))) {
, r/ G0 @# s8 Y$ ?               continue;6 K6 U+ x  i$ l0 N4 g& R# ~7 ~
          }1 P$ U3 n& q, D% Z$ r
           count++;; m% p+ b( |6 {
      }
, j1 E# ^" P( }       return count;, k) G8 ^) V0 U0 k8 u
  }* R+ v( Y2 i* h0 U. E. a

1 C5 L- ~% R% y/ o& s. |   boolean isNotAlpha(char c) {* u1 y; _4 v! O& ~
       return 'a' > c || c > 'z';2 Q' g4 i$ ~. K  I+ J% k
  }0 C* W- d7 _% Q
, d: ~( ^- `: }% o+ j
}1 o; }1 a* Y7 j$ i0 N
+ L* B( N8 x& b
【 NO.2 下一个更大的数值平衡数】
! I; O8 X  Z6 f/ z8 ?6 p$ Q  m: j- [8 a! h7 c# V! |/ {6 S! a
解题思路
  {2 P- Z8 y) _8 s( W枚举即可。
2 Z  M+ J9 {$ A& P# ?
( {! z9 H9 a6 C# P代码展示
# j+ j% p' r* `: x  U
, q* t9 O& v1 n9 a& H0 uclass Solution {
) h' o: F& V8 g* H. ~6 u7 b. b5 l, w   public int nextBeautifulNumber(int n) {5 h2 F0 ?1 d: o0 t
       for (int i = n + 1; ; i++) {
- H8 q& M, b5 ~, }' y0 P5 T% k           if (balance(i)) {3 |. {* ~; P2 J' C+ b+ X
               return i;. y2 {" V0 P: F
          }
7 F& B  G5 c; s1 I2 M      }* Y' G3 @- q( {: w, c9 p' K
  }
* c. j* c/ _8 H8 N$ p. [! ^" z9 O' B, Y3 |! a
   private boolean balance(int num) {
0 E7 X' W" x4 S6 U) s, h/ o& s       int[] cnt = new int[10];
& @: X% ~7 V+ O* ?8 K, X, z       for (; num > 0; num /= 10) {
; ?# P2 u" h1 n+ c" k. x  V8 W& d           cnt[num % 10]++;
8 [; A" z8 ~2 b+ [! S9 k5 q3 D      }. s! V- l6 i5 f0 Y
       for (int i = 0; i < 10; i++) {. \" ?( m) q  I
           if (cnt[i] != 0 && cnt[i] != i) {  a7 l, A; s; K. ~
               return false;
" T& r8 h0 J7 K' V* \          }$ E/ r  D) u" X6 ^$ m/ o
      }8 A* N$ V# q# h! M( R$ A
       return true;
6 x2 p. b+ o" c3 S  }  }5 ~+ A7 u3 ~, O3 D0 S$ [9 x  {
}) y, I8 n0 d# r$ p; M) d
. Z; X) G$ W" V% W; ~
【 NO.3 统计最高分的节点数目】
, I3 V: J! S( i: q* \' p5 x
, k6 m+ J* n# a6 R解题思路6 c0 N+ {: M! T/ V! T4 M& B1 W
首先进行一次 DFS 求出每个节点的子树大小,然后进行一次 DFS 求出每个节点的分数。- A% n! d' M& D/ i) i9 U

, W! m, n" a  [$ {注意计算分数需要用 Long 类型,避免乘法溢出。. c* B0 \$ j' ^4 }( y+ c' n* S
( w% k. H/ r7 y/ Y
代码展示" K' E' f( ?8 v" c

' R; z9 {& _& G9 F7 T+ cclass Solution {- }- ~, y1 g$ ?; ^4 K2 [- a% D7 I3 i
   Long maxScore;
/ k$ u' Q6 k) ^* E6 m, d   Map<Long, Integer> count;
! }/ S4 f& z# G1 h. a
0 N5 ^2 y& [1 Q, g8 J8 V( f/ h$ x   public int countHighestScoreNodes(int[] parents) {8 _3 H. n, h0 e
       List<List<Integer>> children = new ArrayList<>();
) K! K( C  W# S! G5 O- k       for (int i = 0; i < parents.length; i++) {% C4 Q1 E  O# }2 `
           children.add(new ArrayList<>());+ f& t1 R+ P$ U# c# J0 o, `
      }
6 ~6 i! t( F0 x* E5 r3 f6 O       for (int i = 1; i < parents.length; i++) {+ G) {) m# l; q
           children.get(parents[i]).add(i);3 v" W; N9 A- w1 y
      }
9 a! r& O& x, e, l       int[] sum = new int[parents.length];
$ B) o9 d1 {7 H; O# A       calcSum(0, sum, children);
% d( j% R5 @& S: t2 T* n5 o! \% A8 D2 ^       maxScore = 0L;
$ u% J% x9 j8 u* A* d% B       count = new HashMap<>();
$ N0 C. H; ^+ V9 r" b2 `       calcMaxScore(0, sum, children);  w. j2 T, O( h9 F
       return count.get(maxScore);9 d; l5 E- ~. m$ ?5 G7 }* m0 x
  }
0 ~5 E8 y, D& ]9 n1 G& w% r- J3 O
( ~+ \. M3 x2 Y" m   private void calcMaxScore(int cur, int[] sum, List<List<Integer>> children) {
& g& d% M7 ~7 I$ f       long score = 1;: g% M6 f! C1 C2 e, A- M1 z
       for (var nxt : children.get(cur)) {
/ j* T9 Z. [: M! F% w" k           score *= sum[nxt];& k" N# K8 }3 m' U
      }
* I' F. v/ \) R( y6 O2 O       if (sum[0] - sum[cur] > 0) {/ d4 w4 Y& Y/ ^/ h6 S
           score *= sum[0] - sum[cur];
0 o. j- O; t- U8 D5 j- P3 t3 v      }! Q+ W. u1 v; Y; W  _4 a
       count.put(score, count.getOrDefault(score, 0) + 1);7 u; F* k8 i. U4 K& {
       maxScore = Math.max(maxScore, score);5 F4 U7 T! h1 U* Q8 K( t# r; C2 y
       for (var nxt : children.get(cur)) {
; |: @3 e/ e& n  r& ^4 k; r           calcMaxScore(nxt, sum, children);
0 C3 i" S5 \, x2 I* _      }9 d& z0 K- \" l* }$ q
  }
. G5 i3 R7 I( [6 q4 |3 C
7 l$ @( E: \: P/ t   private void calcSum(int cur, int[] sum, List<List<Integer>> children) {! o& r8 N! f6 ], c% t- k2 |$ c& H2 B
       sum[cur] = 1;, K2 ]- X  f9 P6 G$ t$ I; c& _
       for (var nxt : children.get(cur)) {
1 d% R6 q, m+ k/ C( y           calcSum(nxt, sum, children);% M9 w& S! w; D9 Q
           sum[cur] += sum[nxt];
4 @# q5 M; N4 y; j      }
7 T+ t, ^$ q' t  }" V0 O5 {8 x, X2 V. A* @3 d2 [
}
% {* ?' E/ v. u7 n( ?& r9 r3 p4 n" P1 C
【 NO.4 并行课程 III】
! N* m; k0 }% B& p0 K0 a" j- b) {# D* O1 K
解题思路8 c! a, S: c' P1 ?* O
几乎是树型 DP 模板题,比较简单。令 finish(i) 表示完成课程 i 的最短时间,则 finish(i) = max(finish(j)) + time[i],其中 j 是 i 的前置课程。2 c0 g: K) C( o4 B* g
1 D; u' b/ y0 x, I" W6 S- i
代码展示4 R0 u8 U3 k7 `8 G* \1 ~) b" u$ M
) B* o" ]3 c9 }
class Solution {/ p3 `3 J7 n# b8 Y# |: n
   public int minimumTime(int n, int[][] relations, int[] time) {7 v. e4 G) X0 M9 ^4 `5 l2 _+ ^
       List<List<Integer>> prev = new ArrayList<>();' ?- ^* _2 U8 A+ ~
       for (int i = 0; i < n; i++) {
9 I/ U/ y4 [4 d  ?6 V           prev.add(new ArrayList<>());
: o1 E$ D' J% v5 ]      }
8 ?% p# ?  _% _       for (int[] rel : relations) {- S3 [9 {4 ^5 X) {4 ~, q  l
           prev.get(rel[1] - 1).add(rel[0] - 1);0 N7 A9 V/ I+ a. O- Q6 f
      }
$ w5 M; V& b8 e& Y0 b6 h/ f       int[] mem = new int[n];
4 {" c' y9 Z* g. C       int result = 0;
5 m+ N1 o$ g$ b; u  b: ~       for (int i = 0; i < n; i++) {, i& ]0 b( ?& x. E- D
           result = Math.max(result, finish(i, prev, time, mem));' S, b+ e9 l9 @2 F, }
      }
6 J" J" b. f( D& j3 l6 }       return result;& ]/ M7 F- j$ U; l! \/ D- H( G
  }) D4 ?) S/ @# t+ C& H
8 y% T& R4 E8 n2 C- _2 h' ~
   private int finish(int cur, List<List<Integer>> prev, int[] time, int[] mem) {
( ?( o. V4 m) Q  p       if (mem[cur] > 0) {- Z. A  a5 |& @  h! C
           return mem[cur];, i" R1 O; ^5 v% V* p' X/ V1 D
      }# Y% d) S9 A3 C5 w/ C  u
       if (prev.get(cur).size() == 0) {+ u- R* Z5 I2 L* @" W$ }5 X
           return time[cur];' ?$ K/ v1 F8 I& {1 t
      }
  F9 ~5 P9 F: d7 _! C' B       for (int p : prev.get(cur)) {
) q; G5 a4 F. Z% o6 h* U6 @$ \% w$ ^           mem[cur] = Math.max(mem[cur], finish(p, prev, time, mem) + time[cur]);% B& }2 d4 Q6 S9 ^* r& W
      }' m0 o$ N- ?3 X
       return mem[cur];
+ T- a% C; r6 F8 H  X  }
, p' ^. b  u6 a6 A" A}
您需要登录后才可以回帖 登录 | 注册账号

本版积分规则

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