Skip to content

Commit f1387f8

Browse files
authored
Added tasks 3340-3343
1 parent f39a024 commit f1387f8

File tree

12 files changed

+520
-0
lines changed

12 files changed

+520
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package g3301_3400.s3340_check_balanced_string
2+
3+
// #Easy #String #2024_11_05_Time_1_ms_(100.00%)_Space_34.9_MB_(84.38%)
4+
5+
class Solution {
6+
fun isBalanced(num: String): Boolean {
7+
var diff = 0
8+
var sign = 1
9+
val n = num.length
10+
for (i in 0 until n) {
11+
diff += sign * (num[i].code - '0'.code)
12+
sign = -sign
13+
}
14+
return diff == 0
15+
}
16+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
3340\. Check Balanced String
2+
3+
Easy
4+
5+
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.
6+
7+
Return `true` if `num` is **balanced**, otherwise return `false`.
8+
9+
**Example 1:**
10+
11+
**Input:** num = "1234"
12+
13+
**Output:** false
14+
15+
**Explanation:**
16+
17+
* The sum of digits at even indices is `1 + 3 == 4`, and the sum of digits at odd indices is `2 + 4 == 6`.
18+
* Since 4 is not equal to 6, `num` is not balanced.
19+
20+
**Example 2:**
21+
22+
**Input:** num = "24123"
23+
24+
**Output:** true
25+
26+
**Explanation:**
27+
28+
* The sum of digits at even indices is `2 + 1 + 3 == 6`, and the sum of digits at odd indices is `4 + 2 == 6`.
29+
* Since both are equal the `num` is balanced.
30+
31+
**Constraints:**
32+
33+
* `2 <= num.length <= 100`
34+
* `num` consists of digits only
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package g3301_3400.s3341_find_minimum_time_to_reach_last_room_i
2+
3+
// #Medium #Array #Matrix #Heap_Priority_Queue #Graph #Shortest_Path
4+
// #2024_11_05_Time_257_ms_(42.10%)_Space_46.1_MB_(10.53%)
5+
6+
import java.util.Comparator
7+
import java.util.PriorityQueue
8+
import java.util.function.ToIntFunction
9+
import kotlin.math.max
10+
11+
class Solution {
12+
fun minTimeToReach(moveTime: Array<IntArray>): Int {
13+
val rows = moveTime.size
14+
val cols = moveTime[0].size
15+
val minHeap =
16+
PriorityQueue<IntArray>(Comparator.comparingInt<IntArray>(ToIntFunction { a: IntArray -> a[0] }))
17+
val time: Array<IntArray> = Array<IntArray>(rows) { IntArray(cols) }
18+
for (row in time) {
19+
row.fill(Int.Companion.MAX_VALUE)
20+
}
21+
minHeap.offer(intArrayOf(0, 0, 0))
22+
time[0][0] = 0
23+
val directions = arrayOf<IntArray>(intArrayOf(1, 0), intArrayOf(-1, 0), intArrayOf(0, 1), intArrayOf(0, -1))
24+
while (minHeap.isNotEmpty()) {
25+
val current = minHeap.poll()
26+
val currentTime = current[0]
27+
val x = current[1]
28+
val y = current[2]
29+
if (x == rows - 1 && y == cols - 1) {
30+
return currentTime
31+
}
32+
for (dir in directions) {
33+
val newX = x + dir[0]
34+
val newY = y + dir[1]
35+
if (newX >= 0 && newX < rows && newY >= 0 && newY < cols) {
36+
val waitTime: Int = max((moveTime[newX][newY] - currentTime), 0)
37+
val newTime = currentTime + 1 + waitTime
38+
if (newTime < time[newX][newY]) {
39+
time[newX][newY] = newTime
40+
minHeap.offer(intArrayOf(newTime, newX, newY))
41+
}
42+
}
43+
}
44+
}
45+
return -1
46+
}
47+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
3341\. Find Minimum Time to Reach Last Room I
2+
3+
Medium
4+
5+
There is a dungeon with `n x m` rooms arranged as a grid.
6+
7+
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.
8+
9+
Return the **minimum** time to reach the room `(n - 1, m - 1)`.
10+
11+
Two rooms are **adjacent** if they share a common wall, either _horizontally_ or _vertically_.
12+
13+
**Example 1:**
14+
15+
**Input:** moveTime = [[0,4],[4,4]]
16+
17+
**Output:** 6
18+
19+
**Explanation:**
20+
21+
The minimum time required is 6 seconds.
22+
23+
* At time `t == 4`, move from room `(0, 0)` to room `(1, 0)` in one second.
24+
* At time `t == 5`, move from room `(1, 0)` to room `(1, 1)` in one second.
25+
26+
**Example 2:**
27+
28+
**Input:** moveTime = [[0,0,0],[0,0,0]]
29+
30+
**Output:** 3
31+
32+
**Explanation:**
33+
34+
The minimum time required is 3 seconds.
35+
36+
* At time `t == 0`, move from room `(0, 0)` to room `(1, 0)` in one second.
37+
* At time `t == 1`, move from room `(1, 0)` to room `(1, 1)` in one second.
38+
* At time `t == 2`, move from room `(1, 1)` to room `(1, 2)` in one second.
39+
40+
**Example 3:**
41+
42+
**Input:** moveTime = [[0,1],[1,2]]
43+
44+
**Output:** 3
45+
46+
**Constraints:**
47+
48+
* `2 <= n == moveTime.length <= 50`
49+
* `2 <= m == moveTime[i].length <= 50`
50+
* <code>0 <= moveTime[i][j] <= 10<sup>9</sup></code>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
package g3301_3400.s3342_find_minimum_time_to_reach_last_room_ii
2+
3+
// #Medium #Array #Matrix #Heap_Priority_Queue #Graph #Shortest_Path
4+
// #2024_11_05_Time_122_ms_(100.00%)_Space_136.2_MB_(72.73%)
5+
6+
import java.util.Comparator
7+
import java.util.PriorityQueue
8+
import kotlin.math.max
9+
10+
class Solution {
11+
private class Node {
12+
var x: Int = 0
13+
var y: Int = 0
14+
var t: Int = 0
15+
var turn: Int = 0
16+
}
17+
18+
private val dir = arrayOf<IntArray?>(intArrayOf(1, 0), intArrayOf(-1, 0), intArrayOf(0, 1), intArrayOf(0, -1))
19+
20+
fun minTimeToReach(moveTime: Array<IntArray>): Int {
21+
val pq = PriorityQueue<Node>(Comparator { a: Node, b: Node -> a.t - b.t })
22+
val m = moveTime.size
23+
val n = moveTime[0].size
24+
val node = Node()
25+
node.x = 0
26+
node.y = 0
27+
var t = 0
28+
node.t = t
29+
node.turn = 0
30+
pq.add(node)
31+
moveTime[0][0] = -1
32+
while (pq.isNotEmpty()) {
33+
val curr = pq.poll()
34+
for (i in 0..3) {
35+
val x = curr.x + dir[i]!![0]
36+
val y = curr.y + dir[i]!![1]
37+
if (x == m - 1 && y == n - 1) {
38+
t = max(curr.t, moveTime[x][y]) + 1 + curr.turn
39+
return t
40+
}
41+
if (x >= 0 && x < m && y < n && y >= 0 && moveTime[x][y] != -1) {
42+
val newNode = Node()
43+
t = max(curr.t, moveTime[x][y]) + 1 + curr.turn
44+
newNode.x = x
45+
newNode.y = y
46+
newNode.t = t
47+
newNode.turn = if (curr.turn == 1) 0 else 1
48+
pq.add(newNode)
49+
moveTime[x][y] = -1
50+
}
51+
}
52+
}
53+
return -1
54+
}
55+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
3342\. Find Minimum Time to Reach Last Room II
2+
3+
Medium
4+
5+
There is a dungeon with `n x m` rooms arranged as a grid.
6+
7+
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.
8+
9+
Return the **minimum** time to reach the room `(n - 1, m - 1)`.
10+
11+
Two rooms are **adjacent** if they share a common wall, either _horizontally_ or _vertically_.
12+
13+
**Example 1:**
14+
15+
**Input:** moveTime = [[0,4],[4,4]]
16+
17+
**Output:** 7
18+
19+
**Explanation:**
20+
21+
The minimum time required is 7 seconds.
22+
23+
* At time `t == 4`, move from room `(0, 0)` to room `(1, 0)` in one second.
24+
* At time `t == 5`, move from room `(1, 0)` to room `(1, 1)` in two seconds.
25+
26+
**Example 2:**
27+
28+
**Input:** moveTime = [[0,0,0,0],[0,0,0,0]]
29+
30+
**Output:** 6
31+
32+
**Explanation:**
33+
34+
The minimum time required is 6 seconds.
35+
36+
* At time `t == 0`, move from room `(0, 0)` to room `(1, 0)` in one second.
37+
* At time `t == 1`, move from room `(1, 0)` to room `(1, 1)` in two seconds.
38+
* At time `t == 3`, move from room `(1, 1)` to room `(1, 2)` in one second.
39+
* At time `t == 4`, move from room `(1, 2)` to room `(1, 3)` in two seconds.
40+
41+
**Example 3:**
42+
43+
**Input:** moveTime = [[0,1],[1,2]]
44+
45+
**Output:** 4
46+
47+
**Constraints:**
48+
49+
* `2 <= n == moveTime.length <= 750`
50+
* `2 <= m == moveTime[i].length <= 750`
51+
* <code>0 <= moveTime[i][j] <= 10<sup>9</sup></code>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
package g3301_3400.s3343_count_number_of_balanced_permutations
2+
3+
// #Hard #String #Dynamic_Programming #Math #Combinatorics
4+
// #2024_11_05_Time_66_ms_(100.00%)_Space_38.1_MB_(100.00%)
5+
6+
class Solution {
7+
fun countBalancedPermutations(num: String): Int {
8+
val l = num.length
9+
var ts = 0
10+
val c = IntArray(10)
11+
for (d in num.toCharArray()) {
12+
c[d.code - '0'.code]++
13+
ts += d.code - '0'.code
14+
}
15+
if (ts % 2 != 0) {
16+
return 0
17+
}
18+
val hs = ts / 2
19+
val m = (l + 1) / 2
20+
val f = LongArray(l + 1)
21+
f[0] = 1
22+
for (i in 1..l) {
23+
f[i] = f[i - 1] * i % M
24+
}
25+
val invF = LongArray(l + 1)
26+
invF[l] = modInverse(f[l], M)
27+
for (i in l - 1 downTo 0) {
28+
invF[i] = invF[i + 1] * (i + 1) % M
29+
}
30+
val dp = Array<LongArray?>(m + 1) { LongArray(hs + 1) }
31+
dp[0]!![0] = 1
32+
for (d in 0..9) {
33+
if (c[d] == 0) {
34+
continue
35+
}
36+
for (k in m downTo 0) {
37+
for (s in hs downTo 0) {
38+
if (dp[k]!![s] == 0L) {
39+
continue
40+
}
41+
var t = 1
42+
while (t <= c[d] && k + t <= m && s + d * t <= hs) {
43+
dp[k + t]!![s + d * t] =
44+
(
45+
dp[k + t]!![s + d * t] + dp[k]!![s] * comb(
46+
c[d],
47+
t,
48+
f,
49+
invF,
50+
M
51+
)
52+
) % M
53+
t++
54+
}
55+
}
56+
}
57+
}
58+
val w = dp[m]!![hs]
59+
var r: Long = f[m] * f[l - m] % M
60+
for (d in 0..9) {
61+
r = r * invF[c[d]] % M
62+
}
63+
r = r * w % M
64+
return r.toInt()
65+
}
66+
67+
private fun modInverse(a: Long, m: Int): Long {
68+
var r: Long = 1
69+
var p = m - 2L
70+
var b = a
71+
while (p > 0) {
72+
if ((p and 1L) == 1L) {
73+
r = r * b % m
74+
}
75+
b = b * b % m
76+
p = p shr 1
77+
}
78+
return r
79+
}
80+
81+
private fun comb(n: Int, k: Int, f: LongArray, invF: LongArray, m: Int): Long {
82+
if (k > n) {
83+
return 0
84+
}
85+
return f[n] * invF[k] % m * invF[n - k] % m
86+
}
87+
88+
companion object {
89+
private const val M = 1000000007
90+
}
91+
}

0 commit comments

Comments
 (0)