Skip to content

Commit a8620a1

Browse files
committed
Added tasks 3492-3495
1 parent 08faaee commit a8620a1

File tree

7 files changed

+500
-137
lines changed
  • src/main/java
    • g2201_2300/s2213_longest_substring_of_one_repeating_character
    • g3201_3300/s3283_maximum_number_of_moves_to_kill_all_pawns
    • g3401_3500
      • s3492_maximum_containers_on_a_ship
      • s3493_properties_graph
      • s3494_find_the_minimum_amount_of_time_to_brew_potions
      • s3495_minimum_operations_to_make_array_elements_zero

7 files changed

+500
-137
lines changed

README.md

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

20892089
| # | Title | Difficulty | Tag | Time, ms | Time, %
20902090
|------|----------------|-------------|-------------|----------|--------
2091+
| 3495 |[Minimum Operations to Make Array Elements Zero](src/main/java/g3401_3500/s3495_minimum_operations_to_make_array_elements_zero)| Hard | Array, Math, Bit_Manipulation | 9 | 99.71
2092+
| 3494 |[Find the Minimum Amount of Time to Brew Potions](src/main/java/g3401_3500/s3494_find_the_minimum_amount_of_time_to_brew_potions)| Medium | Array, Simulation, Prefix_Sum | 113 | 90.95
2093+
| 3493 |[Properties Graph](src/main/java/g3401_3500/s3493_properties_graph)| Medium | Array, Hash_Table, Depth_First_Search, Breadth_First_Search, Graph, Union_Find | 27 | 99.82
2094+
| 3492 |[Maximum Containers on a Ship](src/main/java/g3401_3500/s3492_maximum_containers_on_a_ship)| Easy | Math | 0 | 100.00
20912095
| 3490 |[Count Beautiful Numbers](src/main/java/g3401_3500/s3490_count_beautiful_numbers)| Hard | Dynamic_Programming | 523 | 100.00
20922096
| 3489 |[Zero Array Transformation IV](src/main/java/g3401_3500/s3489_zero_array_transformation_iv)| Medium | Array, Dynamic_Programming | 142 | 100.00
20932097
| 3488 |[Closest Equal Element Queries](src/main/java/g3401_3500/s3488_closest_equal_element_queries)| Medium | Array, Hash_Table, Binary_Search | 50 | 100.00
20942098
| 3487 |[Maximum Unique Subarray Sum After Deletion](src/main/java/g3401_3500/s3487_maximum_unique_subarray_sum_after_deletion)| Easy | Array, Hash_Table, Greedy | 2 | 100.00
2095-
| 3486 |[Longest Special Path II](src/main/java/g3401_3500/s3486_longest_special_path_ii)| Hard | Array, Hash_Table, Tree, Prefix_Sum, Depth_First_Search | 166 | 166 ms ( %)
2099+
| 3486 |[Longest Special Path II](src/main/java/g3401_3500/s3486_longest_special_path_ii)| Hard | Array, Hash_Table, Depth_First_Search, Tree, Prefix_Sum | 166 | 100.00
20962100
| 3485 |[Longest Common Prefix of K Strings After Removal](src/main/java/g3401_3500/s3485_longest_common_prefix_of_k_strings_after_removal)| Hard | Array, String, Trie | 333 | 100.00
20972101
| 3484 |[Design Spreadsheet](src/main/java/g3401_3500/s3484_design_spreadsheet)| Medium | Array, String, Hash_Table, Matrix, Design | 117 | 100.00
20982102
| 3483 |[Unique 3-Digit Even Numbers](src/main/java/g3401_3500/s3483_unique_3_digit_even_numbers)| Easy | Array, Hash_Table, Recursion, Enumeration | 5 | 100.00
@@ -2259,7 +2263,7 @@
22592263
| 3287 |[Find the Maximum Sequence Value of Array](src/main/java/g3201_3300/s3287_find_the_maximum_sequence_value_of_array)| Hard | Array, Dynamic_Programming, Bit_Manipulation | 1140 | 100.00
22602264
| 3286 |[Find a Safe Walk Through a Grid](src/main/java/g3201_3300/s3286_find_a_safe_walk_through_a_grid)| Medium | Array, Breadth_First_Search, Matrix, Heap_Priority_Queue, Graph, Shortest_Path | 90 | 100.00
22612265
| 3285 |[Find Indices of Stable Mountains](src/main/java/g3201_3300/s3285_find_indices_of_stable_mountains)| Easy | Array | 1 | 100.00
2262-
| 3283 |[Maximum Number of Moves to Kill All Pawns](src/main/java/g3201_3300/s3283_maximum_number_of_moves_to_kill_all_pawns)| Hard | Array, Math, Breadth_First_Search, Bit_Manipulation, Bitmask, Game_Theory | 250 | 98.43
2266+
| 3283 |[Maximum Number of Moves to Kill All Pawns](src/main/java/g3201_3300/s3283_maximum_number_of_moves_to_kill_all_pawns)| Hard | Array, Math, Breadth_First_Search, Bit_Manipulation, Bitmask, Game_Theory | 126 | 100.00
22632267
| 3282 |[Reach End of Array With Max Score](src/main/java/g3201_3300/s3282_reach_end_of_array_with_max_score)| Medium | Array, Greedy | 9 | 100.00
22642268
| 3281 |[Maximize Score of Numbers in Ranges](src/main/java/g3201_3300/s3281_maximize_score_of_numbers_in_ranges)| Medium | Array, Sorting, Greedy, Binary_Search | 47 | 100.00
22652269
| 3280 |[Convert Date to Binary](src/main/java/g3201_3300/s3280_convert_date_to_binary)| Easy | String, Math | 3 | 100.00
@@ -3087,7 +3091,7 @@
30873091
| 2217 |[Find Palindrome With Fixed Length](src/main/java/g2201_2300/s2217_find_palindrome_with_fixed_length)| Medium | Array, Math | 37 | 88.60
30883092
| 2216 |[Minimum Deletions to Make Array Beautiful](src/main/java/g2201_2300/s2216_minimum_deletions_to_make_array_beautiful)| Medium | Array, Greedy, Stack | 7 | 39.24
30893093
| 2215 |[Find the Difference of Two Arrays](src/main/java/g2201_2300/s2215_find_the_difference_of_two_arrays)| Easy | Array, Hash_Table | 11 | 87.39
3090-
| 2213 |[Longest Substring of One Repeating Character](src/main/java/g2201_2300/s2213_longest_substring_of_one_repeating_character)| Hard | Array, String, Ordered_Set, Segment_Tree | 141 | 86.81
3094+
| 2213 |[Longest Substring of One Repeating Character](src/main/java/g2201_2300/s2213_longest_substring_of_one_repeating_character)| Hard | Array, String, Ordered_Set, Segment_Tree | 79 | 89.74
30913095
| 2212 |[Maximum Points in an Archery Competition](src/main/java/g2201_2300/s2212_maximum_points_in_an_archery_competition)| Medium | Array, Bit_Manipulation, Recursion, Enumeration | 7 | 78.16
30923096
| 2211 |[Count Collisions on a Road](src/main/java/g2201_2300/s2211_count_collisions_on_a_road)| Medium | String, Stack | 113 | 45.96
30933097
| 2210 |[Count Hills and Valleys in an Array](src/main/java/g2201_2300/s2210_count_hills_and_valleys_in_an_array)| Easy | Array | 0 | 100.00

src/main/java/g2201_2300/s2213_longest_substring_of_one_repeating_character/readme.md

Lines changed: 53 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -50,90 +50,70 @@ Thus, we return [2,3].
5050

5151
```java
5252
public class Solution {
53-
static class TreeNode {
54-
int start;
55-
int end;
56-
char leftChar;
57-
int leftCharLen;
58-
char rightChar;
59-
int rightCharLen;
60-
int max;
61-
TreeNode left;
62-
TreeNode right;
63-
64-
TreeNode(int start, int end) {
65-
this.start = start;
66-
this.end = end;
67-
left = null;
68-
right = null;
69-
}
70-
}
53+
private char[] ca;
7154

7255
public int[] longestRepeating(String s, String queryCharacters, int[] queryIndices) {
73-
char[] sChar = s.toCharArray();
74-
char[] qChar = queryCharacters.toCharArray();
75-
TreeNode root = buildTree(sChar, 0, sChar.length - 1);
76-
int[] result = new int[qChar.length];
77-
for (int i = 0; i < qChar.length; i++) {
78-
updateTree(root, queryIndices[i], qChar[i]);
79-
if (root != null) {
80-
result[i] = root.max;
81-
}
56+
ca = s.toCharArray();
57+
int[] result = new int[queryIndices.length];
58+
SegmentTree root = new SegmentTree(0, ca.length);
59+
for (int i = 0; i < queryIndices.length; i++) {
60+
ca[queryIndices[i]] = queryCharacters.charAt(i);
61+
root.update(queryIndices[i]);
62+
result[i] = root.longest;
8263
}
8364
return result;
8465
}
8566

86-
private TreeNode buildTree(char[] s, int from, int to) {
87-
if (from > to) {
88-
return null;
89-
}
90-
TreeNode root = new TreeNode(from, to);
91-
if (from == to) {
92-
root.max = 1;
93-
root.rightChar = root.leftChar = s[from];
94-
root.leftCharLen = root.rightCharLen = 1;
95-
return root;
96-
}
97-
int middle = from + (to - from) / 2;
98-
root.left = buildTree(s, from, middle);
99-
root.right = buildTree(s, middle + 1, to);
100-
updateNode(root);
101-
return root;
102-
}
67+
private class SegmentTree {
68+
final int start;
69+
final int end;
70+
int longest;
71+
int leftLength;
72+
int rightLength;
73+
SegmentTree left;
74+
SegmentTree right;
10375

104-
private void updateTree(TreeNode root, int index, char c) {
105-
if (root == null || root.start > index || root.end < index) {
106-
return;
107-
}
108-
if (root.start == index && root.end == index) {
109-
root.leftChar = root.rightChar = c;
110-
return;
76+
SegmentTree(int start, int end) {
77+
this.start = start;
78+
this.end = end;
79+
if (end - start > 1) {
80+
int mid = (start + end) / 2;
81+
left = new SegmentTree(start, mid);
82+
right = new SegmentTree(mid, end);
83+
merge();
84+
} else {
85+
longest = leftLength = rightLength = 1;
86+
}
11187
}
112-
updateTree(root.left, index, c);
113-
updateTree(root.right, index, c);
114-
updateNode(root);
115-
}
11688

117-
private void updateNode(TreeNode root) {
118-
if (root == null) {
119-
return;
120-
}
121-
root.leftChar = root.left.leftChar;
122-
root.leftCharLen = root.left.leftCharLen;
123-
root.rightChar = root.right.rightChar;
124-
root.rightCharLen = root.right.rightCharLen;
125-
root.max = Math.max(root.left.max, root.right.max);
126-
if (root.left.rightChar == root.right.leftChar) {
127-
int len = root.left.rightCharLen + root.right.leftCharLen;
128-
if (root.left.leftChar == root.left.rightChar
129-
&& root.left.leftCharLen == root.left.end - root.left.start + 1) {
130-
root.leftCharLen = len;
89+
void update(int index) {
90+
if (end - start == 1) {
91+
return;
92+
}
93+
if (index < left.end) {
94+
left.update(index);
95+
} else {
96+
right.update(index);
13197
}
132-
if (root.right.leftChar == root.right.rightChar
133-
&& root.right.leftCharLen == root.right.end - root.right.start + 1) {
134-
root.rightCharLen = len;
98+
merge();
99+
}
100+
101+
private void merge() {
102+
longest = Math.max(left.longest, right.longest);
103+
if (ca[left.end - 1] == ca[right.start]) {
104+
longest = Math.max(longest, left.rightLength + right.leftLength);
105+
leftLength =
106+
(left.leftLength == left.end - left.start)
107+
? left.leftLength + right.leftLength
108+
: left.leftLength;
109+
rightLength =
110+
(right.rightLength == right.end - right.start)
111+
? right.rightLength + left.rightLength
112+
: right.rightLength;
113+
} else {
114+
leftLength = left.leftLength;
115+
rightLength = right.rightLength;
135116
}
136-
root.max = Math.max(root.max, len);
137117
}
138118
}
139119
}

src/main/java/g3201_3300/s3283_maximum_number_of_moves_to_kill_all_pawns/readme.md

Lines changed: 73 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -69,85 +69,97 @@ The knight takes 4 moves to reach the pawn at `(0, 0)`.
6969
## Solution
7070

7171
```java
72-
import java.util.LinkedList;
72+
import java.util.ArrayDeque;
7373
import java.util.Queue;
7474

7575
public class Solution {
76-
private static final int[][] KNIGHT_MOVES = {
77-
{-2, -1}, {-2, 1}, {-1, -2}, {-1, 2},
78-
{1, -2}, {1, 2}, {2, -1}, {2, 1}
76+
private static final int[][] DIRECTIONS = {
77+
{2, 1}, {1, 2}, {-1, 2}, {-2, 1}, {-2, -1}, {-1, -2}, {1, -2}, {2, -1}
7978
};
80-
private int[][] distances;
81-
private Integer[][] memo;
8279

83-
public int maxMoves(int kx, int ky, int[][] positions) {
80+
private void initializePositions(int[][] positions, int[][] pos, int kx, int ky) {
8481
int n = positions.length;
85-
distances = new int[n + 1][n + 1];
86-
memo = new Integer[n + 1][1 << n];
87-
// Calculate distances between all pairs of positions (including knight's initial position)
8882
for (int i = 0; i < n; i++) {
89-
distances[n][i] = calculateMoves(kx, ky, positions[i][0], positions[i][1]);
90-
for (int j = i + 1; j < n; j++) {
91-
int dist =
92-
calculateMoves(
93-
positions[i][0], positions[i][1], positions[j][0], positions[j][1]);
94-
distances[i][j] = distances[j][i] = dist;
95-
}
83+
int x = positions[i][0];
84+
int y = positions[i][1];
85+
pos[x][y] = i;
9686
}
97-
return minimax(n, (1 << n) - 1, true);
87+
pos[kx][ky] = n;
9888
}
9989

100-
private int minimax(int lastPos, int remainingPawns, boolean isAlice) {
101-
if (remainingPawns == 0) {
102-
return 0;
103-
}
104-
if (memo[lastPos][remainingPawns] != null) {
105-
return memo[lastPos][remainingPawns];
106-
}
107-
int result = isAlice ? 0 : Integer.MAX_VALUE;
108-
for (int i = 0; i < distances.length - 1; i++) {
109-
if ((remainingPawns & (1 << i)) != 0) {
110-
int newRemainingPawns = remainingPawns & ~(1 << i);
111-
int moveValue = distances[lastPos][i] + minimax(i, newRemainingPawns, !isAlice);
112-
113-
if (isAlice) {
114-
result = Math.max(result, moveValue);
115-
} else {
116-
result = Math.min(result, moveValue);
90+
private void calculateDistances(int[][] positions, int[][] pos, int[][] distances) {
91+
int n = positions.length;
92+
for (int i = 0; i < n; i++) {
93+
int count = n - i;
94+
boolean[][] visited = new boolean[50][50];
95+
visited[positions[i][0]][positions[i][1]] = true;
96+
Queue<int[]> que = new ArrayDeque<>();
97+
que.offer(new int[] {positions[i][0], positions[i][1]});
98+
int steps = 1;
99+
while (!que.isEmpty() && count > 0) {
100+
int size = que.size();
101+
while (size-- > 0) {
102+
int[] cur = que.poll();
103+
int x = cur[0];
104+
int y = cur[1];
105+
for (int[] d : DIRECTIONS) {
106+
int nx = x + d[0];
107+
int ny = y + d[1];
108+
if (0 <= nx && nx < 50 && 0 <= ny && ny < 50 && !visited[nx][ny]) {
109+
que.offer(new int[] {nx, ny});
110+
visited[nx][ny] = true;
111+
int j = pos[nx][ny];
112+
if (j > i) {
113+
distances[i][j] = distances[j][i] = steps;
114+
if (--count == 0) {
115+
break;
116+
}
117+
}
118+
}
119+
}
120+
if (count == 0) {
121+
break;
122+
}
117123
}
124+
steps++;
118125
}
119126
}
120-
memo[lastPos][remainingPawns] = result;
121-
return result;
122127
}
123128

124-
private int calculateMoves(int x1, int y1, int x2, int y2) {
125-
if (x1 == x2 && y1 == y2) {
126-
return 0;
127-
}
128-
boolean[][] visited = new boolean[50][50];
129-
Queue<int[]> queue = new LinkedList<>();
130-
queue.offer(new int[] {x1, y1, 0});
131-
visited[x1][y1] = true;
132-
while (!queue.isEmpty()) {
133-
int[] current = queue.poll();
134-
int x = current[0];
135-
int y = current[1];
136-
int moves = current[2];
137-
for (int[] move : KNIGHT_MOVES) {
138-
int nx = x + move[0];
139-
int ny = y + move[1];
140-
if (nx == x2 && ny == y2) {
141-
return moves + 1;
142-
}
143-
if (nx >= 0 && nx < 50 && ny >= 0 && ny < 50 && !visited[nx][ny]) {
144-
queue.offer(new int[] {nx, ny, moves + 1});
145-
visited[nx][ny] = true;
129+
private int calculateDP(int n, int[][] distances) {
130+
int m = (1 << n) - 1;
131+
int[][] dp = new int[1 << n][n + 1];
132+
for (int mask = 1; mask < (1 << n); mask++) {
133+
boolean isEven = (Integer.bitCount(m ^ mask)) % 2 == 0;
134+
for (int i = 0; i <= n; i++) {
135+
int result = 0;
136+
if (isEven) {
137+
for (int j = 0; j < n; j++) {
138+
if ((mask & (1 << j)) > 0) {
139+
result = Math.max(result, dp[mask ^ (1 << j)][j] + distances[i][j]);
140+
}
141+
}
142+
} else {
143+
result = Integer.MAX_VALUE;
144+
for (int j = 0; j < n; j++) {
145+
if ((mask & (1 << j)) > 0) {
146+
result = Math.min(result, dp[mask ^ (1 << j)][j] + distances[i][j]);
147+
}
148+
}
146149
}
150+
dp[mask][i] = result;
147151
}
148152
}
149-
// Should never reach here if input is valid
150-
return -1;
153+
return dp[m][n];
154+
}
155+
156+
public int maxMoves(int kx, int ky, int[][] positions) {
157+
int n = positions.length;
158+
int[][] pos = new int[50][50];
159+
initializePositions(positions, pos, kx, ky);
160+
int[][] distances = new int[n + 1][n + 1];
161+
calculateDistances(positions, pos, distances);
162+
return calculateDP(n, distances);
151163
}
152164
}
153165
```
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
[![](https://img.shields.io/github/stars/javadev/LeetCode-in-Java?label=Stars&style=flat-square)](https://github.com/javadev/LeetCode-in-Java)
2+
[![](https://img.shields.io/github/forks/javadev/LeetCode-in-Java?label=Fork%20me%20on%20GitHub%20&style=flat-square)](https://github.com/javadev/LeetCode-in-Java/fork)
3+
4+
## 3492\. Maximum Containers on a Ship
5+
6+
Easy
7+
8+
You are given a positive integer `n` representing an `n x n` cargo deck on a ship. Each cell on the deck can hold one container with a weight of **exactly** `w`.
9+
10+
However, the total weight of all containers, if loaded onto the deck, must not exceed the ship's maximum weight capacity, `maxWeight`.
11+
12+
Return the **maximum** number of containers that can be loaded onto the ship.
13+
14+
**Example 1:**
15+
16+
**Input:** n = 2, w = 3, maxWeight = 15
17+
18+
**Output:** 4
19+
20+
**Explanation:**
21+
22+
The deck has 4 cells, and each container weighs 3. The total weight of loading all containers is 12, which does not exceed `maxWeight`.
23+
24+
**Example 2:**
25+
26+
**Input:** n = 3, w = 5, maxWeight = 20
27+
28+
**Output:** 4
29+
30+
**Explanation:**
31+
32+
The deck has 9 cells, and each container weighs 5. The maximum number of containers that can be loaded without exceeding `maxWeight` is 4.
33+
34+
**Constraints:**
35+
36+
* `1 <= n <= 1000`
37+
* `1 <= w <= 1000`
38+
* <code>1 <= maxWeight <= 10<sup>9</sup></code>
39+
40+
## Solution
41+
42+
```java
43+
public class Solution {
44+
public int maxContainers(int n, int w, int maxWeight) {
45+
int c = n * n;
46+
int count = maxWeight / w;
47+
return Math.min(c, count);
48+
}
49+
}
50+
```

0 commit comments

Comments
 (0)