Skip to content

Added tasks 3340-3343 #715

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Nov 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions src/main/kotlin/g3301_3400/s3340_check_balanced_string/Solution.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package g3301_3400.s3340_check_balanced_string

// #Easy #String #2024_11_05_Time_1_ms_(100.00%)_Space_34.9_MB_(84.38%)

class Solution {
fun isBalanced(num: String): Boolean {
var diff = 0
var sign = 1
val n = num.length
for (i in 0 until n) {
diff += sign * (num[i].code - '0'.code)
sign = -sign
}
return diff == 0
}
}
34 changes: 34 additions & 0 deletions src/main/kotlin/g3301_3400/s3340_check_balanced_string/readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
3340\. Check Balanced String

Easy

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.

Return `true` if `num` is **balanced**, otherwise return `false`.

**Example 1:**

**Input:** num = "1234"

**Output:** false

**Explanation:**

* The sum of digits at even indices is `1 + 3 == 4`, and the sum of digits at odd indices is `2 + 4 == 6`.
* Since 4 is not equal to 6, `num` is not balanced.

**Example 2:**

**Input:** num = "24123"

**Output:** true

**Explanation:**

* The sum of digits at even indices is `2 + 1 + 3 == 6`, and the sum of digits at odd indices is `4 + 2 == 6`.
* Since both are equal the `num` is balanced.

**Constraints:**

* `2 <= num.length <= 100`
* `num` consists of digits only
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package g3301_3400.s3341_find_minimum_time_to_reach_last_room_i

// #Medium #Array #Matrix #Heap_Priority_Queue #Graph #Shortest_Path
// #2024_11_05_Time_257_ms_(42.10%)_Space_46.1_MB_(10.53%)

import java.util.Comparator
import java.util.PriorityQueue
import java.util.function.ToIntFunction
import kotlin.math.max

class Solution {
fun minTimeToReach(moveTime: Array<IntArray>): Int {
val rows = moveTime.size
val cols = moveTime[0].size
val minHeap =
PriorityQueue<IntArray>(Comparator.comparingInt<IntArray>(ToIntFunction { a: IntArray -> a[0] }))
val time: Array<IntArray> = Array<IntArray>(rows) { IntArray(cols) }
for (row in time) {
row.fill(Int.Companion.MAX_VALUE)
}
minHeap.offer(intArrayOf(0, 0, 0))
time[0][0] = 0
val directions = arrayOf<IntArray>(intArrayOf(1, 0), intArrayOf(-1, 0), intArrayOf(0, 1), intArrayOf(0, -1))
while (minHeap.isNotEmpty()) {
val current = minHeap.poll()
val currentTime = current[0]
val x = current[1]
val y = current[2]
if (x == rows - 1 && y == cols - 1) {
return currentTime
}
for (dir in directions) {
val newX = x + dir[0]
val newY = y + dir[1]
if (newX >= 0 && newX < rows && newY >= 0 && newY < cols) {
val waitTime: Int = max((moveTime[newX][newY] - currentTime), 0)
val newTime = currentTime + 1 + waitTime
if (newTime < time[newX][newY]) {
time[newX][newY] = newTime
minHeap.offer(intArrayOf(newTime, newX, newY))
}
}
}
}
return -1
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
3341\. Find Minimum Time to Reach Last Room I

Medium

There is a dungeon with `n x m` rooms arranged as a grid.

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.

Return the **minimum** time to reach the room `(n - 1, m - 1)`.

Two rooms are **adjacent** if they share a common wall, either _horizontally_ or _vertically_.

**Example 1:**

**Input:** moveTime = [[0,4],[4,4]]

**Output:** 6

**Explanation:**

The minimum time required is 6 seconds.

* At time `t == 4`, move from room `(0, 0)` to room `(1, 0)` in one second.
* At time `t == 5`, move from room `(1, 0)` to room `(1, 1)` in one second.

**Example 2:**

**Input:** moveTime = [[0,0,0],[0,0,0]]

**Output:** 3

**Explanation:**

The minimum time required is 3 seconds.

* At time `t == 0`, move from room `(0, 0)` to room `(1, 0)` in one second.
* At time `t == 1`, move from room `(1, 0)` to room `(1, 1)` in one second.
* At time `t == 2`, move from room `(1, 1)` to room `(1, 2)` in one second.

**Example 3:**

**Input:** moveTime = [[0,1],[1,2]]

**Output:** 3

**Constraints:**

* `2 <= n == moveTime.length <= 50`
* `2 <= m == moveTime[i].length <= 50`
* <code>0 <= moveTime[i][j] <= 10<sup>9</sup></code>
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package g3301_3400.s3342_find_minimum_time_to_reach_last_room_ii

// #Medium #Array #Matrix #Heap_Priority_Queue #Graph #Shortest_Path
// #2024_11_05_Time_122_ms_(100.00%)_Space_136.2_MB_(72.73%)

import java.util.Comparator
import java.util.PriorityQueue
import kotlin.math.max

class Solution {
private class Node {
var x: Int = 0
var y: Int = 0
var t: Int = 0
var turn: Int = 0
}

private val dir = arrayOf<IntArray?>(intArrayOf(1, 0), intArrayOf(-1, 0), intArrayOf(0, 1), intArrayOf(0, -1))

fun minTimeToReach(moveTime: Array<IntArray>): Int {
val pq = PriorityQueue<Node>(Comparator { a: Node, b: Node -> a.t - b.t })
val m = moveTime.size
val n = moveTime[0].size
val node = Node()
node.x = 0
node.y = 0
var t = 0
node.t = t
node.turn = 0
pq.add(node)
moveTime[0][0] = -1
while (pq.isNotEmpty()) {
val curr = pq.poll()
for (i in 0..3) {
val x = curr.x + dir[i]!![0]
val y = curr.y + dir[i]!![1]
if (x == m - 1 && y == n - 1) {
t = max(curr.t, moveTime[x][y]) + 1 + curr.turn
return t
}
if (x >= 0 && x < m && y < n && y >= 0 && moveTime[x][y] != -1) {
val newNode = Node()
t = max(curr.t, moveTime[x][y]) + 1 + curr.turn
newNode.x = x
newNode.y = y
newNode.t = t
newNode.turn = if (curr.turn == 1) 0 else 1
pq.add(newNode)
moveTime[x][y] = -1
}
}
}
return -1
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
3342\. Find Minimum Time to Reach Last Room II

Medium

There is a dungeon with `n x m` rooms arranged as a grid.

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.

Return the **minimum** time to reach the room `(n - 1, m - 1)`.

Two rooms are **adjacent** if they share a common wall, either _horizontally_ or _vertically_.

**Example 1:**

**Input:** moveTime = [[0,4],[4,4]]

**Output:** 7

**Explanation:**

The minimum time required is 7 seconds.

* At time `t == 4`, move from room `(0, 0)` to room `(1, 0)` in one second.
* At time `t == 5`, move from room `(1, 0)` to room `(1, 1)` in two seconds.

**Example 2:**

**Input:** moveTime = [[0,0,0,0],[0,0,0,0]]

**Output:** 6

**Explanation:**

The minimum time required is 6 seconds.

* At time `t == 0`, move from room `(0, 0)` to room `(1, 0)` in one second.
* At time `t == 1`, move from room `(1, 0)` to room `(1, 1)` in two seconds.
* At time `t == 3`, move from room `(1, 1)` to room `(1, 2)` in one second.
* At time `t == 4`, move from room `(1, 2)` to room `(1, 3)` in two seconds.

**Example 3:**

**Input:** moveTime = [[0,1],[1,2]]

**Output:** 4

**Constraints:**

* `2 <= n == moveTime.length <= 750`
* `2 <= m == moveTime[i].length <= 750`
* <code>0 <= moveTime[i][j] <= 10<sup>9</sup></code>
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
package g3301_3400.s3343_count_number_of_balanced_permutations

// #Hard #String #Dynamic_Programming #Math #Combinatorics
// #2024_11_05_Time_66_ms_(100.00%)_Space_38.1_MB_(100.00%)

class Solution {
fun countBalancedPermutations(num: String): Int {
val l = num.length
var ts = 0
val c = IntArray(10)
for (d in num.toCharArray()) {
c[d.code - '0'.code]++
ts += d.code - '0'.code
}
if (ts % 2 != 0) {
return 0
}
val hs = ts / 2
val m = (l + 1) / 2
val f = LongArray(l + 1)
f[0] = 1
for (i in 1..l) {
f[i] = f[i - 1] * i % M
}
val invF = LongArray(l + 1)
invF[l] = modInverse(f[l], M)
for (i in l - 1 downTo 0) {
invF[i] = invF[i + 1] * (i + 1) % M
}
val dp = Array<LongArray?>(m + 1) { LongArray(hs + 1) }
dp[0]!![0] = 1
for (d in 0..9) {
if (c[d] == 0) {
continue
}
for (k in m downTo 0) {
for (s in hs downTo 0) {
if (dp[k]!![s] == 0L) {
continue
}
var t = 1
while (t <= c[d] && k + t <= m && s + d * t <= hs) {
dp[k + t]!![s + d * t] =
(
dp[k + t]!![s + d * t] + dp[k]!![s] * comb(
c[d],
t,
f,
invF,
M
)
) % M
t++
}
}
}
}
val w = dp[m]!![hs]
var r: Long = f[m] * f[l - m] % M
for (d in 0..9) {
r = r * invF[c[d]] % M
}
r = r * w % M
return r.toInt()
}

private fun modInverse(a: Long, m: Int): Long {
var r: Long = 1
var p = m - 2L
var b = a
while (p > 0) {
if ((p and 1L) == 1L) {
r = r * b % m
}
b = b * b % m
p = p shr 1
}
return r
}

private fun comb(n: Int, k: Int, f: LongArray, invF: LongArray, m: Int): Long {
if (k > n) {
return 0
}
return f[n] * invF[k] % m * invF[n - k] % m
}

companion object {
private const val M = 1000000007
}
}
Loading