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

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

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

UWCSSA提醒您:

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

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

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

登录后可回复主题

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

x
【 NO.1 句子中的有效单词数】
9 B' H7 n: U, ^7 c0 t: y4 H1 J$ k! {" B
解题思路: A/ d& H* `9 O& n& O0 y
签到题。. Y3 H( U. l$ K( S0 c1 f. c, |  a

  W, F( \, L* Q& L代码展示: z: h  U& N0 X. U

! i! X/ }" R9 _& l) K. K5 n# @class Solution {0 n7 M0 ^- q* c
   public int countValidWords(String sentence) {1 {5 o# K" ^- d1 \! ^
       String[] words = sentence.split(" ");# J- i$ ~3 P& K
       int count = 0;0 B1 o# X1 |0 |( l7 H' ^
       for (var word : words) {
5 n: I: q; p0 r           char[] chars = word.trim().toCharArray();+ j  Z2 v' I7 C
           boolean invalid = false;
2 i& W/ K, _, J2 d           int index = -1;
; d3 r9 P/ ^' j2 `- j. \6 K: ^           for (int i = 0; i < chars.length && !invalid; i++) {' v  {- |1 c% d; Q% H# e' ^; \
               char c = chars[i];
# n" C1 H1 J* V" r               if ('0' <= c && c <= '9') {
: ~% C3 M  z6 A( {                   invalid = true;
# ?# q' a  W: {& P              } else if (c == '-') {  E2 U/ S) P2 n9 i! T
                   if (index == -1) {& y1 u9 m+ n# H, b& n1 V; m9 [
                       index = i;7 m( S9 s" o! q
                  } else {. D! [' T  S* d
                       invalid = true;
9 `- d' Q; Q  @2 s2 a0 K6 k8 X                  }" {" b; E$ h" O8 U$ F8 _6 o5 _
              } else if (isNotAlpha(c) && i != chars.length - 1) {
; h2 P, j6 a) K* x' k                   invalid = true;4 M  u+ Z& F2 G& o
              }
1 \& ^4 C5 f5 E# p- Q7 w          }
4 f) U; w4 Z, [% ^  O5 X* C3 Z           if (invalid || index == 0 || index == chars.length - 1) {: {, S# x, g  a' z' j6 }
               continue;' x  e& j) m4 r4 n$ _5 v
          }
# f* J4 |! [) w4 N+ `) c           if (index > 0 && (isNotAlpha(chars[index - 1]) || isNotAlpha(chars[index + 1]))) {
1 t& c; U6 M7 q, S               continue;
% f( {/ L' ?- U- i1 C/ b          }
$ {9 t. \; Y% @1 B1 e( X+ ?% s: e           count++;
' Y9 Q" \$ N7 l6 N3 C% y      }
6 m( V, h% u: E; K# I       return count;/ N- p9 S, g( r8 Y* `/ F# h& p7 g
  }
; C! u* a8 |. C1 J" _) E  s1 L
' j. E6 V1 F; j  H   boolean isNotAlpha(char c) {
9 L# n* e( u2 ?& X       return 'a' > c || c > 'z';
. f! ~3 G# U) |4 Z- k* q3 M  }3 ~- [! D: F/ A2 u* U& P$ V

; d. R: Y$ c9 T6 n3 V/ [}
# @7 M# J( q% |7 a; ^0 f
8 m1 B6 ?( _5 j/ a) s5 ?1 T【 NO.2 下一个更大的数值平衡数】
4 i9 j9 h) M+ W& t/ d1 D
( X  a% O( I# a- ?* t. z5 t" `  T解题思路/ o* O8 ?: _& T) T$ s1 \! p2 g0 _
枚举即可。, V0 P" y7 j* d5 E, r

- N  r& H7 U$ T# _' r8 u4 @代码展示
2 X# o5 _( T9 T& U& i! U: S& C* b- [+ q1 }
class Solution {
0 O: V! z: k7 f! F9 _& h0 S# P   public int nextBeautifulNumber(int n) {
5 {3 i  C$ V/ _! ?2 p4 e; e8 T1 r       for (int i = n + 1; ; i++) {: ?" p3 L1 H' O, r% w
           if (balance(i)) {1 t' b5 F% T3 x  P, R
               return i;
. r, J& D& h2 Y+ }; s          }
( o# W4 H0 ?  _- o9 D+ k      }
/ C- w: l6 f- K  v% r% j- `" v  }- Z7 i; N) [2 c9 G6 [) X* j
; ?; Z* J3 k5 a( z$ h0 m4 Q
   private boolean balance(int num) {
1 E* q/ `* I" S, }: T0 h       int[] cnt = new int[10];* _2 Y+ [2 h: s
       for (; num > 0; num /= 10) {4 a1 O( f1 H5 [3 e: U+ D4 r( Y
           cnt[num % 10]++;
- ?5 i6 l3 K7 A, J* @: ?      }+ s# Q3 ?6 Y" o
       for (int i = 0; i < 10; i++) {: f+ H4 k* H6 B
           if (cnt[i] != 0 && cnt[i] != i) {% z2 @# J/ l' {8 A) |6 j
               return false;% K2 A& H0 b& j6 t: Q% O$ h( S+ q
          }
" T# h! P8 D) s/ K  C6 Z      }
: X5 p& V8 s) o4 y) f' S- U       return true;( J6 e$ `. C% q9 J& V
  }
# n3 J0 s4 m; K- ~. R}6 G. D- U2 V& R4 B3 z% N
# B2 g7 `: b7 Y" t8 X! j3 o2 D# \. I. U
【 NO.3 统计最高分的节点数目】* J$ v( x, D1 b8 X- v0 U

$ e- ?2 }4 g1 ]/ w, U* t2 {: y解题思路+ b8 O$ N' X' S( A+ x+ e
首先进行一次 DFS 求出每个节点的子树大小,然后进行一次 DFS 求出每个节点的分数。0 ~- U2 `! r, s' X, ]/ K: N
# h" R, J* H) {' j! y# d" r. C
注意计算分数需要用 Long 类型,避免乘法溢出。
' {& Z5 a4 M; D& v& c' H' @& Y3 P7 c. P. f9 u5 l
代码展示# Z/ u  s! p! {8 l! \9 Z( @8 F/ Z
% U$ ?+ z+ f4 ^. z/ f- i- g4 q, K
class Solution {
& b$ _5 N$ {, b   Long maxScore;) U& p7 Z  ?9 e5 h; Q1 ^. i
   Map<Long, Integer> count;
% \3 d9 c# C+ p; y8 `; \$ D8 g$ b6 r& D1 j7 H7 s. c; F+ I
   public int countHighestScoreNodes(int[] parents) {
/ E- f2 Z" g: h9 j) f6 n, E       List<List<Integer>> children = new ArrayList<>();
5 q+ g) f+ c! @       for (int i = 0; i < parents.length; i++) {
  y; }4 }4 o& A+ \7 k+ Z, o. i           children.add(new ArrayList<>());
/ W% x. B9 ^8 @4 y      }
$ d0 |* h+ ?) x+ y0 U       for (int i = 1; i < parents.length; i++) {
5 y# V, P& O* \3 b5 w           children.get(parents[i]).add(i);
' w$ w; a/ B' M1 m) Z1 \      }7 o1 |% O. q. K& S
       int[] sum = new int[parents.length];
% s! n2 Q. o8 h! J9 t       calcSum(0, sum, children);
8 o7 Z5 ^. l/ O5 k" f       maxScore = 0L;( c0 H) b# P, Y( B! b6 ]8 s( Z4 I) r; Y
       count = new HashMap<>();
" \, \, u: h( g; L       calcMaxScore(0, sum, children);
4 i2 [3 u4 w- a$ \* q% S- K       return count.get(maxScore);: W. a/ `( i3 q3 @% M# ^8 ~
  }2 i+ y. S9 I) l" y' B! Y; ~6 O. X+ \

: ?+ v' h8 v' a/ }4 o* W   private void calcMaxScore(int cur, int[] sum, List<List<Integer>> children) {
2 k/ g4 B8 {8 n6 ?; {       long score = 1;  e/ T. y! @' E- |3 F5 B
       for (var nxt : children.get(cur)) {
+ N: X0 T* O( j0 T9 S2 l' K5 f           score *= sum[nxt];
* V: f9 \: H' ?( X      }, @( d6 C# s% `: _/ ~% L
       if (sum[0] - sum[cur] > 0) {& [9 r  q5 P9 G6 `0 r
           score *= sum[0] - sum[cur];
8 ?  K  o& X0 H& j! T      }
% V, b4 o; z+ q1 H2 ]       count.put(score, count.getOrDefault(score, 0) + 1);0 b; P8 n) U/ g$ _1 h
       maxScore = Math.max(maxScore, score);
# t5 G; B, F8 h5 V* w       for (var nxt : children.get(cur)) {
8 E3 [  w+ t( K5 ]( B           calcMaxScore(nxt, sum, children);
5 k5 f, c7 Q  j, U' _      }
! c, z8 a5 l6 d* P/ K9 \  }
" V3 S: s# z- `1 p( Q, m) E
+ W* j! I" I1 |9 H/ n   private void calcSum(int cur, int[] sum, List<List<Integer>> children) {: @6 r6 q; m/ F( G
       sum[cur] = 1;: d* ~' k4 |9 n( C* P. ?( X
       for (var nxt : children.get(cur)) {
( f/ w. W' g6 h2 ]           calcSum(nxt, sum, children);
1 G6 O1 ?( N  @3 g7 n/ s           sum[cur] += sum[nxt];
6 q% u) a- M. L2 O% W5 u      }
2 X$ L7 R4 F! D- x* r+ |  }
2 a- S6 B' b' m( h# W. x7 U& [# C}/ R* r6 S& {1 P6 ^  L

) F4 d3 d" i/ F5 U) Q! x9 `4 R【 NO.4 并行课程 III】7 `  C) c. I- c. W) R  s
% b- v( l. _3 b
解题思路: L2 b& u0 c( ]& `
几乎是树型 DP 模板题,比较简单。令 finish(i) 表示完成课程 i 的最短时间,则 finish(i) = max(finish(j)) + time[i],其中 j 是 i 的前置课程。$ t1 R, ?: \* d, G4 ?$ e* A
7 {# i- r& P1 ~! H5 b
代码展示
/ T( i+ O- U. W5 d4 H' c
9 q9 Y$ i  M: G; C0 R( J  m8 lclass Solution {
8 i* r  D! ?5 ^   public int minimumTime(int n, int[][] relations, int[] time) {
! h" M7 D+ j! Q1 e       List<List<Integer>> prev = new ArrayList<>();
& H) w- o( b- y8 B# _: [* m       for (int i = 0; i < n; i++) {
% h5 \- V! l' U9 d2 p           prev.add(new ArrayList<>());
* Y5 w' _5 Z' |  R& t/ v4 v      }
% r% O6 Z; W3 P0 R/ w# x+ |       for (int[] rel : relations) {8 c2 m3 ^& \( c& k: M( N8 b1 d
           prev.get(rel[1] - 1).add(rel[0] - 1);; [& |5 m* Q8 `5 Z
      }1 l; x7 I+ {9 R5 @" D: K
       int[] mem = new int[n];2 D) ~, I; X" f7 z  {  h$ P4 q  [% f. {
       int result = 0;
) i! q' r4 M0 B0 C5 O       for (int i = 0; i < n; i++) {0 Y: h8 D! R$ b) \. C
           result = Math.max(result, finish(i, prev, time, mem));
2 ]3 d9 Z0 A( R! P/ @; n      }
5 |8 Y) T3 C7 o+ I7 R       return result;
; f4 B9 b4 Y, L8 ^  }( y% u: z/ a4 P
; ?* g3 ^; b  V3 A' o. V' s1 q
   private int finish(int cur, List<List<Integer>> prev, int[] time, int[] mem) {' i/ D: |' a8 k7 O
       if (mem[cur] > 0) {
, G& E9 m0 W( G" R6 S           return mem[cur];5 T0 G. [: h9 i: [) ]( O0 B3 q/ j: ~
      }* {) @8 G5 G# P& e* b+ {
       if (prev.get(cur).size() == 0) {
0 c' c# x) K# v* J7 W7 z           return time[cur];# }( u$ ~8 o: E  x, k
      }
% R# e, D2 [, v4 G. L( |7 _       for (int p : prev.get(cur)) {. I. |% l9 D6 w0 _0 E1 {" {2 [: p% M
           mem[cur] = Math.max(mem[cur], finish(p, prev, time, mem) + time[cur]);9 {) |, l& N) w6 a2 R5 \
      }
5 P; Y9 L2 Y( O& H       return mem[cur];; u8 b. o/ c$ J) q' k! T
  }1 u6 `( O& h# l1 G6 O: j* ]- E
}
您需要登录后才可以回帖 登录 | 注册账号

本版积分规则

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