diff --git a/src/main/kotlin/g3401_3500/s3417_zigzag_grid_traversal_with_skip/Solution.kt b/src/main/kotlin/g3401_3500/s3417_zigzag_grid_traversal_with_skip/Solution.kt new file mode 100644 index 000000000..0a594a1c6 --- /dev/null +++ b/src/main/kotlin/g3401_3500/s3417_zigzag_grid_traversal_with_skip/Solution.kt @@ -0,0 +1,34 @@ +package g3401_3500.s3417_zigzag_grid_traversal_with_skip + +// #Easy #Array #Matrix #Simulation #2025_01_14_Time_2_(100.00%)_Space_43.72_(76.92%) + +class Solution { + fun zigzagTraversal(grid: Array): List { + val ans: MutableList = ArrayList() + val m = grid.size + val n = grid[0].size + var i = 0 + var flag = true + var skip = false + while (i < m) { + if (flag) { + for (j in 0..): Int { + val m = coins.size + val n = coins[0].size + val dp = Array(m) { IntArray(n) } + val dp1 = Array(m) { IntArray(n) } + val dp2 = Array(m) { IntArray(n) } + dp[0][0] = coins[0][0] + for (j in 1..= 0`, the robot gains that many coins. +* If `coins[i][j] < 0`, the robot encounters a robber, and the robber steals the **absolute** value of `coins[i][j]` coins. + +The robot has a special ability to **neutralize robbers** in at most **2 cells** on its path, preventing them from stealing coins in those cells. + +**Note:** The robot's total coins can be negative. + +Return the **maximum** profit the robot can gain on the route. + +**Example 1:** + +**Input:** coins = [[0,1,-1],[1,-2,3],[2,-3,4]] + +**Output:** 8 + +**Explanation:** + +An optimal path for maximum coins is: + +1. Start at `(0, 0)` with `0` coins (total coins = `0`). +2. Move to `(0, 1)`, gaining `1` coin (total coins = `0 + 1 = 1`). +3. Move to `(1, 1)`, where there's a robber stealing `2` coins. The robot uses one neutralization here, avoiding the robbery (total coins = `1`). +4. Move to `(1, 2)`, gaining `3` coins (total coins = `1 + 3 = 4`). +5. Move to `(2, 2)`, gaining `4` coins (total coins = `4 + 4 = 8`). + +**Example 2:** + +**Input:** coins = [[10,10,10],[10,10,10]] + +**Output:** 40 + +**Explanation:** + +An optimal path for maximum coins is: + +1. Start at `(0, 0)` with `10` coins (total coins = `10`). +2. Move to `(0, 1)`, gaining `10` coins (total coins = `10 + 10 = 20`). +3. Move to `(0, 2)`, gaining another `10` coins (total coins = `20 + 10 = 30`). +4. Move to `(1, 2)`, gaining the final `10` coins (total coins = `30 + 10 = 40`). + +**Constraints:** + +* `m == coins.length` +* `n == coins[i].length` +* `1 <= m, n <= 500` +* `-1000 <= coins[i][j] <= 1000` \ No newline at end of file diff --git a/src/main/kotlin/g3401_3500/s3419_minimize_the_maximum_edge_weight_of_graph/Solution.kt b/src/main/kotlin/g3401_3500/s3419_minimize_the_maximum_edge_weight_of_graph/Solution.kt new file mode 100644 index 000000000..704dcfdf5 --- /dev/null +++ b/src/main/kotlin/g3401_3500/s3419_minimize_the_maximum_edge_weight_of_graph/Solution.kt @@ -0,0 +1,52 @@ +package g3401_3500.s3419_minimize_the_maximum_edge_weight_of_graph + +// #Medium #Depth_First_Search #Breadth_First_Search #Binary_Search #Graph #Shortest_Path +// #2025_01_14_Time_88_(100.00%)_Space_115.26_(83.33%) + +import java.util.LinkedList +import java.util.Queue +import kotlin.math.max + +@Suppress("unused") +class Solution { + fun minMaxWeight(n: Int, edges: Array, threshold: Int): Int { + val reversedG: Array?> = arrayOfNulls?>(n) + for (i in 0..() + } + for (i in edges) { + val a = i[0] + val b = i[1] + val w = i[2] + reversedG[b]!!.add(intArrayOf(a, w)) + } + val distance = IntArray(n) + distance.fill(Int.Companion.MAX_VALUE) + distance[0] = 0 + if (reversedG[0]!!.isEmpty()) { + return -1 + } + val que: Queue = LinkedList() + que.add(0) + while (que.isNotEmpty()) { + val cur: Int = que.poll()!! + for (next in reversedG[cur]!!) { + val node = next[0] + val w = next[1] + val nextdis = max(w, distance[cur]) + if (nextdis < distance[node]) { + distance[node] = nextdis + que.add(node) + } + } + } + var ans = 0 + for (i in 0..edges[i] = [Ai, Bi, Wi] indicates that there is an edge going from node Ai to node Bi with weight Wi. + +You have to remove some edges from this graph (possibly **none**), so that it satisfies the following conditions: + +* Node 0 must be reachable from all other nodes. +* The **maximum** edge weight in the resulting graph is **minimized**. +* Each node has **at most** `threshold` outgoing edges. + +Return the **minimum** possible value of the **maximum** edge weight after removing the necessary edges. If it is impossible for all conditions to be satisfied, return -1. + +**Example 1:** + +**Input:** n = 5, edges = [[1,0,1],[2,0,2],[3,0,1],[4,3,1],[2,1,1]], threshold = 2 + +**Output:** 1 + +**Explanation:** + +![](https://assets.leetcode.com/uploads/2024/12/09/s-1.png) + +Remove the edge `2 -> 0`. The maximum weight among the remaining edges is 1. + +**Example 2:** + +**Input:** n = 5, edges = [[0,1,1],[0,2,2],[0,3,1],[0,4,1],[1,2,1],[1,4,1]], threshold = 1 + +**Output:** \-1 + +**Explanation:** + +It is impossible to reach node 0 from node 2. + +**Example 3:** + +**Input:** n = 5, edges = [[1,2,1],[1,3,3],[1,4,5],[2,3,2],[3,4,2],[4,0,1]], threshold = 1 + +**Output:** 2 + +**Explanation:** + +![](https://assets.leetcode.com/uploads/2024/12/09/s2-1.png) + +Remove the edges `1 -> 3` and `1 -> 4`. The maximum weight among the remaining edges is 2. + +**Example 4:** + +**Input:** n = 5, edges = [[1,2,1],[1,3,3],[1,4,5],[2,3,2],[4,0,1]], threshold = 1 + +**Output:** \-1 + +**Constraints:** + +* 2 <= n <= 105 +* `1 <= threshold <= n - 1` +* 1 <= edges.length <= min(105, n * (n - 1) / 2). +* `edges[i].length == 3` +* 0 <= Ai, Bi < n +* Ai != Bi +* 1 <= Wi <= 106 +* There **may be** multiple edges between a pair of nodes, but they must have unique weights. \ No newline at end of file diff --git a/src/main/kotlin/g3401_3500/s3420_count_non_decreasing_subarrays_after_k_operations/Solution.kt b/src/main/kotlin/g3401_3500/s3420_count_non_decreasing_subarrays_after_k_operations/Solution.kt new file mode 100644 index 000000000..6aa270ea0 --- /dev/null +++ b/src/main/kotlin/g3401_3500/s3420_count_non_decreasing_subarrays_after_k_operations/Solution.kt @@ -0,0 +1,47 @@ +package g3401_3500.s3420_count_non_decreasing_subarrays_after_k_operations + +// #Hard #Array #Two_Pointers #Stack #Monotonic_Stack #Queue #Segment_Tree #Monotonic_Queue +// #2025_01_15_Time_28_(100.00%)_Space_68.93_(88.89%) + +class Solution { + fun countNonDecreasingSubarrays(nums: IntArray, k: Int): Long { + val n = nums.size + reverse(nums) + var res: Long = 0 + var t = k.toLong() + val q = IntArray(n + 1) + var hh = 0 + var tt = -1 + var j = 0 + var i = 0 + while (j < n) { + while (hh <= tt && nums[q[tt]] < nums[j]) { + val r = q[tt--] + val l = if (hh <= tt) q[tt] else i - 1 + t -= (r - l).toLong() * (nums[j] - nums[r]) + } + q[++tt] = j + while (t < 0) { + t += (nums[q[hh]] - nums[i]).toLong() + if (q[hh] == i) hh++ + i++ + } + res += (j - i + 1).toLong() + j++ + } + return res + } + + private fun reverse(nums: IntArray) { + val n = nums.size + var i = 0 + var j = n - 1 + while (i < j) { + val t = nums[i] + nums[i] = nums[j] + nums[j] = t + i++ + j-- + } + } +} diff --git a/src/main/kotlin/g3401_3500/s3420_count_non_decreasing_subarrays_after_k_operations/readme.md b/src/main/kotlin/g3401_3500/s3420_count_non_decreasing_subarrays_after_k_operations/readme.md new file mode 100644 index 000000000..46169b39e --- /dev/null +++ b/src/main/kotlin/g3401_3500/s3420_count_non_decreasing_subarrays_after_k_operations/readme.md @@ -0,0 +1,39 @@ +3420\. Count Non-Decreasing Subarrays After K Operations + +Hard + +You are given an array `nums` of `n` integers and an integer `k`. + +For each subarray of `nums`, you can apply **up to** `k` operations on it. In each operation, you increment any element of the subarray by 1. + +**Note** that each subarray is considered independently, meaning changes made to one subarray do not persist to another. + +Return the number of subarrays that you can make **non-decreasing** after performing at most `k` operations. + +An array is said to be **non-decreasing** if each element is greater than or equal to its previous element, if it exists. + +**Example 1:** + +**Input:** nums = [6,3,1,2,4,4], k = 7 + +**Output:** 17 + +**Explanation:** + +Out of all 21 possible subarrays of `nums`, only the subarrays `[6, 3, 1]`, `[6, 3, 1, 2]`, `[6, 3, 1, 2, 4]` and `[6, 3, 1, 2, 4, 4]` cannot be made non-decreasing after applying up to k = 7 operations. Thus, the number of non-decreasing subarrays is `21 - 4 = 17`. + +**Example 2:** + +**Input:** nums = [6,3,1,3,6], k = 4 + +**Output:** 12 + +**Explanation:** + +The subarray `[3, 1, 3, 6]` along with all subarrays of `nums` with three or fewer elements, except `[6, 3, 1]`, can be made non-decreasing after `k` operations. There are 5 subarrays of a single element, 4 subarrays of two elements, and 2 subarrays of three elements except `[6, 3, 1]`, so there are `1 + 5 + 4 + 2 = 12` subarrays that can be made non-decreasing. + +**Constraints:** + +* 1 <= nums.length <= 105 +* 1 <= nums[i] <= 109 +* 1 <= k <= 109 \ No newline at end of file diff --git a/src/test/kotlin/g3401_3500/s3417_zigzag_grid_traversal_with_skip/SolutionTest.kt b/src/test/kotlin/g3401_3500/s3417_zigzag_grid_traversal_with_skip/SolutionTest.kt new file mode 100644 index 000000000..6d4887b0b --- /dev/null +++ b/src/test/kotlin/g3401_3500/s3417_zigzag_grid_traversal_with_skip/SolutionTest.kt @@ -0,0 +1,37 @@ +package g3401_3500.s3417_zigzag_grid_traversal_with_skip + +import org.hamcrest.CoreMatchers.equalTo +import org.hamcrest.MatcherAssert.assertThat +import org.junit.jupiter.api.Test + +internal class SolutionTest { + @Test + fun zigzagTraversal() { + assertThat>( + Solution().zigzagTraversal(arrayOf(intArrayOf(1, 2), intArrayOf(3, 4))), + equalTo>(listOf(1, 4)), + ) + } + + @Test + fun zigzagTraversal2() { + assertThat>( + Solution().zigzagTraversal(arrayOf(intArrayOf(2, 1), intArrayOf(2, 1), intArrayOf(2, 1))), + equalTo>(listOf(2, 1, 2)), + ) + } + + @Test + fun zigzagTraversal3() { + assertThat>( + Solution().zigzagTraversal( + arrayOf( + intArrayOf(1, 2, 3), + intArrayOf(4, 5, 6), + intArrayOf(7, 8, 9), + ), + ), + equalTo>(listOf(1, 3, 5, 7, 9)), + ) + } +} diff --git a/src/test/kotlin/g3401_3500/s3418_maximum_amount_of_money_robot_can_earn/SolutionTest.kt b/src/test/kotlin/g3401_3500/s3418_maximum_amount_of_money_robot_can_earn/SolutionTest.kt new file mode 100644 index 000000000..fd0e8bdae --- /dev/null +++ b/src/test/kotlin/g3401_3500/s3418_maximum_amount_of_money_robot_can_earn/SolutionTest.kt @@ -0,0 +1,29 @@ +package g3401_3500.s3418_maximum_amount_of_money_robot_can_earn + +import org.hamcrest.CoreMatchers.equalTo +import org.hamcrest.MatcherAssert.assertThat +import org.junit.jupiter.api.Test + +internal class SolutionTest { + @Test + fun maximumAmount() { + assertThat( + Solution().maximumAmount( + arrayOf( + intArrayOf(0, 1, -1), + intArrayOf(1, -2, 3), + intArrayOf(2, -3, 4), + ), + ), + equalTo(8), + ) + } + + @Test + fun maximumAmount2() { + assertThat( + Solution().maximumAmount(arrayOf(intArrayOf(10, 10, 10), intArrayOf(10, 10, 10))), + equalTo(40), + ) + } +} diff --git a/src/test/kotlin/g3401_3500/s3419_minimize_the_maximum_edge_weight_of_graph/SolutionTest.kt b/src/test/kotlin/g3401_3500/s3419_minimize_the_maximum_edge_weight_of_graph/SolutionTest.kt new file mode 100644 index 000000000..604a20284 --- /dev/null +++ b/src/test/kotlin/g3401_3500/s3419_minimize_the_maximum_edge_weight_of_graph/SolutionTest.kt @@ -0,0 +1,85 @@ +package g3401_3500.s3419_minimize_the_maximum_edge_weight_of_graph + +import org.hamcrest.CoreMatchers.equalTo +import org.hamcrest.MatcherAssert.assertThat +import org.junit.jupiter.api.Test + +internal class SolutionTest { + @Test + fun minMaxWeight() { + assertThat( + Solution() + .minMaxWeight( + 5, + arrayOf( + intArrayOf(1, 0, 1), + intArrayOf(2, 0, 2), + intArrayOf(3, 0, 1), + intArrayOf(4, 3, 1), + intArrayOf(2, 1, 1), + ), + 2, + ), + equalTo(1), + ) + } + + @Test + fun minMaxWeight2() { + assertThat( + Solution() + .minMaxWeight( + 5, + arrayOf( + intArrayOf(0, 1, 1), + intArrayOf(0, 2, 2), + intArrayOf(0, 3, 1), + intArrayOf(0, 4, 1), + intArrayOf(1, 2, 1), + intArrayOf(1, 4, 1), + ), + 1, + ), + equalTo(-1), + ) + } + + @Test + fun minMaxWeight3() { + assertThat( + Solution() + .minMaxWeight( + 5, + arrayOf( + intArrayOf(1, 2, 1), + intArrayOf(1, 3, 3), + intArrayOf(1, 4, 5), + intArrayOf(2, 3, 2), + intArrayOf(3, 4, 2), + intArrayOf(4, 0, 1), + ), + 1, + ), + equalTo(2), + ) + } + + @Test + fun minMaxWeight4() { + assertThat( + Solution() + .minMaxWeight( + 5, + arrayOf( + intArrayOf(1, 2, 1), + intArrayOf(1, 3, 3), + intArrayOf(1, 4, 5), + intArrayOf(2, 3, 2), + intArrayOf(4, 0, 1), + ), + 1, + ), + equalTo(-1), + ) + } +} diff --git a/src/test/kotlin/g3401_3500/s3420_count_non_decreasing_subarrays_after_k_operations/SolutionTest.kt b/src/test/kotlin/g3401_3500/s3420_count_non_decreasing_subarrays_after_k_operations/SolutionTest.kt new file mode 100644 index 000000000..d1bb05588 --- /dev/null +++ b/src/test/kotlin/g3401_3500/s3420_count_non_decreasing_subarrays_after_k_operations/SolutionTest.kt @@ -0,0 +1,23 @@ +package g3401_3500.s3420_count_non_decreasing_subarrays_after_k_operations + +import org.hamcrest.CoreMatchers.equalTo +import org.hamcrest.MatcherAssert.assertThat +import org.junit.jupiter.api.Test + +internal class SolutionTest { + @Test + fun countNonDecreasingSubarrays() { + assertThat( + Solution().countNonDecreasingSubarrays(intArrayOf(6, 3, 1, 2, 4, 4), 7), + equalTo(17L), + ) + } + + @Test + fun countNonDecreasingSubarrays2() { + assertThat( + Solution().countNonDecreasingSubarrays(intArrayOf(6, 3, 1, 3, 6), 4), + equalTo(12L), + ) + } +}