Skip to content

Commit c67d4ff

Browse files
authored
Added tasks 3340-3343
1 parent 9ecc6a1 commit c67d4ff

File tree

6 files changed

+431
-37
lines changed
  • src/main/java
    • g3001_3100/s3001_minimum_moves_to_capture_the_queen
    • g3301_3400
      • s3340_check_balanced_string
      • s3341_find_minimum_time_to_reach_last_room_i
      • s3342_find_minimum_time_to_reach_last_room_ii
      • s3343_count_number_of_balanced_permutations

6 files changed

+431
-37
lines changed

README.md

Lines changed: 35 additions & 31 deletions
Large diffs are not rendered by default.

src/main/java/g3001_3100/s3001_minimum_moves_to_capture_the_queen/readme.md

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -59,17 +59,16 @@ It is impossible to capture the black queen in less than two moves since it is n
5959
public class Solution {
6060
public int minMovesToCaptureTheQueen(int a, int b, int c, int d, int e, int f) {
6161
if (a == e || b == f) {
62-
if (a == c && (d > b && d < f || d > f && d < b)) {
62+
if (a == e && a == c && (d - b) * (d - f) < 0) {
6363
return 2;
6464
}
65-
if (b == d && (c > a && c < e || c > e && c < a)) {
65+
if (b == f && b == d && (c - a) * (c - e) < 0) {
6666
return 2;
6767
}
6868
return 1;
69-
} else if (Math.abs(c - e) == Math.abs(d - f)) {
70-
if (Math.abs(a - c) == Math.abs(b - d)
71-
&& Math.abs(e - a) == Math.abs(f - b)
72-
&& (a > e && a < c || a > c && a < e)) {
69+
}
70+
if (Math.abs(c - e) == Math.abs(d - f)) {
71+
if (Math.abs(c - a) == Math.abs(d - b) && (b - f) * (b - d) < 0) {
7372
return 2;
7473
}
7574
return 1;
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
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+
## 3340\. Check Balanced String
5+
6+
Easy
7+
8+
You are given a string `num` consisting of only digits. A string of digits is called **balanced** if the sum of the digits at even indices is equal to the sum of digits at odd indices.
9+
10+
Return `true` if `num` is **balanced**, otherwise return `false`.
11+
12+
**Example 1:**
13+
14+
**Input:** num = "1234"
15+
16+
**Output:** false
17+
18+
**Explanation:**
19+
20+
* The sum of digits at even indices is `1 + 3 == 4`, and the sum of digits at odd indices is `2 + 4 == 6`.
21+
* Since 4 is not equal to 6, `num` is not balanced.
22+
23+
**Example 2:**
24+
25+
**Input:** num = "24123"
26+
27+
**Output:** true
28+
29+
**Explanation:**
30+
31+
* The sum of digits at even indices is `2 + 1 + 3 == 6`, and the sum of digits at odd indices is `4 + 2 == 6`.
32+
* Since both are equal the `num` is balanced.
33+
34+
**Constraints:**
35+
36+
* `2 <= num.length <= 100`
37+
* `num` consists of digits only
38+
39+
## Solution
40+
41+
```java
42+
public class Solution {
43+
public boolean isBalanced(String num) {
44+
int diff = 0;
45+
int sign = 1;
46+
int n = num.length();
47+
for (int i = 0; i < n; ++i) {
48+
diff += sign * (num.charAt(i) - '0');
49+
sign = -sign;
50+
}
51+
return diff == 0;
52+
}
53+
}
54+
```
Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
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+
## 3341\. Find Minimum Time to Reach Last Room I
5+
6+
Medium
7+
8+
There is a dungeon with `n x m` rooms arranged as a grid.
9+
10+
You are given a 2D array `moveTime` of size `n x m`, where `moveTime[i][j]` represents the **minimum** time in seconds when you can **start moving** to that room. You start from the room `(0, 0)` at time `t = 0` and can move to an **adjacent** room. Moving between adjacent rooms takes _exactly_ one second.
11+
12+
Return the **minimum** time to reach the room `(n - 1, m - 1)`.
13+
14+
Two rooms are **adjacent** if they share a common wall, either _horizontally_ or _vertically_.
15+
16+
**Example 1:**
17+
18+
**Input:** moveTime = \[\[0,4],[4,4]]
19+
20+
**Output:** 6
21+
22+
**Explanation:**
23+
24+
The minimum time required is 6 seconds.
25+
26+
* At time `t == 4`, move from room `(0, 0)` to room `(1, 0)` in one second.
27+
* At time `t == 5`, move from room `(1, 0)` to room `(1, 1)` in one second.
28+
29+
**Example 2:**
30+
31+
**Input:** moveTime = \[\[0,0,0],[0,0,0]]
32+
33+
**Output:** 3
34+
35+
**Explanation:**
36+
37+
The minimum time required is 3 seconds.
38+
39+
* At time `t == 0`, move from room `(0, 0)` to room `(1, 0)` in one second.
40+
* At time `t == 1`, move from room `(1, 0)` to room `(1, 1)` in one second.
41+
* At time `t == 2`, move from room `(1, 1)` to room `(1, 2)` in one second.
42+
43+
**Example 3:**
44+
45+
**Input:** moveTime = \[\[0,1],[1,2]]
46+
47+
**Output:** 3
48+
49+
**Constraints:**
50+
51+
* `2 <= n == moveTime.length <= 50`
52+
* `2 <= m == moveTime[i].length <= 50`
53+
* <code>0 <= moveTime[i][j] <= 10<sup>9</sup></code>
54+
55+
## Solution
56+
57+
```java
58+
import java.util.Arrays;
59+
import java.util.Comparator;
60+
import java.util.PriorityQueue;
61+
62+
public class Solution {
63+
public int minTimeToReach(int[][] moveTime) {
64+
int rows = moveTime.length;
65+
int cols = moveTime[0].length;
66+
PriorityQueue<int[]> minHeap = new PriorityQueue<>(Comparator.comparingInt(a -> a[0]));
67+
int[][] time = new int[rows][cols];
68+
for (int[] row : time) {
69+
Arrays.fill(row, Integer.MAX_VALUE);
70+
}
71+
minHeap.offer(new int[] {0, 0, 0});
72+
time[0][0] = 0;
73+
int[][] directions = { {1, 0}, {-1, 0}, {0, 1}, {0, -1}};
74+
while (!minHeap.isEmpty()) {
75+
int[] current = minHeap.poll();
76+
int currentTime = current[0];
77+
int x = current[1];
78+
int y = current[2];
79+
if (x == rows - 1 && y == cols - 1) {
80+
return currentTime;
81+
}
82+
for (int[] dir : directions) {
83+
int newX = x + dir[0];
84+
int newY = y + dir[1];
85+
if (newX >= 0 && newX < rows && newY >= 0 && newY < cols) {
86+
int waitTime = Math.max(moveTime[newX][newY] - currentTime, 0);
87+
int newTime = currentTime + 1 + waitTime;
88+
if (newTime < time[newX][newY]) {
89+
time[newX][newY] = newTime;
90+
minHeap.offer(new int[] {newTime, newX, newY});
91+
}
92+
}
93+
}
94+
}
95+
return -1;
96+
}
97+
}
98+
```
Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
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+
## 3342\. Find Minimum Time to Reach Last Room II
5+
6+
Medium
7+
8+
There is a dungeon with `n x m` rooms arranged as a grid.
9+
10+
You are given a 2D array `moveTime` of size `n x m`, where `moveTime[i][j]` represents the **minimum** time in seconds when you can **start moving** to that room. You start from the room `(0, 0)` at time `t = 0` and can move to an **adjacent** room. Moving between **adjacent** rooms takes one second for one move and two seconds for the next, **alternating** between the two.
11+
12+
Return the **minimum** time to reach the room `(n - 1, m - 1)`.
13+
14+
Two rooms are **adjacent** if they share a common wall, either _horizontally_ or _vertically_.
15+
16+
**Example 1:**
17+
18+
**Input:** moveTime = \[\[0,4],[4,4]]
19+
20+
**Output:** 7
21+
22+
**Explanation:**
23+
24+
The minimum time required is 7 seconds.
25+
26+
* At time `t == 4`, move from room `(0, 0)` to room `(1, 0)` in one second.
27+
* At time `t == 5`, move from room `(1, 0)` to room `(1, 1)` in two seconds.
28+
29+
**Example 2:**
30+
31+
**Input:** moveTime = \[\[0,0,0,0],[0,0,0,0]]
32+
33+
**Output:** 6
34+
35+
**Explanation:**
36+
37+
The minimum time required is 6 seconds.
38+
39+
* At time `t == 0`, move from room `(0, 0)` to room `(1, 0)` in one second.
40+
* At time `t == 1`, move from room `(1, 0)` to room `(1, 1)` in two seconds.
41+
* At time `t == 3`, move from room `(1, 1)` to room `(1, 2)` in one second.
42+
* At time `t == 4`, move from room `(1, 2)` to room `(1, 3)` in two seconds.
43+
44+
**Example 3:**
45+
46+
**Input:** moveTime = \[\[0,1],[1,2]]
47+
48+
**Output:** 4
49+
50+
**Constraints:**
51+
52+
* `2 <= n == moveTime.length <= 750`
53+
* `2 <= m == moveTime[i].length <= 750`
54+
* <code>0 <= moveTime[i][j] <= 10<sup>9</sup></code>
55+
56+
## Solution
57+
58+
```java
59+
import java.util.PriorityQueue;
60+
61+
public class Solution {
62+
private static class Node {
63+
int x;
64+
int y;
65+
int t;
66+
int turn;
67+
}
68+
69+
private final int[][] dir = { {1, 0}, {-1, 0}, {0, 1}, {0, -1}};
70+
71+
public int minTimeToReach(int[][] moveTime) {
72+
PriorityQueue<Node> pq = new PriorityQueue<>((a, b) -> a.t - b.t);
73+
int m = moveTime.length;
74+
int n = moveTime[0].length;
75+
Node node = new Node();
76+
node.x = 0;
77+
node.y = 0;
78+
int t = 0;
79+
node.t = t;
80+
node.turn = 0;
81+
pq.add(node);
82+
moveTime[0][0] = -1;
83+
while (!pq.isEmpty()) {
84+
Node curr = pq.poll();
85+
for (int i = 0; i < 4; i++) {
86+
int x = curr.x + dir[i][0];
87+
int y = curr.y + dir[i][1];
88+
if (x == m - 1 && y == n - 1) {
89+
t = Math.max(curr.t, moveTime[x][y]) + 1 + curr.turn;
90+
return t;
91+
}
92+
if (x >= 0 && x < m && y < n && y >= 0 && moveTime[x][y] != -1) {
93+
Node newNode = new Node();
94+
t = Math.max(curr.t, moveTime[x][y]) + 1 + curr.turn;
95+
newNode.x = x;
96+
newNode.y = y;
97+
newNode.t = t;
98+
newNode.turn = curr.turn == 1 ? 0 : 1;
99+
pq.add(newNode);
100+
moveTime[x][y] = -1;
101+
}
102+
}
103+
}
104+
return -1;
105+
}
106+
}
107+
```

0 commit comments

Comments
 (0)