Skip to content

Commit 4781844

Browse files
committed
Added tags
1 parent d4cd546 commit 4781844

File tree

4 files changed

+44
-35
lines changed

4 files changed

+44
-35
lines changed

src/main/java/g3301_3400/s3340_check_balanced_string/Solution.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package g3301_3400.s3340_check_balanced_string;
22

3-
// #Easy #2024_11_04_Time_0_ms_(100.00%)_Space_42.1_MB_(100.00%)
3+
// #Easy #String #2024_11_05_Time_1_ms_(99.60%)_Space_42_MB_(79.77%)
44

55
public class Solution {
66
public boolean isBalanced(String num) {

src/main/java/g3301_3400/s3341_find_minimum_time_to_reach_last_room_i/Solution.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package g3301_3400.s3341_find_minimum_time_to_reach_last_room_i;
22

3-
// #Medium #2024_11_04_Time_8_ms_(100.00%)_Space_45.3_MB_(100.00%)
3+
// #Medium #Array #Matrix #Heap_Priority_Queue #Graph #Shortest_Path
4+
// #2024_11_05_Time_8_ms_(67.58%)_Space_45.4_MB_(19.79%)
45

56
import java.util.Arrays;
67
import java.util.Comparator;

src/main/java/g3301_3400/s3342_find_minimum_time_to_reach_last_room_ii/Solution.java

Lines changed: 39 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,43 +1,50 @@
11
package g3301_3400.s3342_find_minimum_time_to_reach_last_room_ii;
22

3-
// #Medium #2024_11_04_Time_573_ms_(100.00%)_Space_127_MB_(100.00%)
3+
// #Medium #Array #Matrix #Heap_Priority_Queue #Graph #Shortest_Path
4+
// #2024_11_05_Time_495_ms_(37.48%)_Space_126.6_MB_(8.65%)
45

5-
import java.util.Arrays;
6-
import java.util.Comparator;
76
import java.util.PriorityQueue;
87

98
public class Solution {
9+
private static class Node {
10+
int x;
11+
int y;
12+
int t;
13+
int turn;
14+
}
15+
16+
private final int[][] dir = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}};
17+
1018
public int minTimeToReach(int[][] moveTime) {
11-
int n = moveTime.length;
12-
int m = moveTime[0].length;
13-
int inf = Integer.MAX_VALUE;
14-
int[][] dp = new int[n][m];
15-
for (int i = 0; i < n; i++) {
16-
Arrays.fill(dp[i], inf);
17-
}
18-
PriorityQueue<int[]> h = new PriorityQueue<>(Comparator.comparingInt(a -> a[0]));
19-
// arrive time, i, j
20-
h.offer(new int[] {0, 0, 0});
19+
PriorityQueue<Node> pq = new PriorityQueue<>((a, b) -> a.t - b.t);
20+
int m = moveTime.length;
21+
int n = moveTime[0].length;
22+
Node node = new Node();
23+
node.x = 0;
24+
node.y = 0;
25+
int t = 0;
26+
node.t = t;
27+
node.turn = 0;
28+
pq.add(node);
2129
moveTime[0][0] = -1;
22-
while (!h.isEmpty()) {
23-
int[] cur = h.poll();
24-
int t = cur[0];
25-
int i = cur[1];
26-
int j = cur[2];
27-
if (t >= dp[i][j]) {
28-
continue;
29-
}
30-
if (i == n - 1 && j == m - 1) {
31-
return t;
32-
}
33-
dp[i][j] = t;
34-
int[][] dirs = {{1, 0}, {0, 1}, {-1, 0}, {0, -1}};
35-
for (int[] dir : dirs) {
36-
int x = i + dir[0];
37-
int y = j + dir[1];
38-
int c = (i + j) % 2 + 1;
39-
if (0 <= x && x < n && 0 <= y && y < m && dp[x][y] == inf) {
40-
h.offer(new int[] {Math.max(moveTime[x][y], t) + c, x, y});
30+
while (!pq.isEmpty()) {
31+
Node curr = pq.poll();
32+
for (int i = 0; i < 4; i++) {
33+
int x = curr.x + dir[i][0];
34+
int y = curr.y + dir[i][1];
35+
if (x == m - 1 && y == n - 1) {
36+
t = Math.max(curr.t, moveTime[x][y]) + 1 + curr.turn;
37+
return t;
38+
}
39+
if (x >= 0 && x < m && y < n && y >= 0 && moveTime[x][y] != -1) {
40+
Node newNode = new Node();
41+
t = Math.max(curr.t, moveTime[x][y]) + 1 + curr.turn;
42+
newNode.x = x;
43+
newNode.y = y;
44+
newNode.t = t;
45+
newNode.turn = curr.turn == 1 ? 0 : 1;
46+
pq.add(newNode);
47+
moveTime[x][y] = -1;
4148
}
4249
}
4350
}

src/main/java/g3301_3400/s3343_count_number_of_balanced_permutations/Solution.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package g3301_3400.s3343_count_number_of_balanced_permutations;
22

3-
// #Hard #2024_11_05_Time_61_ms_(97.56%)_Space_44.3_MB_(100.00%)
3+
// #Hard #String #Dynamic_Programming #Math #Combinatorics
4+
// #2024_11_05_Time_64_ms_(85.37%)_Space_44.8_MB_(95.12%)
45

56
public class Solution {
67
private static final int M = 1000000007;

0 commit comments

Comments
 (0)