Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commita84952e

Browse files
authored
Improved task 2029.
1 parent791c38e commita84952e

File tree

2 files changed

+22
-68
lines changed

2 files changed

+22
-68
lines changed

‎README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1708,7 +1708,7 @@ implementation 'com.github.javadev:leetcode-in-java:1.10'
17081708
| 2033 |[Minimum Operations to Make a Uni-Value Grid](src.save/main/java/g2001_2100/s2033_minimum_operations_to_make_a_uni_value_grid/Solution.java)| Medium | Array, Math, Sorting, Matrix | 41 | 87.53
17091709
| 2032 |[Two Out of Three](src.save/main/java/g2001_2100/s2032_two_out_of_three/Solution.java)| Easy | Array, Hash_Table | 9 | 45.56
17101710
| 2030 |[Smallest K-Length Subsequence With Occurrences of a Letter](src.save/main/java/g2001_2100/s2030_smallest_k_length_subsequence_with_occurrences_of_a_letter/Solution.java)| Hard | String, Greedy, Stack, Monotonic_Stack | 131 | 64.46
1711-
| 2029 |[Stone Game IX](src.save/main/java/g2001_2100/s2029_stone_game_ix/Solution.java)| Medium | Array, Math, Greedy, Counting, Game_Theory |31 |10.87
1711+
| 2029 |[Stone Game IX](src.save/main/java/g2001_2100/s2029_stone_game_ix/Solution.java)| Medium | Array, Math, Greedy, Counting, Game_Theory |14 |53.97
17121712
| 2028 |[Find Missing Observations](src.save/main/java/g2001_2100/s2028_find_missing_observations/Solution.java)| Medium | Array, Math, Simulation | 10 | 31.40
17131713
| 2027 |[Minimum Moves to Convert String](src.save/main/java/g2001_2100/s2027_minimum_moves_to_convert_string/Solution.java)| Easy | String, Greedy | 0 | 100.00
17141714
| 2025 |[Maximum Number of Ways to Partition an Array](src.save/main/java/g2001_2100/s2025_maximum_number_of_ways_to_partition_an_array/Solution.java)| Hard | Array, Hash_Table, Prefix_Sum, Counting, Enumeration | 172 | 100.00

‎src.save/main/java/g2001_2100/s2029_stone_game_ix/Solution.java

Lines changed: 21 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -1,84 +1,38 @@
11
packageg2001_2100.s2029_stone_game_ix;
22

33
// #Medium #Array #Math #Greedy #Counting #Game_Theory
4-
// #2022_05_25_Time_31_ms_(10.87%)_Space_114.5_MB_(65.22%)
4+
// #2022_06_21_Time_14_ms_(53.97%)_Space_126.8_MB_(25.40%)
55

66
publicclassSolution {
7-
privateint[]stones;
8-
97
publicbooleanstoneGameIX(int[]stones) {
10-
this.stones =stones;
11-
int[]freq =newint[3];
8+
intzero =0;
9+
intone =0;
10+
inttwo =0;
1211
for (inti :stones) {
1312
if (i %3 ==0) {
14-
freq[0]++;
13+
zero++;
1514
}elseif (i %3 ==1) {
16-
freq[1]++;
17-
}else {
18-
freq[2]++;
15+
one++;
16+
}elseif (i %3 ==2){
17+
two++;
1918
}
2019
}
21-
booleanb1 =false;
22-
booleanb2 =false;
23-
int[]a =freq.clone();
24-
int[]b =freq.clone();
25-
if (a[1] >0) {
26-
a[1]--;
27-
b1 =fun(a,1);
20+
if (one ==0 &&two ==0) {
21+
returnfalse;
2822
}
29-
if (b[2] >0) {
30-
b[2]--;
31-
b2 =fun(b,2);
23+
intmax =Math.max(one,two);
24+
intmin =Math.min(one,two);
25+
if (zero %2 ==0) {
26+
if (min ==0) {
27+
returnfalse;
28+
}
29+
returntrue;
3230
}
33-
returnb1 ||b2;
34-
}
35-
36-
privatebooleanfun(int[]freq,intsum) {
37-
intn =stones.length;
38-
inti =1;
39-
while (i <n) {
40-
if (i %2 ==0) {
41-
if (sum %3 ==1) {
42-
if (freq[0] >0) {
43-
freq[0]--;
44-
}elseif (freq[1] >0) {
45-
freq[1]--;
46-
sum +=1;
47-
}else {
48-
returnfalse;
49-
}
50-
}elseif (sum %3 ==2) {
51-
if (freq[0] >0) {
52-
freq[0]--;
53-
}elseif (freq[2] >0) {
54-
freq[2]--;
55-
sum +=2;
56-
}else {
57-
returnfalse;
58-
}
59-
}
60-
}else {
61-
if (sum %3 ==2) {
62-
if (freq[0] >0) {
63-
freq[0]--;
64-
}elseif (freq[2] >0) {
65-
freq[2]--;
66-
sum +=2;
67-
}else {
68-
returntrue;
69-
}
70-
}elseif (sum %3 ==1) {
71-
if (freq[0] >0) {
72-
freq[0]--;
73-
}elseif (freq[1] >0) {
74-
freq[1]--;
75-
sum +=1;
76-
}else {
77-
returntrue;
78-
}
79-
}
31+
if (zero %2 ==1) {
32+
if (max -2 >min) {
33+
returntrue;
8034
}
81-
i++;
35+
returnfalse;
8236
}
8337
returnfalse;
8438
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp