登录后可回复主题
您需要 登录 才可以下载或查看,没有帐号?注册账号
x
【 NO.1 统计特殊四元组】
' H9 H1 `6 Z# p4 L解题思路
- ^7 z) k8 L# P8 r" \签到题,枚举即可。 Y, @$ A1 w5 Q
c {& v7 T* ^1 ?$ o代码展示
- U9 e0 t w" B& f5 h# a1 K
+ u5 x$ E4 \7 g5 Y* h$ Nclass Solution {/ D& E5 }- Y5 k. h0 _2 X& H% b n
public int countQuadruplets(int[] nums) {6 M, ^% G+ c9 N) D; B0 U+ ]+ }
int n = nums.length;
! }6 z! \* u. ?" j" F1 p& A int res = 0;9 w$ m9 }4 A5 h$ w
for (int a = 0; a < n; a++) {
; B! U) Q' H6 s) i for (int b = a + 1; b < n; b++) {
1 t) C7 E5 M/ T$ [9 p7 V* R for (int c = b + 1; c < n; c++) {
: g- o. z1 x# {( M( i% ?, X for (int d = c + 1; d < n; d++) {/ A; M. y2 X; v) D' }/ l
if (nums[a] + nums[b] + nums[c] == nums[d]) {
$ ]5 V0 R# U* B; i$ |) S res++; F" {' b. N" e7 B
}
i+ j; r8 l8 ~; b6 t }& K4 Q5 B, Y2 o: F0 e
}! x' G3 ~: V6 Q
}- e6 d5 i6 J' Q2 D& C2 d& [
}4 K- U; w0 x& H: ]
return res;5 u/ l0 A. M- k4 y( X
}8 g7 ]' m4 H! \; k# b9 S1 x/ U1 ^
}
. u3 {& c! u U* }/ \2 l
1 c" `/ K! W. N3 A* X% z/ G& {! V9 x6 I8 D
$ M$ n% Y3 n! l5 z7 W ]
# z- l( N# }& C U5 Y: E【 NO.2 游戏中弱角色的数量】
( ] W$ `: h, {2 O6 L" ~9 ^4 v解题思路& r' ?0 s' g q8 f
按照攻击力、防御力从小到大排序,然后逆序统计即可。要注意处理攻击力相同的情况。
$ c- x1 F) J+ R4 \4 g+ Z. J, W3 B' x5 d4 P$ _6 F+ t( P0 N
代码展示
- r- { Y, u7 m8 h) h1 j5 m' Y! p0 A+ M
class Solution {* v2 n; Q4 ]; b+ G2 G
public int numberOfWeakCharacters(int[][] properties) {1 b' C/ [) h; ?9 [( t
Arrays.sort(properties, (a, b) -> {) x6 q+ _( t4 d3 n% S- T
if (a[0] == b[0]) {. G# [' k0 C2 X a
return a[1] - b[1];) L- F: {: n. J7 }4 M, d
}
3 V9 _1 ?- ^' B4 c: X; c% l) t return a[0] - b[0];
( y( s) L- f; Y, K" z2 M; I });
( q, m- `: h: C6 H/ g3 b$ @( H5 _ int res = 0;3 o! k; B+ Q. W4 I8 z
int lastAttack = properties[properties.length - 1][0];
& j$ G" k7 j2 M8 F/ m; p0 V4 ~! ? int lastDefense = properties[properties.length - 1][1];
, p- C; p0 B7 K+ N0 D: {* ~ int maxDefense = 0; // maxDefense 表示大于 lastAttack 的角色中,最大的防御力0 B& i3 q% `; H1 o5 h5 I
for (int i = properties.length - 2; i >= 0; i--) {; Y+ D2 Q( I3 O5 l4 j' q, x5 t" Y
if (properties[i][0] < lastAttack) {
( _- E' _8 c: I) t3 ~0 |0 G maxDefense = Math.max(maxDefense, lastDefense);
p7 V% B5 q+ K5 q- y* f' F8 g lastAttack = properties[i][0];0 e) s q C; d+ T# `5 G: @
lastDefense = properties[i][1];
! j: x( G/ A1 L1 i+ a% V }- K1 T ]8 H, b" J
if (properties[i][1] < maxDefense) {
8 M+ h* j# S9 r0 r res++;
- @6 q& ` {* A+ P8 F" ]/ u }, L. }/ e L) N* V; O
}
% ^7 j9 J3 i' U8 U; Y return res;* _3 \8 @8 s' J% w
}
0 Z( I) P+ h/ c$ j2 F}2 ]$ S( ]& P6 N
3 c8 t$ }6 g7 c% n! C+ Y# j
8 j) v4 C# Z6 J [2 O; T+ S* m8 j【 NO.3 访问完所有房间的第一天】
1 u- M; F! j( `- {6 [ r$ k
& r) ~9 n+ L2 ?解题思路
. {2 @! {! c; C& y, O8 {动态规划,dp[i] 表示访问完第 i 个房间的最小天数。- L- V5 L1 G" r7 ?* M/ F
% K @; u. s, J" ~+ g! b( ?" ^" Z代码展示
+ H' L) k1 W! s- p: E; ]/ n+ A) r' D: h$ v k
class Solution {
5 @' _# W: D* u \# h8 C- N* J public int firstDayBeenInAllRooms(int[] nextVisit) {
9 \" D& G; u1 r( X( u7 L4 t# X# L int n = nextVisit.length;
* K" L8 X& R- Y* O; p6 c$ L: O long[] dp = new long[n];
* h/ Z' M; G1 N5 G8 U$ Z long P = (long) (1e9 + 7);
6 e m n! i: y& O for (int i = 1; i < n; ++i) {% Q6 e. r! x9 X% H I8 D
dp[i] = (2 * dp[i - 1] - dp[nextVisit[i - 1]] + 2 + P) % P;
: y3 P: Z) Q3 W& U }
* b) O3 U2 M( U( d: w- h' I$ t return (int) dp[n - 1];3 u0 k" ^$ q. `7 ^
}1 O$ E* `4 r b7 B- N2 O$ ^
}9 r7 \) @) }+ w2 ]7 X) y9 A
5 S5 L$ Y" h0 I% J1 f
【 NO.4 数组的最大公因数排序】
: `/ ?( \* m3 _9 ~4 _, F$ z4 x
6 c. \& c. p2 S) R! v解题思路
( i9 [8 \* g1 G0 Y4 U0 L i只要元素之间有公因数,那么他们就可以任意排序。所以我们将有相同公因数的元素排序,最后再看序列整体是否有序即可。
3 h, L& W; V2 D9 k! F0 E4 a3 n3 p/ z3 u5 s' Y Z7 v% A. y
, T: R1 j6 q% ^! w3 i9 Y" Y' s/ R代码展示
7 p1 i) R, O: \# ~( W* o! S' J0 O( o h1 M
class UnionFind {, E7 a E' z1 @- }
public UnionFind(int size) {3 f, }* X2 m: @" x' G" ]3 `7 k5 u
f = new int[size];
# b& ?( d' v2 L X6 I+ [ Arrays.fill(f, -1);" I2 }- P' d: t2 o7 z
}) {$ _: o" j2 [6 v
0 {0 b5 d$ b; i0 @# m' u, `. g
public int find(int x) {8 {$ E% N2 ]$ q0 j# e# _& M
if (f[x] < 0)
* Y$ W% e& d' @% |( w return x;( J( _# N/ p! g7 Q% ?$ S) F6 Y. g
return f[x] = find(f[x]);3 E7 o2 j1 |8 `7 j+ ~3 y' j# u3 }
}, ^9 P* G5 @* P. M" f% L* r" O
4 J7 V$ f: E! @4 E3 \( I7 ] public boolean merge(int a, int b) {
2 o0 ]$ u+ U0 l/ T int fa = find(a);
- H' |* [" g5 n" y3 R; c int fb = find(b);2 U1 r1 A8 O) _( i4 L6 A
if (fa == fb)
& t4 T: p0 T k- A# y$ V6 | return false;: k1 M- ` w" M& V) E5 _
f[fa] = fb;1 A8 z0 ]. R1 C3 D8 d/ h( X
return true;
& |& j0 ]7 W/ P/ o1 E }& b% ~: a- M) v/ T
S, y* P4 M& ?. b$ L8 _
public Map<Integer, List<Integer>> sets() {% H4 I+ h* T) l
Map<Integer, List<Integer>> res = new HashMap<>();
1 P Q, S! ]% q; V# M1 [- Q4 O4 [ for (int i = 0; i < f.length; i++) {3 F+ P* b" f |
int fi = find(i);8 T% d- T d4 i& a9 z" X
if (!res.containsKey(fi)) {
. s) F( X2 n! k* ?3 N res.put(fi, new ArrayList<>());6 s) w& i1 d% ?/ B
}% h0 r9 i J/ v. f, D3 K
res.get(fi).add(i);8 a$ B9 e1 F" G0 ^2 Y7 F2 i1 o
}% a+ y0 a% X' d0 m) ]2 a9 b6 u+ k7 J
return res;$ o% S" e/ }5 @# o D) P$ {
}
; V+ G; o2 |0 S9 f5 w$ b0 t7 ?" [2 `0 h/ I4 D' t
private int[] f;
! I. x {: K" o# ~, L4 `5 a7 l- g}* `/ G! n8 z8 g, Y& A9 i2 F
* e8 z; D$ J6 u& l
$ e) _+ s+ j3 T6 T4 D# a4 g9 qclass Solution {
' ]) n$ l2 N& }. z public boolean gcdSort(int[] nums) {3 U9 n( c" M) Q& v" @8 d5 Y( P
Map<Integer, List<Integer>> set = new HashMap<>();# }$ a- U5 I) D g+ L, g
for (int i = 0; i < nums.length; i++) {
( V7 W) g% V7 ?8 l( e0 n% w for (int j = 1; j * j <= nums[i]; j++) {3 A; ?( _. B- n2 Q3 r
if (nums[i] % j == 0) {
/ ~* ?1 |4 d7 ^' d if (!set.containsKey(j)) {) i. y6 |- [( T5 G& a
set.put(j, new ArrayList<>());
% D7 Y ~# w: _6 d6 F5 ` }
7 H8 V: K/ b# v) D' l" A! k set.get(j).add(i);
4 ~) d9 w" R6 W% F; q5 ? if (j * j < nums[i]) {! s! r, S! l4 t9 D' _ c) I5 Q
int k = nums[i] / j;
) N0 l! s3 a7 O: W" l6 I if (!set.containsKey(k)) {+ ^/ o/ L9 z1 [( B
set.put(k, new ArrayList<>());6 o' L+ T; S" N8 @5 N
}! m" O) Q6 v' o6 X# ~ l* `
set.get(k).add(i);
! | u' v: w' S& d8 A }" ~" M+ q& E; C8 B
}
, o8 X/ w/ w2 N }+ ]& m x M- w: g, h6 l9 ~
}' j( ^- }! J, S3 Z
- L. u/ O! n. c, t; ` UnionFind uf = new UnionFind(nums.length);
$ y/ N- w' k# ~' h! } for (var e : set.entrySet()) {
! W$ w* R1 R9 s3 r if (e.getKey() < 2) {0 a, u9 @: g1 X9 b {, ?6 t
continue;2 W& ?6 T& i- B, J5 I
}( J9 K1 \: A, l; ~& i4 x- s
var list = e.getValue();) r" j6 U% R+ F. k( j
for (int i = 1; i < list.size(); i++) {4 o! |' I+ k# S4 p1 i7 ?6 J
uf.merge(list.get(i - 1), list.get(i));, }& g+ [1 N5 l0 f9 M; R0 Y4 g+ X
}1 m. ]. V6 j) `4 }6 J" X9 N5 O
}1 E: ]0 Y+ O8 F8 U# ? ~. p- o5 t0 X
var sets = uf.sets(); D; N' A( ?* @3 X5 L
int[] res = new int[nums.length];
1 q2 Z# ^0 b! ~/ i. K/ p for (var e : sets.entrySet()) {
$ w( x' R/ }6 {6 }7 K9 p' I: f# R var list = e.getValue();; b' ?) } E5 ~" _3 E
var sortedList = new ArrayList<>(list);
; Z: C' v$ i( F1 V' _3 W sortedList.sort(Comparator.comparingInt(a -> nums[a]));
) r: [9 f4 Q4 Q A' K9 d" ?1 C! W for (int i = 0; i < list.size(); i++) {- y+ j# p1 o* J; W4 Q$ l4 l
res[list.get(i)] = nums[sortedList.get(i)];
' Q# Y2 ]; i5 |" A# I: b3 ^! ]- V }
! s# U2 e0 k6 ]! z, Y/ z- S }: T0 c& F8 M8 ?1 j
for (int i = 1; i < res.length; i++) {
- Y; b; }" q I if (res[i] < res[i - 1]) {
# ^: n4 V9 R+ D3 @1 T0 G V return false;3 `/ L/ Z4 V5 `3 r
}! X$ l! x7 D# c( S) Q- C
}
4 b; a0 H$ f& r1 t3 L return true;
3 Y( W; ^% p" J* I6 e }
: U7 A& c/ U2 W8 V8 V7 {} |