Skip to content

Commit 5cec1b4

Browse files
committed
Improved tasks 1815, 3457, 3459
1 parent c31918d commit 5cec1b4

File tree

4 files changed

+190
-102
lines changed
  • src/main/java
    • g1801_1900/s1815_maximum_number_of_groups_getting_fresh_donuts
    • g3401_3500

4 files changed

+190
-102
lines changed

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2088,9 +2088,9 @@
20882088

20892089
| # | Title | Difficulty | Tag | Time, ms | Time, %
20902090
|------|----------------|-------------|-------------|----------|--------
2091-
| 3459 |[Length of Longest V-Shaped Diagonal Segment](src/main/java/g3401_3500/s3459_length_of_longest_v_shaped_diagonal_segment)| Hard | Array, Dynamic_Programming, Matrix, Memoization | 461 | 36.09
2091+
| 3459 |[Length of Longest V-Shaped Diagonal Segment](src/main/java/g3401_3500/s3459_length_of_longest_v_shaped_diagonal_segment)| Hard | Array, Dynamic_Programming, Matrix, Memoization | 56 | 72.97
20922092
| 3458 |[Select K Disjoint Special Substrings](src/main/java/g3401_3500/s3458_select_k_disjoint_special_substrings)| Medium | String, Hash_Table, Dynamic_Programming, Sorting, Greedy | 7 | 95.31
2093-
| 3457 |[Eat Pizzas!](src/main/java/g3401_3500/s3457_eat_pizzas)| Medium | Array, Sorting, Greedy | 63 | 40.14
2093+
| 3457 |[Eat Pizzas!](src/main/java/g3401_3500/s3457_eat_pizzas)| Medium | Array, Sorting, Greedy | 16 | 100.00
20942094
| 3456 |[Find Special Substring of Length K](src/main/java/g3401_3500/s3456_find_special_substring_of_length_k)| Easy | String | 0 | 100.00
20952095
| 3455 |[Shortest Matching Substring](src/main/java/g3401_3500/s3455_shortest_matching_substring)| Hard | String, Binary_Search, Two_Pointers, String_Matching | 116 | 81.44
20962096
| 3454 |[Separate Squares II](src/main/java/g3401_3500/s3454_separate_squares_ii)| Hard | Array, Binary_Search, Segment_Tree, Line_Sweep | 156 | 80.18
@@ -3373,7 +3373,7 @@
33733373
| 1818 |[Minimum Absolute Sum Difference](src/main/java/g1801_1900/s1818_minimum_absolute_sum_difference)| Medium | Array, Sorting, Binary_Search, Ordered_Set, Binary_Search_II_Day_7 | 13 | 99.44
33743374
| 1817 |[Finding the Users Active Minutes](src/main/java/g1801_1900/s1817_finding_the_users_active_minutes)| Medium | Array, Hash_Table | 16 | 91.64
33753375
| 1816 |[Truncate Sentence](src/main/java/g1801_1900/s1816_truncate_sentence)| Easy | Array, String | 2 | 65.71
3376-
| 1815 |[Maximum Number of Groups Getting Fresh Donuts](src/main/java/g1801_1900/s1815_maximum_number_of_groups_getting_fresh_donuts)| Hard | Array, Dynamic_Programming, Bit_Manipulation, Bitmask, Memoization | 7 | 86.67
3376+
| 1815 |[Maximum Number of Groups Getting Fresh Donuts](src/main/java/g1801_1900/s1815_maximum_number_of_groups_getting_fresh_donuts)| Hard | Array, Dynamic_Programming, Bit_Manipulation, Bitmask, Memoization | 2 | 100.00
33773377
| 1814 |[Count Nice Pairs in an Array](src/main/java/g1801_1900/s1814_count_nice_pairs_in_an_array)| Medium | Array, Hash_Table, Math, Counting | 47 | 83.12
33783378
| 1813 |[Sentence Similarity III](src/main/java/g1801_1900/s1813_sentence_similarity_iii)| Medium | Array, String, Two_Pointers | 3 | 41.38
33793379
| 1812 |[Determine Color of a Chessboard Square](src/main/java/g1801_1900/s1812_determine_color_of_a_chessboard_square)| Easy | String, Math | 1 | 49.36

src/main/java/g1801_1900/s1815_maximum_number_of_groups_getting_fresh_donuts/readme.md

Lines changed: 68 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -34,59 +34,88 @@ You can freely rearrange the ordering of the groups. Return _the **maximum** pos
3434
## Solution
3535

3636
```java
37-
import java.util.Arrays;
3837
import java.util.HashMap;
3938
import java.util.Map;
4039

4140
public class Solution {
42-
private static final Map<String, Integer> MAP = new HashMap<>();
43-
4441
public int maxHappyGroups(int batchSize, int[] groups) {
45-
int[] count = new int[batchSize];
46-
int res = 0;
47-
int remGroup = 0;
48-
for (int group : groups) {
49-
int g = group % batchSize;
50-
if (g == 0) {
51-
res++;
52-
} else if (count[batchSize - g] > 0) {
53-
remGroup--;
54-
res++;
55-
count[batchSize - g]--;
56-
} else {
57-
count[g]++;
58-
remGroup++;
59-
}
42+
if (batchSize == 1) {
43+
return groups.length;
44+
}
45+
int[] withSize = new int[batchSize];
46+
for (int size : groups) {
47+
withSize[size % batchSize]++;
48+
}
49+
int fromZero = withSize[0];
50+
withSize[0] = 0;
51+
int fromEnds = 0;
52+
for (int l = 1, r = batchSize - 1; l < r; l++, r--) {
53+
int usable = Math.min(withSize[l], withSize[r]);
54+
fromEnds += usable;
55+
withSize[l] -= usable;
56+
withSize[r] -= usable;
6057
}
61-
res += dfs(0, remGroup, count, batchSize);
62-
return res;
58+
int fromMid = 0;
59+
if (batchSize % 2 == 0) {
60+
fromMid = withSize[batchSize / 2] / 2;
61+
withSize[batchSize / 2] -= fromMid * 2;
62+
}
63+
return get(pruneEnd(withSize), batchSize, 0, new HashMap<>())
64+
+ fromZero
65+
+ fromEnds
66+
+ fromMid;
6367
}
6468

65-
private int dfs(int curr, int remain, int[] count, int batch) {
66-
if (remain == 0) {
67-
return 0;
69+
private int get(int[] ar, int batchSize, int rem, Map<Long, Integer> cache) {
70+
long hash = 0;
71+
for (int e : ar) {
72+
hash = hash * 69L + e;
6873
}
69-
int res = 0;
70-
String s = Arrays.toString(count);
71-
if (MAP.containsKey(s)) {
72-
return MAP.get(s);
74+
Integer fromCache = cache.get(hash);
75+
if (fromCache != null) {
76+
return fromCache;
7377
}
74-
if (curr == 0) {
75-
res++;
76-
curr = batch;
78+
if (zeroed(ar)) {
79+
cache.put(hash, 0);
80+
return 0;
7781
}
78-
int val = 0;
79-
for (int i = 1; i < count.length; i++) {
80-
if (count[i] == 0) {
82+
int max = 0;
83+
for (int i = 0; i < ar.length; i++) {
84+
if (ar[i] == 0) {
8185
continue;
8286
}
83-
count[i]--;
84-
val = Math.max(val, dfs((curr - i + batch) % batch, remain - 1, count, batch));
85-
count[i]++;
87+
ar[i]--;
88+
int from = get(ar, batchSize, (rem + i) % batchSize, cache);
89+
if (from > max) {
90+
max = from;
91+
}
92+
ar[i]++;
93+
}
94+
int score = max + (rem == 0 ? 1 : 0);
95+
cache.put(hash, score);
96+
return score;
97+
}
98+
99+
private int[] pruneEnd(int[] in) {
100+
int endingZeros = 0;
101+
for (int i = in.length - 1; i >= 0; i--) {
102+
if (in[i] != 0) {
103+
break;
104+
}
105+
endingZeros++;
106+
}
107+
int[] out = new int[in.length - endingZeros];
108+
System.arraycopy(in, 0, out, 0, out.length);
109+
return out;
110+
}
111+
112+
private boolean zeroed(int[] ar) {
113+
for (int e : ar) {
114+
if (e != 0) {
115+
return false;
116+
}
86117
}
87-
res += val;
88-
MAP.put(s, res);
89-
return res;
118+
return true;
90119
}
91120
}
92121
```

src/main/java/g3401_3500/s3457_eat_pizzas/readme.md

Lines changed: 30 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -49,26 +49,39 @@ The total weight gained after eating all the pizzas is `2 + 1 = 3.`
4949
## Solution
5050

5151
```java
52-
import java.util.Arrays;
53-
54-
public class Solution {
52+
class Solution {
5553
public long maxWeight(int[] pizzas) {
56-
int n = pizzas.length;
57-
int m = n / 4;
58-
int z = (m + 1) / 2;
59-
int y = m / 2;
60-
int j = 0;
61-
Arrays.sort(pizzas);
62-
long res = 0;
63-
for (int i = 0; i < z; ++i) {
64-
res += pizzas[n - 1 - j];
65-
j += 1;
54+
int max = 0;
55+
for (int x : pizzas) {
56+
max = Math.max(max, x);
57+
}
58+
int[] count = new int[max + 1];
59+
for (int x : pizzas) {
60+
count[x]++;
61+
}
62+
int m = pizzas.length;
63+
int n = m / 4;
64+
int index = 0;
65+
for (int x = max; x > 0; --x) {
66+
if (count[x] != 0) {
67+
int c = count[x];
68+
while (c-- > 0) {
69+
pizzas[index++] = x;
70+
}
71+
if (index >= m / 2) {
72+
break;
73+
}
74+
}
75+
}
76+
long ans = 0;
77+
for (int i = 0; i < (n + 1) / 2; ++i) {
78+
ans += pizzas[i];
6679
}
67-
for (int i = 0; i < y; ++i) {
68-
res += pizzas[n - 1 - j - 1];
69-
j += 2;
80+
int k = n - (n + 1) / 2;
81+
for (int i = (n + 1) / 2 + 1; k > 0; i += 2, k--) {
82+
ans += pizzas[i];
7083
}
71-
return res;
84+
return ans;
7285
}
7386
}
7487
```

src/main/java/g3401_3500/s3459_length_of_longest_v_shaped_diagonal_segment/readme.md

Lines changed: 89 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -76,59 +76,105 @@ The longest V-shaped diagonal segment has a length of 1 and follows these coordi
7676
## Solution
7777

7878
```java
79-
import java.util.Arrays;
80-
8179
public class Solution {
82-
private final int[][] ds = { {1, 1}, {1, -1}, {-1, -1}, {-1, 1}};
83-
private final int[] nx = {2, 2, 0};
84-
private int[][] grid;
85-
private int n;
86-
private int m;
87-
private int[][][][] dp;
88-
89-
public int lenOfVDiagonal(int[][] g) {
90-
this.grid = g;
91-
this.n = g.length;
92-
this.m = g[0].length;
93-
this.dp = new int[n][m][4][2];
94-
for (int[][][] d1 : dp) {
95-
for (int[][] d2 : d1) {
96-
for (int[] d3 : d2) {
97-
Arrays.fill(d3, -1);
98-
}
80+
private final int[][] directions = { {-1, -1}, {-1, 1}, {1, 1}, {1, -1}};
81+
82+
private void initializeArrays(
83+
int[][] bottomLeft,
84+
int[][] bottomRight,
85+
int[][] topLeft,
86+
int[][] topRight,
87+
int m,
88+
int n) {
89+
for (int i = 0; i < m; ++i) {
90+
for (int j = 0; j < n; ++j) {
91+
bottomLeft[i][j] = 1;
92+
bottomRight[i][j] = 1;
93+
topLeft[i][j] = 1;
94+
topRight[i][j] = 1;
9995
}
10096
}
101-
int res = 0;
102-
for (int i = 0; i < n; i++) {
103-
for (int j = 0; j < m; j++) {
104-
if (g[i][j] == 1) {
105-
for (int d = 0; d < 4; d++) {
106-
res = Math.max(res, dp(i, j, 1, d, 1));
107-
}
97+
}
98+
99+
private int processBottomDirections(
100+
int[][] grid, int[][] bottomLeft, int[][] bottomRight, int m, int n) {
101+
int ans = 0;
102+
for (int i = 0; i < m; ++i) {
103+
for (int j = 0; j < n; ++j) {
104+
int x = grid[i][j];
105+
if (x == 1) {
106+
ans = 1;
107+
continue;
108+
}
109+
if (i > 0 && j + 1 < n && grid[i - 1][j + 1] == 2 - x) {
110+
bottomLeft[i][j] = bottomLeft[i - 1][j + 1] + 1;
111+
}
112+
if (i > 0 && j > 0 && grid[i - 1][j - 1] == 2 - x) {
113+
bottomRight[i][j] = bottomRight[i - 1][j - 1] + 1;
108114
}
109115
}
110116
}
111-
return res;
117+
return ans;
112118
}
113119

114-
private int dp(int i, int j, int x, int d, int k) {
115-
if (i < 0 || i >= n || j < 0 || j >= m) {
116-
return 0;
117-
}
118-
if (grid[i][j] != x) {
119-
return 0;
120-
}
121-
if (dp[i][j][d][k] != -1) {
122-
return dp[i][j][d][k];
120+
private void processTopDirections(
121+
int[][] grid, int[][] topLeft, int[][] topRight, int m, int n) {
122+
for (int i = m - 1; i >= 0; --i) {
123+
for (int j = n - 1; j >= 0; --j) {
124+
int x = grid[i][j];
125+
if (x == 1) {
126+
continue;
127+
}
128+
if (i + 1 < m && j + 1 < n && grid[i + 1][j + 1] == 2 - x) {
129+
topLeft[i][j] = topLeft[i + 1][j + 1] + 1;
130+
}
131+
if (i + 1 < m && j > 0 && grid[i + 1][j - 1] == 2 - x) {
132+
topRight[i][j] = topRight[i + 1][j - 1] + 1;
133+
}
134+
}
123135
}
124-
int res = dp(i + ds[d][0], j + ds[d][1], nx[x], d, k) + 1;
125-
if (k > 0) {
126-
int d2 = (d + 1) % 4;
127-
int res2 = dp(i + ds[d2][0], j + ds[d2][1], nx[x], d2, 0) + 1;
128-
res = Math.max(res, res2);
136+
}
137+
138+
private int findMaxDiagonal(int[][] grid, int[][][] memo, int m, int n, int initialAns) {
139+
int ans = initialAns;
140+
for (int i = 0; i < m; ++i) {
141+
for (int j = 0; j < n; ++j) {
142+
int x = grid[i][j];
143+
if (x == 1) {
144+
continue;
145+
}
146+
x >>= 1;
147+
for (int k = 0; k < 4; ++k) {
148+
int v = memo[k][i][j];
149+
if ((v & 1) != x) {
150+
continue;
151+
}
152+
if (v + memo[k + 3 & 3][i][j] > ans) {
153+
int[] d = directions[k];
154+
int ni = i - d[0] * v;
155+
int nj = j - d[1] * v;
156+
if (ni >= 0 && nj >= 0 && ni < m && nj < n && grid[ni][nj] == 1) {
157+
ans = Math.max(ans, v + memo[k + 3 & 3][i][j]);
158+
}
159+
}
160+
}
161+
}
129162
}
130-
dp[i][j][d][k] = res;
131-
return res;
163+
return ans;
164+
}
165+
166+
public int lenOfVDiagonal(int[][] grid) {
167+
int m = grid.length;
168+
int n = grid[0].length;
169+
int[][] bottomLeft = new int[m][n];
170+
int[][] bottomRight = new int[m][n];
171+
int[][] topLeft = new int[m][n];
172+
int[][] topRight = new int[m][n];
173+
initializeArrays(bottomLeft, bottomRight, topLeft, topRight, m, n);
174+
int ans = processBottomDirections(grid, bottomLeft, bottomRight, m, n);
175+
processTopDirections(grid, topLeft, topRight, m, n);
176+
int[][][] memo = {topLeft, topRight, bottomRight, bottomLeft};
177+
return findMaxDiagonal(grid, memo, m, n, ans);
132178
}
133179
}
134180
```

0 commit comments

Comments
 (0)