登录后可回复主题
您需要 登录 才可以下载或查看,没有帐号?注册账号
x
【 NO.1 统计字符串中的元音子字符串】
6 V3 H! z2 L. e; q解题思路& ?( Q& [6 Z/ F2 n% E) D3 b
签到题。7 x1 D1 L8 k; U1 v
3 A; y* c! \/ K+ a2 F代码展示' O3 k0 h: t# O O# [. C/ G
/ m2 @7 L/ w' u% D$ H$ iclass Solution {5 r+ ~/ T- ?" }, R; s, E
public int countVowelSubstrings(String word) {
! R! x6 f8 m9 w* D7 F int count = 0;/ c1 ^ E7 Q( b$ w( b$ H; j
for (int i = 0; i < word.length(); i++) {" X8 O' C) R- W: X" S
for (int j = i + 1; j <= word.length(); j++) {+ V* t, r- Z' O1 p# e
count += containsAll(word.substring(i, j));
% A7 G1 E! n% |- P: S K* v: O }
5 d3 \/ n2 }: D/ h5 ~ }2 F; x" w P7 G% `
return count;' C5 G' r% ? J- Y- L
}' a; u+ F I- A. h! d- n" w
% C3 `, t3 T6 o; Y1 ]# H" J
private int containsAll(String s) {
* j. {: u8 p# b# W. p' ? if (s.contains("a") && s.contains("e") && s.contains("i") && s.contains("o") && s.contains("u")) {
1 \. q9 S _' R- J3 X for (var c : s.toCharArray()) {
3 t* U0 S7 S' e0 a4 K if (!"aeiou".contains(String.valueOf(c))) {$ k8 l) }. n* M6 v E( p; [# T
return 0;
5 T2 ~ K* f! W3 G9 u, b% f$ a }; j8 @) j: E$ P( ]! g
}
8 F' u+ `# _% R: J9 G! i return 1;
& D: z( z: Z4 g2 @+ G6 L }6 Q7 ]0 e! R4 f' h
return 0;
/ W2 {( i T8 a, {6 W4 p }
; t5 R& ~! l: e9 k9 i& L}
) h- P( _. U) z" \* ^2 i4 E" y# q! _! U5 f
【 NO.2 所有子字符串中的元音】
4 `. V8 R+ E1 s# m5 P' G解题思路# A( z9 t1 l1 V3 Y u" @2 X9 d
依次计算每个位置的元音字符会被多少个子串计数即可。
$ A( p+ Y+ O% a; c# T3 l+ K( j
9 o w( @4 p% Z* F+ P代码展示
/ C$ C. p3 ]1 \# S# a. U' P8 g# `. G4 e& K3 ?* A
class Solution {' e+ E, _" v b0 ?/ x; s! D
public long countVowels(String word) {1 x- [' Y1 w$ l0 C! N
long result = 0;. f5 {. y/ K' P3 a W( h( F
for (int i = 0; i < word.length(); i++) {+ I) k" B. L$ }& l {
if (!"aeiou".contains(String.valueOf(word.charAt(i)))) {4 H+ K- I* b4 p8 N3 V G& R: t# X
continue;
; L) ^& N* b$ _4 t* M m1 [ }. C1 I+ L3 W, A2 Y; z( ~2 ^
long left = i;& W) o7 y! u+ R# ?8 [ t/ v
long right = word.length() - i - 1;
2 \$ w0 |0 J! s9 S7 a: A, p result += left * right + left + right + 1;# n* d" r% B+ v+ \2 k
}
4 n0 s" M& M8 G return result;
0 E- k) s. |! a2 i7 l$ P }# x! D" c8 U. l G* ^7 }8 |2 q, c
}8 ?, W/ H4 l- [5 V3 T
/ R% g1 A1 M( a2 G7 H2 F! Q【 NO.3 分配给商店的最多商品的最小值】
, t9 M/ q& F1 s$ j6 _9 K7 i; M解题思路
# K- l, e4 d2 h4 }* E2 D" V二分答案,假定一个商店最多能分配 x 个商品,那么我们可以轻易计算出需要多少个商店,即可得到 n 个商店能否分配完这 m 种商品。
, Z! d- k+ I: o7 n) ^( `" }4 B$ K
代码展示" I+ q& b% T Z. S5 a
5 w! `/ U+ E a8 m
class Solution {
6 v' S! y R L public int minimizedMaximum(int n, int[] quantities) {
/ C$ P4 ~' ?+ M5 i$ A int left = 1;& o, p5 k* d/ T; L
int right = Arrays.stream(quantities).max().getAsInt();8 Q7 A. r9 L8 D6 O- W
while (left + 1 < right) {) E' r' [- T3 D1 K: H1 ]
int mid = (left + right) / 2;& m! y4 C: [' W3 B- d( [: m2 G) l/ k
if (check(n, quantities, mid)) {
% B# A, u* [$ B6 C% }5 x, T# [, c right = mid;) Y( E, f1 f$ C! `$ ?& g: Y
} else {
. m' u0 I6 d* c( P5 i! M left = mid;1 y) i" V% m: J* \$ ]
}
' W1 B9 i, q+ Z9 s }
8 H! ^$ Z4 S! G& ] return check(n, quantities, left) ? left : right;/ r* _8 m% b7 }% @& c, _
}
% l& \7 U$ B( W8 k" q0 V9 F0 Y1 U0 r5 J, _
private boolean check(int n, int[] quantities, int x) {
# H8 o4 a% k, B% f1 t4 P7 e int cnt = 0;& [2 c$ D J% y3 A+ e1 q
for (int q : quantities) {: Q! ?4 S, g" I8 F/ P& x8 w
cnt += (q + x - 1) / x;
3 R( T7 p3 a+ M8 m) e8 O: U) O }/ H7 L7 u6 ~2 E# M# R" w7 q
return cnt <= n;1 Q' m2 U, f1 H6 i
}
1 G5 M& D4 }; S. R}, ?' j7 k( B- }8 ?' S7 n
; @! W+ g$ L8 U. [. J9 [" }
【 NO.4 最大化一张图中的路径价值】
+ @- n. \2 G. `( d解题思路
& F. E( g( k" I0 I5 p2 @看似复杂,但是观察数据范围,发现直接回溯即可。* a. j: J7 |) L
) I1 g% K4 F: E, _; {7 h7 l
代码展示- m# W& ]( ?. ]. p) j% J/ L6 q
- [0 U: q, x7 {3 F- P
class Solution {$ B; _0 s3 A1 w1 d8 a) h+ K; K
int result;
& e* V& P3 f1 U5 \6 d List<Integer> empty = new ArrayList<>();& j0 l/ B2 A4 B) k( Z$ Q
2 Q4 u' b F5 ?2 O, |! _" o
public int maximalPathQuality(int[] values, int[][] edges, int maxTime) {
8 y' t5 C" I% L. b- y' H! _! n Map<Integer, List<Integer>> children = new HashMap<>();
* v5 Y' O" J7 N* w; P, R Map<Integer, Map<Integer, Integer>> times = new HashMap<>();! C+ b* G$ \9 f: ~( V2 r6 ]
for (int[] e : edges) {
: _4 G7 t9 F+ y4 @: j: K4 y if (!children.containsKey(e[0])) {) Q% B! H. `- c& j1 d
children.put(e[0], new ArrayList<>());
! Z- ~3 w. r o+ e0 Z5 z' C5 V* f }+ \' a6 R3 z1 ?3 @; Q
if (!children.containsKey(e[1])) {
! q9 l4 m/ f/ `' y6 B children.put(e[1], new ArrayList<>());6 Q+ ^4 @- \8 ]! `: b4 T! h
}5 D- @' u' i5 \- I0 |5 V: }
if (!times.containsKey(e[0])) {
$ O8 D& o2 g& Q times.put(e[0], new HashMap<>());
" c" G' ] T8 u+ v; P) K L }
; W* R. \8 }3 \' H/ h if (!times.containsKey(e[1])) {4 ]9 {, N& e0 n1 L: f5 O0 J" I# v4 {9 s
times.put(e[1], new HashMap<>());
9 A- D8 u* g0 M! U: f" ]2 ? }5 y) D8 a; x" ^+ F! l+ X& u
children.get(e[0]).add(e[1]);
2 J0 U0 R$ r' H* D# a9 \9 x% [1 h children.get(e[1]).add(e[0]);9 q* x F% n9 a# U8 \1 l" Y
times.get(e[0]).put(e[1], e[2]);
4 y7 c" H; x4 T4 R3 t# p, b: U! d times.get(e[1]).put(e[0], e[2]);5 b" c6 G' ], Z8 t7 ?* J
}# [* q) H4 Q8 I' i
int[] vis = new int[values.length]; i+ G& B4 q! p' S! M
result = 0;
% N6 f i+ B: Q6 D) b dfs(0, 0, 0, maxTime, vis, values, children, times);# G9 P: T+ |2 m3 a9 w9 i' s3 x
return result;
% ~* e8 ^9 \, D' t. @ }
2 L0 L0 e* W: }# x, W" [* @% {( h# Q. C* [1 t+ k
private void dfs(int pos, int sum, int time, int maxTime, int[] vis, int[] values, Map<Integer, List<Integer>> children, Map<Integer, Map<Integer, Integer>> times) {
8 Q. o/ [! e& V' N- ~ if (vis[pos] == 0) {2 V3 A$ m! M& X9 b" }. f
sum += values[pos];) r( a g3 P8 T& n
}! Q) W9 }, ^/ u: w' j* R
vis[pos]++;
0 C( C: c+ U4 C3 B6 b if (pos == 0) {8 z: x- H8 q9 e+ Y
result = Math.max(result, sum);
- f/ Z2 Z! I' {- g: _ I }/ w, l4 Z9 ^& s
for (int nxt : children.getOrDefault(pos, empty)) {" C+ i/ K/ P! m, {$ d7 j# w& \
if (time + times.get(pos).get(nxt) <= maxTime) {& t& [! | U" p. V0 V- e8 j
dfs(nxt, sum, time + times.get(pos).get(nxt), maxTime, vis, values, children, times);
3 m. C" Y7 t3 K }
- Z( T; P8 k0 w. V B }( ]. y% q7 t) H9 x- H- B
vis[pos]--;* g& W7 Y8 V9 S% w1 [4 d# P
}8 y) W U1 `, ^$ U
}
8 T& D1 f. _6 h4 Q Z |