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

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

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

UWCSSA提醒您:

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

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

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

登录后可回复主题

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

x
【 NO.1 句子中的有效单词数】
, F, h! x4 p! r" Q, l' q7 I
' p% z  \) P7 _8 X解题思路
7 ], K0 d7 _2 D( E5 P& p3 Z4 X签到题。+ v& ^7 k- c- g6 ]6 J9 U1 G

9 o2 {. r$ R- d* a* l代码展示8 |# p$ U4 ^$ ~; n% @7 R
1 g; U* h2 Z/ a
class Solution {
( o  ?/ n% z) |# Z, y1 C   public int countValidWords(String sentence) {
. R1 z3 i& p  d; w! _4 N- w       String[] words = sentence.split(" ");
+ @9 j$ I7 \# p0 u7 E) t       int count = 0;3 c' e' O8 s0 B- ]) E0 F' O
       for (var word : words) {  y# f1 Y" `* E2 V  o1 H
           char[] chars = word.trim().toCharArray();
2 `8 E4 z# A! N( z           boolean invalid = false;) P, Q5 p5 R4 Z, k& ?3 T7 N- v
           int index = -1;
1 W2 U" h' f9 [4 W           for (int i = 0; i < chars.length && !invalid; i++) {! g4 t# O0 M" m  }
               char c = chars[i];% f! G3 ^' Z2 G
               if ('0' <= c && c <= '9') {
$ |" w' Y% k' y! j. W                   invalid = true;
% G7 k5 C( b4 e+ `              } else if (c == '-') {4 H9 _, H& M3 L" @, S: M; i
                   if (index == -1) {
: y. a5 [8 y8 L1 L# V                       index = i;
" f4 J+ y8 q+ K+ ~1 w! f                  } else {) n! ?- R5 d" o- U  z7 k
                       invalid = true;' E" A  e" c4 |; [, c5 p
                  }
$ D0 k7 j& O# D              } else if (isNotAlpha(c) && i != chars.length - 1) {
: d$ ~9 k! u- A  Z/ B, W. j* t* |                   invalid = true;" a2 ]/ T8 Y* k5 V, l; ~
              }3 Y- P, D" m+ t# D' @4 `: P% z3 h
          }, O7 p; R* n$ V, T4 A( I; i
           if (invalid || index == 0 || index == chars.length - 1) {! J8 B; M( h3 D
               continue;
, ?( F& i5 [2 D6 P. @" b          }
6 N' O" i- R/ d# Z           if (index > 0 && (isNotAlpha(chars[index - 1]) || isNotAlpha(chars[index + 1]))) {0 [$ x* K+ K) p
               continue;
5 \" T  P: E9 _6 D, {% ?          }3 `, `# d# M) N7 S" i7 j
           count++;
8 w; o' H, p5 ?2 v      }
; z: y8 R& ]9 N+ j* l  L       return count;, a; u: B  R9 ?8 B. G( N
  }
, b: s8 l" K8 K( p/ U/ l5 [
) i6 H5 Y" t' O; S. z, Y7 E( Q   boolean isNotAlpha(char c) {4 R! ]! N0 n$ H7 U( q
       return 'a' > c || c > 'z';% j( p' u7 C4 @0 r4 n
  }! k" s' g" Y% s5 c- }9 A' T2 g

/ ~. w# l% g  F: w5 P}& m8 r9 {1 L5 d: l- d
/ C6 W  X2 N0 i" U+ R% r6 K
【 NO.2 下一个更大的数值平衡数】
3 b" ^& {' p( a+ Y5 T" ]# X
$ O' ?8 |0 M7 h1 N6 f( D8 @1 z解题思路
2 P$ W  m" g4 F/ c% N( `4 Z% l% U枚举即可。5 K! t) e1 }/ \; w# a8 ~  U% \2 ], V
. O4 r, k3 ~$ w
代码展示5 ~; r  p7 y) J& ^: e. G* g2 [. p
. X3 k+ {. {6 Z$ ?, R3 d& |
class Solution {2 K7 z% u% l# w4 d* J% g$ M. D
   public int nextBeautifulNumber(int n) {
" D- ^( ~$ X1 Z3 g, h       for (int i = n + 1; ; i++) {
# S2 i1 d. ]) I: Q8 Z8 a6 L6 v& T           if (balance(i)) {
8 n7 D' Y  x" k3 _' F9 D               return i;( z8 |3 v+ e# x$ n+ W$ U
          }
& J9 B* }9 I. ^5 J( V  Y      }" n! W8 D2 V- V: s
  }
% x9 P6 n+ \1 Y1 z5 Z: V
. o+ H8 ~. u8 K* V# X$ g& G+ `; w   private boolean balance(int num) {
5 m; @4 {* M" N2 s/ Q  Z       int[] cnt = new int[10];
6 |- `* N; M: `& h# K! z) _       for (; num > 0; num /= 10) {
. |8 O% e+ B+ m0 X1 ^: o3 ~* x           cnt[num % 10]++;
1 o: X: P1 g$ _4 w5 a# }% D      }( X" D7 ?, |3 ?* q. U
       for (int i = 0; i < 10; i++) {
0 F& b4 S" q% h6 v           if (cnt[i] != 0 && cnt[i] != i) {
! Y3 N$ o& @1 S! h4 I& d               return false;' X+ j' T* X" g; j: V
          }' E4 c: Q0 c' ?$ \. f2 v
      }
8 A: D% s7 l& G. y       return true;
  J' f; I8 u9 f6 F4 \  }& ^% Y+ s/ ~3 M% K, {- L' Y
}
" K4 Q' W. a1 a$ p& W1 v" S+ \- C* V/ B1 u& f
【 NO.3 统计最高分的节点数目】
, f# Y5 R( ?( j0 t% ~5 G% K! z2 [! I2 Q3 a) @1 |+ q0 x
解题思路) |! b. n( ]/ `# l# p7 O
首先进行一次 DFS 求出每个节点的子树大小,然后进行一次 DFS 求出每个节点的分数。/ z/ O, M& H; i  a/ q$ ^

- w; @# t, ~7 Q注意计算分数需要用 Long 类型,避免乘法溢出。
' V2 m2 c: Z2 z2 I4 d) f% d5 y) V3 `: Q- P6 [3 Z/ j" d
代码展示
  S& R- g4 t% T. A4 j; s- F2 _2 E6 T) p; T
class Solution {( @8 K. m' p2 N
   Long maxScore;8 e% ^, I+ ^% w% w; [. S
   Map<Long, Integer> count;" j7 X7 F* Z/ C# b$ H, C) z

3 g* i9 Z  k, G* w% j   public int countHighestScoreNodes(int[] parents) {0 Z/ B. `) P! U: t; g
       List<List<Integer>> children = new ArrayList<>();. d1 v4 `9 L/ i; l8 Q! {" b7 p. w
       for (int i = 0; i < parents.length; i++) {
4 Y. j0 A6 M# @$ h: w. g           children.add(new ArrayList<>());
3 s6 J% d1 D! n& H/ t6 O      }
" x4 _6 ~' U' ]9 T5 B$ T       for (int i = 1; i < parents.length; i++) {% B5 a: F" }8 d4 _
           children.get(parents[i]).add(i);1 ~$ j! Q9 Z9 v( ?% d6 a# J
      }' w7 g+ i3 v- S2 T6 j% E. I
       int[] sum = new int[parents.length];4 I2 W- `6 N5 G
       calcSum(0, sum, children);
6 P6 P- C3 w" N2 L) W! q6 i       maxScore = 0L;
6 |3 V1 d# x" o; }% k3 {8 e6 k: c       count = new HashMap<>();1 q/ ?. @0 B. h: D0 [
       calcMaxScore(0, sum, children);5 L# D7 W. A6 m6 j
       return count.get(maxScore);1 S9 }6 t+ ]& e  u
  }
: S  g% E/ Q2 b, u, E, c0 P. j( F# p9 s9 h/ U# @
   private void calcMaxScore(int cur, int[] sum, List<List<Integer>> children) {
9 h& N7 Q, L, w: _& T% M+ ~       long score = 1;
2 }; m1 c# ?, l/ L6 K1 \       for (var nxt : children.get(cur)) {
' U$ O6 g  J9 z4 u7 p. ~           score *= sum[nxt];  j% k" |3 x, n) W: T
      }
: E( I6 B* j$ V# Q7 |       if (sum[0] - sum[cur] > 0) {/ A4 r: u9 L6 G3 f5 b2 M
           score *= sum[0] - sum[cur];
4 H# p& G$ P% b$ F      }
, n1 O/ J7 P2 P       count.put(score, count.getOrDefault(score, 0) + 1);6 _, ?$ m7 A6 e: J7 Y  h1 M
       maxScore = Math.max(maxScore, score);
1 I, X. I" i6 U* P5 j+ t5 }3 S5 D       for (var nxt : children.get(cur)) {7 [7 K/ k2 r9 j0 X
           calcMaxScore(nxt, sum, children);
* I& }7 [6 p$ Z1 A" W7 c; v      }; _1 G, a  w/ }5 t; v+ s
  }
  a& ]. e& q2 k0 W' U
% E1 O% r1 U" G0 A' z1 }   private void calcSum(int cur, int[] sum, List<List<Integer>> children) {) U7 q6 i$ m, P$ @- a* x; m
       sum[cur] = 1;
. _( h3 N7 ^6 k       for (var nxt : children.get(cur)) {
/ M0 Y3 o! ^5 ?. y           calcSum(nxt, sum, children);
" r# C) g, l! V: S# A5 r3 a4 X& [+ u           sum[cur] += sum[nxt];
6 Z: R; @  F& d  H5 T      }
0 T% h. L( k! l  n  }( c5 u. K, h( R
}
& o  ]% l# u! j/ j# f( P3 B! G) @$ L5 o6 e7 p! J
【 NO.4 并行课程 III】
8 I+ P9 w$ `7 D- S& i; ]( Q2 M/ _! U  w! J
解题思路
2 Y: G( n8 g  a6 b" g: ~几乎是树型 DP 模板题,比较简单。令 finish(i) 表示完成课程 i 的最短时间,则 finish(i) = max(finish(j)) + time[i],其中 j 是 i 的前置课程。6 g1 e+ ~* B. r9 s/ F8 q2 V' H

7 X2 Q; G2 m. _$ h2 N0 j代码展示
# Q( ~1 E' X6 G: ]( x& [* ^# z' A3 [% `7 `& m" Q$ S2 o
class Solution {
+ S- O! {8 ]7 X   public int minimumTime(int n, int[][] relations, int[] time) {) u5 H: g. [1 c: v' {
       List<List<Integer>> prev = new ArrayList<>();0 Q9 g( a* l$ @/ n
       for (int i = 0; i < n; i++) {
5 v9 x- l. D, a+ g/ w           prev.add(new ArrayList<>());
, ]" T1 [2 c+ _# r* K      }0 T8 U% S/ ?; N) P, ^+ W
       for (int[] rel : relations) {
+ n4 j1 o) E0 t; W4 W6 t           prev.get(rel[1] - 1).add(rel[0] - 1);
  _( P  u6 a8 K: d# b, T      }1 y, D% ~5 W7 u& U6 _; k
       int[] mem = new int[n];
; [5 r( A' D& O, C! }4 d/ N       int result = 0;; l7 P. ?! a- q
       for (int i = 0; i < n; i++) {
' h* ]% ~  ^/ `/ L1 ^" i- ^           result = Math.max(result, finish(i, prev, time, mem));9 R* j4 _0 p( g# `( A" Z7 |8 `# P6 V
      }
6 h7 V$ w8 g; J1 q. r4 m% Z+ T' `1 N       return result;
; k: U" A' M# a  }
$ P' N9 c: {! B/ N& l# q$ a% x" W! x' \' j5 |4 E6 r3 f6 V& i
   private int finish(int cur, List<List<Integer>> prev, int[] time, int[] mem) {! `" Z* e+ n) {8 @# o( \  \$ B
       if (mem[cur] > 0) {/ y# Z4 a( K0 y8 G2 l4 e
           return mem[cur];
) P% X! t/ s# n9 F      }
* n' B# a* V( w1 J4 p/ L       if (prev.get(cur).size() == 0) {# H! l8 O2 i# |9 A4 e" b) k
           return time[cur];$ Z' D9 z- k# y% j' b
      }1 t4 v5 _* R: F) J8 X( y
       for (int p : prev.get(cur)) {
: D; x. d9 }8 q           mem[cur] = Math.max(mem[cur], finish(p, prev, time, mem) + time[cur]);
% k, i  w% r1 E$ C* J4 P) e      }* B6 F" Y1 ^2 F
       return mem[cur];, e; o4 y/ @4 v* ^8 X' ?3 b
  }
. m* T( [- N; \, A$ h0 h1 z}
您需要登录后才可以回帖 登录 | 注册账号

本版积分规则

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