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

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

上岸算法 回复:0 | 查看:2256 | 发表于 2021-11-14 17:02:13 |阅读模式 |复制链接

UWCSSA提醒您:

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

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

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

登录后可回复主题

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

x
【 NO.1 买票需要的时间】
$ `0 o- f- P, _/ D! ~
3 A1 b, p6 Y6 o) i9 c0 @  `解题思路
. ]% k/ s0 p( w, K7 y- d# D8 J签到题,使用一个 LinkedList 模拟即可。! T2 z1 D6 O/ D9 Q1 B
: S, J+ }9 f0 |5 {! m0 V
代码展示
' Y) K6 e' H1 x- f0 L3 U3 N6 g6 E" n$ J1 j) u( \- U
class Solution {& t8 [( G- K) m, y7 e
   public int timeRequiredToBuy(int[] tickets, int k) {
( ^$ `4 O+ \; c& \2 ?5 V8 k       LinkedList<Integer> queue = new LinkedList<>();
" V; u5 e1 t# f# P: X       for (int i : tickets) {5 V' N. A$ O- d& J+ ]. P
           queue.add(i);
+ `' @8 ~# ^9 @9 A" a2 B+ z" [( y( y      }' y/ E1 ]: F- X, e( P
       int result = 0;
$ T" T/ p* i! @- F       while (!queue.isEmpty()) {
0 ~7 Q4 e# P% Z( |* m           result++;) y4 J7 j0 e+ f
           int t = queue.pollFirst() - 1;
; b  `3 Z/ K# T+ ]           if (t == 0 && k == 0) {2 Q; \+ a, K0 \! t4 y! o
               break;3 M. X! N( D1 k, ]0 F7 x+ O
          }) i) H9 ]5 S. O+ l  D7 g! b# a, p4 ]/ ]
           if (t > 0) {
0 X2 J* p: T: o8 R0 L& C. W               queue.addLast(t);; [& o, w2 w" A) y
          }
* r7 w6 _) p# g3 B! J  o1 [           k = (k - 1 + queue.size()) % queue.size();
0 ~  j) V7 _, D9 y      }
7 a% w* |; A, r2 |7 X( [; t       return result;
: R. N5 [- N9 U8 n+ b! ?  }; B* O3 h" z0 j  X( `( h
}
& u1 [7 @9 s, N3 s* }; G
+ i' ]% {6 n( W  G( K, D5 w  y【 NO.2 反转偶数长度组的节点】
/ y% Y, w1 Q) {/ a+ r& O- f; E2 Z
" f! Y7 K  M5 w2 y, D: ?解题思路5 Z* b: `$ I( \- X0 E5 y
数组更有利于反转操作,可以先将链表转换成数组,操作完后再转换成链表。
, s) o6 {/ b) E' J% X# ^# a& d+ b8 C4 f/ g* t+ I; L8 s4 \' y
代码展示
- W" L# m, t' r! n/ ~* \
0 A2 q. B3 `( E. c7 @7 n* Wclass Solution {5 u2 w8 e3 D* ], p; E0 S; A+ ]. A! _5 _
   public ListNode reverseEvenLengthGroups(ListNode head) {
# t6 A3 ^- Q6 M! l- Q& B& F5 M       List<Integer> list = new ArrayList<>();
5 D! E( x$ w/ \% w( e       for (ListNode i = head; i != null; i = i.next) {8 z# h+ ]' P9 u
           list.add(i.val);
) }0 b) }! |/ W1 a( K) s      }  I+ n9 n' d0 x5 U7 ^; b4 Y8 {
       for (int start = 1, len = 2; start < list.size(); ) {) I1 E, D" w8 o
           int end = Math.min(list.size(), start + len);
) G) v" M' u5 D2 ]% L           if ((end - start) % 2 == 0) {
$ R) N) o& m; T* [- r0 h) @               // reverse [start, end)4 U3 G. ^2 b" j  M2 s0 _8 [, V  Z' m
               for (int l = start, r = end - 1; l < r; ) {! R/ W; k! [7 A% m
                   int t = list.get(l);) m6 h' P: J  w5 O
                   list.set(l, list.get(r));
$ p7 y' t/ ]3 D, \# S! _# H: E                   list.set(r, t);
* y9 C% E$ h/ B; Z                   l++;
2 Z) @; q$ x& ~                   r--;8 p; \# ?% O- _8 m0 R
              }0 ~  A1 J9 J+ Z
          }8 k" h6 y$ {" t) r% c4 C
           start += len;
( P! p" y) ~0 W4 @, {9 `           len++;6 z! z- R+ [# }' Q/ U0 \1 R
      }$ r) J- s' Q$ K+ h) ]- Y7 L
       ListNode h = new ListNode(list.get(0));
( T+ Z- I# q( B) t" Q' w* L4 n       ListNode cur = h;
+ {! o  ]/ [( C3 S, B% U9 n$ D3 G4 p       for (int i = 1; i < list.size(); i++) {
8 }  t9 S; j; K( w           cur.next = new ListNode(list.get(i));+ z6 e6 D2 U; @0 b1 U
           cur = cur.next;
, G- n% s+ w$ n      }1 L- d% R* C$ }7 @( i" ]  Z
       return h;3 d' Q1 S6 ]# k  _: `
  }. Y, f+ b. v2 H
}
! ^; k) n+ R, @7 d1 J
9 h  U1 Q1 m+ h) E# \  Y【 NO.3 解码斜向换位密码】
( v9 W" v* M9 C8 T
) u3 s/ y5 |( a9 M" f! _解题思路
8 w' d3 Y/ Q. a$ W8 |+ G4 v还原出矩阵即可,最后注意将后缀空格删去。  N- s. e( l' x2 ^& o& T0 P
: w) F, R  `# j: t
代码展示6 V6 K! _1 [9 [3 }& ^
5 Z, D# ]0 J/ |/ Q6 H
class Solution {  m; H  [, G" ]( F/ V# H
   public String decodeCiphertext(String encodedText, int rows) {
8 c$ S: L5 o- v       int cols = encodedText.length() / rows;
3 Z* ^% o8 b& [6 n       char[][] mtx = new char[rows][cols];
5 y3 E0 q# H2 F6 _' l- T       for (int i = 0; i < encodedText.length(); i++) {
/ t( t+ G( E3 B9 ?           int x = i / cols;
3 c5 `( l. |7 [( T+ n4 w2 Q           int y = i % cols;
. W2 t1 L* K% N) {  f           mtx[x][y] = encodedText.charAt(i);
2 u4 z4 M, a5 |) E# D      }) v5 z% Q( T2 `
       StringBuilder sb = new StringBuilder();7 q: h/ v9 \, E' e% u' H
       for (int startY = 0; startY < cols; startY++) {
$ O! C# G' v1 V0 ?$ h: r           for (int x = 0, y = startY; x < rows && y < cols; x++, y++) {
. C+ v4 k' n+ W               sb.append(mtx[x][y]);: F; T- i' X1 F- i! K" `# b
          }
( v5 n. w( q: i/ L, s. T% {% o      }
: o; N# d' O: R# d6 }6 P8 n       while (sb.length() > 0 && sb.charAt(sb.length() - 1) == ' ') {
+ G3 r# X5 w5 f* V. Y8 d3 X           sb.deleteCharAt(sb.length() - 1);
7 L8 `8 r; u/ S8 I* g7 \6 t      }
" I' ?' B: S8 f0 n0 _       return sb.toString();- k1 F- @2 J$ V4 u) m/ Q2 o
  }+ i/ G; g( e- u- k
}
- [$ w9 H8 {, P" Z4 Y# Q& q3 v: e$ X  i9 t

& R" ~9 K/ h0 f+ H( c【 NO.4 处理含限制条件的好友请求】
0 l. P( Y% H/ A7 v- w/ C
3 }( \, J6 K  w+ M解题思路
: x  a4 X- @0 e2 i: I( s  N并查集,详见代码注释。
; q, W$ B, _9 F6 E
: L- F$ _: l! ?+ m1 O. L代码展示' B$ P/ x" q0 {4 L; \! \; o

( h, ^& ^, e" |' S9 Pclass Solution {4 O+ F9 n4 w* O* _5 l
   public boolean[] friendRequests(int n, int[][] restrictions, int[][] requests) {
& I! {; A4 S" w$ d* R. e9 M       boolean[] result = new boolean[requests.length];
% F3 P- \; e/ y( n( ~4 k2 J       // R[i] 表示不能与 i 做朋友的人4 u  w# _. J( x0 A6 ~4 P' a$ E
       List<Set<Integer>> R = new ArrayList<>();8 s" y0 A/ Z, i6 ~# D
       for (int i = 0; i < n; i++) {; K/ [; a3 d: E% e
           R.add(new HashSet<>());" `! W" Y( _1 R6 p
      }
3 g3 D4 _3 H, C& P       for (var r : restrictions) {
/ d0 B9 C! Q! d7 N, t0 K- x           R.get(r[0]).add(r[1]);
0 l" j+ ?" A, c& R           R.get(r[1]).add(r[0]);. v$ S# r- P3 Y; |
      }* j: R2 H, P+ L. I
       // uf 维护间接朋友关系
. R6 k4 T7 N5 r# S$ `- L       UnionFind uf = new UnionFind(n);- r. g8 \5 Z" [8 b- B
       for (int i = 0; i < requests.length; i++) {( M) `  e4 S  T" r6 \
           var r = requests[i];; Y4 I% P; l. P% k/ O
           int f0 = uf.find(r[0]);
) R1 E* _& Q) P. B# w- U( e' w           int f1 = uf.find(r[1]);, }. o; u! j& z# T+ _
           if (f0 == f1) { // 说明 r[0] 和 r[1] 已经是直接或间接朋友了
2 ~4 ~) L- [$ w# d% G" l( R( G               result[i] = true;
$ d* l* o) x1 l3 ?- B% G2 A+ C               continue;8 I8 i7 H: a- w( ]1 \: j4 C9 Y
          }
2 |8 W* G  C+ Z2 f4 R, q. Y1 E           // 由于我们总是将限制关系合并到根,所以只需判断根节点即可' I' ^  ^7 s8 }1 O7 ^( O
           result[i] = !R.get(f0).contains(f1) && !R.get(f1).contains(f0);+ r% _7 `- ~/ n8 D
           if (result[i]) {+ n; K1 b6 z, W( T1 e
               uf.merge(f0, f1); // merge 后 f1 将成为 根
: T' J0 p) T% U) l  W( E2 {' b               // 原本不能与 f0 做朋友的人也不能与 f1 做朋友了/ l$ m. I5 ]* V7 ~
               R.get(f1).addAll(R.get(f0));2 `4 s/ y. w; H6 h
               for (int j : R.get(f1)) {
5 e0 T+ [4 P' O3 X                   R.get(j).add(f1);
/ ~5 F! l8 Q3 g0 n7 c              }4 ^4 }# i/ H) f% \( j4 H& \
          }
3 z$ a! ]" c2 t0 h      }* P9 u9 g2 \6 j3 c: u3 h  n1 f8 Y. J
       return result;
: D- ^  g' x; `/ r: D% j, T! ?  }
: P7 I- B2 a  W  r}* Z1 l/ Y  X3 q, b7 F+ D

7 _6 o% }% {% r4 @) cclass UnionFind {8 w1 R  s* b% z. x2 ]5 v  J
   public UnionFind(int size) {; E8 `0 `* k7 s$ t" ^( l
       f = new int[size];. A9 j$ N) c+ k) a- Q) j
       Arrays.fill(f, -1);
8 ~. H% j- I( s  }
5 s6 Z% q- a4 e: J. s5 l- u: v
, h2 p& c$ U9 M! H# x# U3 D2 Q   public int find(int x) {6 ?% N3 o7 s# o- H1 M6 o
       if (f[x] < 0)
6 Y1 R* n' A1 @6 E4 x" o" x& F           return x;- M1 t) _  q8 P) ^6 o
       return f[x] = find(f[x]);
3 ?* ^- H# H8 V: U1 i8 M' y  }$ w& h: d- P' `- _/ ?
" L6 O8 W( h( z" ~' |7 G( P* L
   public boolean merge(int a, int b) {
; X0 Z& s. @' N       int fa = find(a);
4 {' A# f  J0 ]/ T       int fb = find(b);
# d# ^3 z$ P1 F5 R- c6 J7 J       if (fa == fb)# f, @3 t1 @( g- {7 L5 ~
           return false;& B8 a7 a% f( g/ H: m' W. L) ^. k
       f[fa] = fb;# F# Y5 g7 O9 I0 w4 R1 E
       return true;
  I8 n3 `0 y! M& n2 W/ z  }3 V6 A9 J( }+ h2 Y( Q# T$ ?
0 k7 Z( g: w" D0 \* T5 |4 q
   public Map<Integer, List<Integer>> sets() {/ F# `0 ?9 B$ S& @0 w; c
       Map<Integer, List<Integer>> res = new HashMap<>();
# n/ L' m" L, G; b       for (int i = 0; i < f.length; i++) {
. s  u9 T% \8 p# O+ Z7 _  l) l           int fi = find(i);# u8 ?" z$ i/ G' W7 s% g1 O
           if (!res.containsKey(fi)) {
8 A% d+ G/ X5 V& M, {4 N               res.put(fi, new ArrayList<>());6 I7 N# l  L8 n. A5 N
          }
! ^1 n" W! E7 u: T) {6 \6 A6 R           res.get(fi).add(i);$ i# I* M6 y, |4 x3 _
      }1 O( W- X) j& V
       return res;& m8 Q% k. P, K* W
  }
. U+ f, @/ @# C! X. w) |2 X' ]
( v* W! T7 ?- i9 U   private int[] f;
5 \' P- @0 b$ w6 t9 v; N3 P. {9 j}
2 U- i0 @3 @; E9 K% r
您需要登录后才可以回帖 登录 | 注册账号

本版积分规则

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