From bbebdc92b5c9f774306c4c46140c7b9ff928ca0b Mon Sep 17 00:00:00 2001 From: Valentyn Kolesnikov Date: Wed, 7 Aug 2024 06:55:26 +0300 Subject: [PATCH] Added tasks 3238-3245 --- .../Solution.kt | 29 ++ .../readme.md | 54 ++++ .../Solution.kt | 27 ++ .../readme.md | 52 ++++ .../Solution.kt | 51 ++++ .../readme.md | 48 ++++ .../Solution.kt | 95 +++++++ .../readme.md | 64 +++++ .../NeighborSum.kt | 73 +++++ .../readme.md | 57 ++++ .../Solution.kt | 44 +++ .../readme.md | 56 ++++ .../Solution.kt | 31 +++ .../readme.md | 59 ++++ .../s3245_alternating_groups_iii/Solution.kt | 260 ++++++++++++++++++ .../s3245_alternating_groups_iii/readme.md | 73 +++++ .../SolutionTest.kt | 48 ++++ .../SolutionTest.kt | 28 ++ .../SolutionTest.kt | 28 ++ .../SolutionTest.kt | 31 +++ .../SolutionTest.kt | 31 +++ .../SolutionTest.kt | 24 ++ .../SolutionTest.kt | 24 ++ .../SolutionTest.kt | 30 ++ 24 files changed, 1317 insertions(+) create mode 100644 src/main/kotlin/g3201_3300/s3238_find_the_number_of_winning_players/Solution.kt create mode 100644 src/main/kotlin/g3201_3300/s3238_find_the_number_of_winning_players/readme.md create mode 100644 src/main/kotlin/g3201_3300/s3239_minimum_number_of_flips_to_make_binary_grid_palindromic_i/Solution.kt create mode 100644 src/main/kotlin/g3201_3300/s3239_minimum_number_of_flips_to_make_binary_grid_palindromic_i/readme.md create mode 100644 src/main/kotlin/g3201_3300/s3240_minimum_number_of_flips_to_make_binary_grid_palindromic_ii/Solution.kt create mode 100644 src/main/kotlin/g3201_3300/s3240_minimum_number_of_flips_to_make_binary_grid_palindromic_ii/readme.md create mode 100644 src/main/kotlin/g3201_3300/s3241_time_taken_to_mark_all_nodes/Solution.kt create mode 100644 src/main/kotlin/g3201_3300/s3241_time_taken_to_mark_all_nodes/readme.md create mode 100644 src/main/kotlin/g3201_3300/s3242_design_neighbor_sum_service/NeighborSum.kt create mode 100644 src/main/kotlin/g3201_3300/s3242_design_neighbor_sum_service/readme.md create mode 100644 src/main/kotlin/g3201_3300/s3243_shortest_distance_after_road_addition_queries_i/Solution.kt create mode 100644 src/main/kotlin/g3201_3300/s3243_shortest_distance_after_road_addition_queries_i/readme.md create mode 100644 src/main/kotlin/g3201_3300/s3244_shortest_distance_after_road_addition_queries_ii/Solution.kt create mode 100644 src/main/kotlin/g3201_3300/s3244_shortest_distance_after_road_addition_queries_ii/readme.md create mode 100644 src/main/kotlin/g3201_3300/s3245_alternating_groups_iii/Solution.kt create mode 100644 src/main/kotlin/g3201_3300/s3245_alternating_groups_iii/readme.md create mode 100644 src/test/kotlin/g3201_3300/s3238_find_the_number_of_winning_players/SolutionTest.kt create mode 100644 src/test/kotlin/g3201_3300/s3239_minimum_number_of_flips_to_make_binary_grid_palindromic_i/SolutionTest.kt create mode 100644 src/test/kotlin/g3201_3300/s3240_minimum_number_of_flips_to_make_binary_grid_palindromic_ii/SolutionTest.kt create mode 100644 src/test/kotlin/g3201_3300/s3241_time_taken_to_mark_all_nodes/SolutionTest.kt create mode 100644 src/test/kotlin/g3201_3300/s3242_design_neighbor_sum_service/SolutionTest.kt create mode 100644 src/test/kotlin/g3201_3300/s3243_shortest_distance_after_road_addition_queries_i/SolutionTest.kt create mode 100644 src/test/kotlin/g3201_3300/s3244_shortest_distance_after_road_addition_queries_ii/SolutionTest.kt create mode 100644 src/test/kotlin/g3201_3300/s3245_alternating_groups_iii/SolutionTest.kt diff --git a/src/main/kotlin/g3201_3300/s3238_find_the_number_of_winning_players/Solution.kt b/src/main/kotlin/g3201_3300/s3238_find_the_number_of_winning_players/Solution.kt new file mode 100644 index 000000000..cb4b35015 --- /dev/null +++ b/src/main/kotlin/g3201_3300/s3238_find_the_number_of_winning_players/Solution.kt @@ -0,0 +1,29 @@ +package g3201_3300.s3238_find_the_number_of_winning_players + +// #Easy #Array #Hash_Table #Counting #2024_08_07_Time_207_ms_(90.38%)_Space_42_MB_(75.00%) + +@Suppress("UNUSED_PARAMETER") +class Solution { + fun winningPlayerCount(n: Int, pick: Array): Int { + val dp = Array(11) { IntArray(11) } + for (ints in pick) { + val p = ints[0] + val pi = ints[1] + dp[p][pi] += 1 + } + var count = 0 + for (i in 0..10) { + var win = false + for (j in 0..10) { + if (dp[i][j] >= i + 1) { + win = true + break + } + } + if (win) { + count += 1 + } + } + return count + } +} diff --git a/src/main/kotlin/g3201_3300/s3238_find_the_number_of_winning_players/readme.md b/src/main/kotlin/g3201_3300/s3238_find_the_number_of_winning_players/readme.md new file mode 100644 index 000000000..a42ca5743 --- /dev/null +++ b/src/main/kotlin/g3201_3300/s3238_find_the_number_of_winning_players/readme.md @@ -0,0 +1,54 @@ +3238\. Find the Number of Winning Players + +Easy + +You are given an integer `n` representing the number of players in a game and a 2D array `pick` where pick[i] = [xi, yi] represents that the player xi picked a ball of color yi. + +Player `i` **wins** the game if they pick **strictly more** than `i` balls of the **same** color. In other words, + +* Player 0 wins if they pick any ball. +* Player 1 wins if they pick at least two balls of the _same_ color. +* ... +* Player `i` wins if they pick at least`i + 1` balls of the _same_ color. + +Return the number of players who **win** the game. + +**Note** that _multiple_ players can win the game. + +**Example 1:** + +**Input:** n = 4, pick = [[0,0],[1,0],[1,0],[2,1],[2,1],[2,0]] + +**Output:** 2 + +**Explanation:** + +Player 0 and player 1 win the game, while players 2 and 3 do not win. + +**Example 2:** + +**Input:** n = 5, pick = [[1,1],[1,2],[1,3],[1,4]] + +**Output:** 0 + +**Explanation:** + +No player wins the game. + +**Example 3:** + +**Input:** n = 5, pick = [[1,1],[2,4],[2,4],[2,4]] + +**Output:** 1 + +**Explanation:** + +Player 2 wins the game by picking 3 balls with color 4. + +**Constraints:** + +* `2 <= n <= 10` +* `1 <= pick.length <= 100` +* `pick[i].length == 2` +* 0 <= xi <= n - 1 +* 0 <= yi <= 10 \ No newline at end of file diff --git a/src/main/kotlin/g3201_3300/s3239_minimum_number_of_flips_to_make_binary_grid_palindromic_i/Solution.kt b/src/main/kotlin/g3201_3300/s3239_minimum_number_of_flips_to_make_binary_grid_palindromic_i/Solution.kt new file mode 100644 index 000000000..51f92d2c3 --- /dev/null +++ b/src/main/kotlin/g3201_3300/s3239_minimum_number_of_flips_to_make_binary_grid_palindromic_i/Solution.kt @@ -0,0 +1,27 @@ +package g3201_3300.s3239_minimum_number_of_flips_to_make_binary_grid_palindromic_i + +// #Medium #Array #Matrix #Two_Pointers #2024_08_07_Time_856_ms_(87.50%)_Space_109.2_MB_(66.67%) + +import kotlin.math.min + +class Solution { + fun minFlips(grid: Array): Int { + val m = grid.size + val n = grid[0].size + var rowFlips = 0 + for (i in 0 until m / 2) { + for (j in 0 until n) { + val sum = grid[i][j] + grid[m - 1 - i][j] + rowFlips += min(sum, (2 - sum)) + } + } + var columnFlips = 0 + for (j in 0 until n / 2) { + for (ints in grid) { + val sum = ints[j] + ints[n - 1 - j] + columnFlips += min(sum, (2 - sum)) + } + } + return min(rowFlips, columnFlips) + } +} diff --git a/src/main/kotlin/g3201_3300/s3239_minimum_number_of_flips_to_make_binary_grid_palindromic_i/readme.md b/src/main/kotlin/g3201_3300/s3239_minimum_number_of_flips_to_make_binary_grid_palindromic_i/readme.md new file mode 100644 index 000000000..0e89c8faa --- /dev/null +++ b/src/main/kotlin/g3201_3300/s3239_minimum_number_of_flips_to_make_binary_grid_palindromic_i/readme.md @@ -0,0 +1,52 @@ +3239\. Minimum Number of Flips to Make Binary Grid Palindromic I + +Medium + +You are given an `m x n` binary matrix `grid`. + +A row or column is considered **palindromic** if its values read the same forward and backward. + +You can **flip** any number of cells in `grid` from `0` to `1`, or from `1` to `0`. + +Return the **minimum** number of cells that need to be flipped to make **either** all rows **palindromic** or all columns **palindromic**. + +**Example 1:** + +**Input:** grid = [[1,0,0],[0,0,0],[0,0,1]] + +**Output:** 2 + +**Explanation:** + +![](https://assets.leetcode.com/uploads/2024/07/07/screenshot-from-2024-07-08-00-20-10.png) + +Flipping the highlighted cells makes all the rows palindromic. + +**Example 2:** + +**Input:** grid = [[0,1],[0,1],[0,0]] + +**Output:** 1 + +**Explanation:** + +![](https://assets.leetcode.com/uploads/2024/07/07/screenshot-from-2024-07-08-00-31-23.png) + +Flipping the highlighted cell makes all the columns palindromic. + +**Example 3:** + +**Input:** grid = [[1],[0]] + +**Output:** 0 + +**Explanation:** + +All rows are already palindromic. + +**Constraints:** + +* `m == grid.length` +* `n == grid[i].length` +* 1 <= m * n <= 2 * 105 +* `0 <= grid[i][j] <= 1` \ No newline at end of file diff --git a/src/main/kotlin/g3201_3300/s3240_minimum_number_of_flips_to_make_binary_grid_palindromic_ii/Solution.kt b/src/main/kotlin/g3201_3300/s3240_minimum_number_of_flips_to_make_binary_grid_palindromic_ii/Solution.kt new file mode 100644 index 000000000..bd0b1e67f --- /dev/null +++ b/src/main/kotlin/g3201_3300/s3240_minimum_number_of_flips_to_make_binary_grid_palindromic_ii/Solution.kt @@ -0,0 +1,51 @@ +package g3201_3300.s3240_minimum_number_of_flips_to_make_binary_grid_palindromic_ii + +// #Medium #Array #Matrix #Two_Pointers #2024_08_07_Time_805_ms_(100.00%)_Space_99_MB_(100.00%) + +import kotlin.math.min + +class Solution { + fun minFlips(grid: Array): Int { + var res = 0 + var one = 0 + var diff = 0 + val m = grid.size + val n = grid[0].size + // Handle quadrants + for (i in 0 until m / 2) { + for (j in 0 until n / 2) { + val v = + ( + grid[i][j] + + grid[i][n - 1 - j] + + grid[m - 1 - i][j] + + grid[m - 1 - i][n - 1 - j] + ) + res += min(v, (4 - v)) + } + } + // Handle middle column + if (n % 2 > 0) { + for (i in 0 until m / 2) { + diff += grid[i][n / 2] xor grid[m - 1 - i][n / 2] + one += grid[i][n / 2] + grid[m - 1 - i][n / 2] + } + } + // Handle middle row + if (m % 2 > 0) { + for (j in 0 until n / 2) { + diff += grid[m / 2][j] xor grid[m / 2][n - 1 - j] + one += grid[m / 2][j] + grid[m / 2][n - 1 - j] + } + } + // Handle center point + if (n % 2 > 0 && m % 2 > 0) { + res += grid[m / 2][n / 2] + } + // Divisible by 4 + if (diff == 0 && one % 4 > 0) { + res += 2 + } + return res + diff + } +} diff --git a/src/main/kotlin/g3201_3300/s3240_minimum_number_of_flips_to_make_binary_grid_palindromic_ii/readme.md b/src/main/kotlin/g3201_3300/s3240_minimum_number_of_flips_to_make_binary_grid_palindromic_ii/readme.md new file mode 100644 index 000000000..1d1a9765c --- /dev/null +++ b/src/main/kotlin/g3201_3300/s3240_minimum_number_of_flips_to_make_binary_grid_palindromic_ii/readme.md @@ -0,0 +1,48 @@ +3240\. Minimum Number of Flips to Make Binary Grid Palindromic II + +Medium + +You are given an `m x n` binary matrix `grid`. + +A row or column is considered **palindromic** if its values read the same forward and backward. + +You can **flip** any number of cells in `grid` from `0` to `1`, or from `1` to `0`. + +Return the **minimum** number of cells that need to be flipped to make **all** rows and columns **palindromic**, and the total number of `1`'s in `grid` **divisible** by `4`. + +**Example 1:** + +**Input:** grid = [[1,0,0],[0,1,0],[0,0,1]] + +**Output:** 3 + +**Explanation:** + +![](https://assets.leetcode.com/uploads/2024/08/01/image.png) + +**Example 2:** + +**Input:** grid = [[0,1],[0,1],[0,0]] + +**Output:** 2 + +**Explanation:** + +![](https://assets.leetcode.com/uploads/2024/07/08/screenshot-from-2024-07-09-01-37-48.png) + +**Example 3:** + +**Input:** grid = [[1],[1]] + +**Output:** 2 + +**Explanation:** + +![](https://assets.leetcode.com/uploads/2024/08/01/screenshot-from-2024-08-01-23-05-26.png) + +**Constraints:** + +* `m == grid.length` +* `n == grid[i].length` +* 1 <= m * n <= 2 * 105 +* `0 <= grid[i][j] <= 1` \ No newline at end of file diff --git a/src/main/kotlin/g3201_3300/s3241_time_taken_to_mark_all_nodes/Solution.kt b/src/main/kotlin/g3201_3300/s3241_time_taken_to_mark_all_nodes/Solution.kt new file mode 100644 index 000000000..c4a75eb8b --- /dev/null +++ b/src/main/kotlin/g3201_3300/s3241_time_taken_to_mark_all_nodes/Solution.kt @@ -0,0 +1,95 @@ +package g3201_3300.s3241_time_taken_to_mark_all_nodes + +// #Hard #Dynamic_Programming #Depth_First_Search #Tree #Graph +// #2024_08_07_Time_1228_ms_(100.00%)_Space_108.5_MB_(100.00%) + +import kotlin.math.max + +class Solution { + private lateinit var head: IntArray + private lateinit var nxt: IntArray + private lateinit var to: IntArray + private lateinit var last: IntArray + private lateinit var lastNo: IntArray + private lateinit var second: IntArray + private lateinit var ans: IntArray + + fun timeTaken(edges: Array): IntArray { + val n = edges.size + 1 + head = IntArray(n) + nxt = IntArray(n shl 1) + to = IntArray(n shl 1) + head.fill(-1) + var i = 0 + var j = 2 + while (i < edges.size) { + val u = edges[i][0] + val v = edges[i][1] + nxt[j] = head[u] + head[u] = j + to[j] = v + j++ + nxt[j] = head[v] + head[v] = j + to[j] = u + j++ + i++ + } + last = IntArray(n) + lastNo = IntArray(n) + second = IntArray(n) + ans = IntArray(n) + dfs(-1, 0) + System.arraycopy(last, 0, ans, 0, n) + dfs2(-1, 0, 0) + return ans + } + + private fun dfs2(f: Int, u: Int, preLast: Int) { + var e = head[u] + var v: Int + while (e != -1) { + v = to[e] + if (f != v) { + val pl = if (v == lastNo[u]) { + ( + max( + preLast, + second[u] + ) + (if ((u and 1) == 0) 2 else 1) + ) + } else { + ( + max( + preLast, + last[u] + ) + (if ((u and 1) == 0) 2 else 1) + ) + } + ans[v] = max(ans[v], pl) + dfs2(u, v, pl) + } + e = nxt[e] + } + } + + private fun dfs(f: Int, u: Int) { + var e = head[u] + var v: Int + while (e != -1) { + v = to[e] + if (f != v) { + dfs(u, v) + val t = last[v] + (if ((v and 1) == 0) 2 else 1) + if (last[u] < t) { + second[u] = last[u] + last[u] = t + lastNo[u] = v + } else if (second[u] < t) { + second[u] = t + } + } + e = nxt[e] + } + } +} diff --git a/src/main/kotlin/g3201_3300/s3241_time_taken_to_mark_all_nodes/readme.md b/src/main/kotlin/g3201_3300/s3241_time_taken_to_mark_all_nodes/readme.md new file mode 100644 index 000000000..a64e0c6eb --- /dev/null +++ b/src/main/kotlin/g3201_3300/s3241_time_taken_to_mark_all_nodes/readme.md @@ -0,0 +1,64 @@ +3241\. Time Taken to Mark All Nodes + +Hard + +There exists an **undirected** tree with `n` nodes numbered `0` to `n - 1`. You are given a 2D integer array `edges` of length `n - 1`, where edges[i] = [ui, vi] indicates that there is an edge between nodes ui and vi in the tree. + +Initially, **all** nodes are **unmarked**. For each node `i`: + +* If `i` is odd, the node will get marked at time `x` if there is **at least** one node _adjacent_ to it which was marked at time `x - 1`. +* If `i` is even, the node will get marked at time `x` if there is **at least** one node _adjacent_ to it which was marked at time `x - 2`. + +Return an array `times` where `times[i]` is the time when all nodes get marked in the tree, if you mark node `i` at time `t = 0`. + +**Note** that the answer for each `times[i]` is **independent**, i.e. when you mark node `i` all other nodes are _unmarked_. + +**Example 1:** + +**Input:** edges = [[0,1],[0,2]] + +**Output:** [2,4,3] + +**Explanation:** + +![](https://assets.leetcode.com/uploads/2024/06/01/screenshot-2024-06-02-122236.png) + +* For `i = 0`: + * Node 1 is marked at `t = 1`, and Node 2 at `t = 2`. +* For `i = 1`: + * Node 0 is marked at `t = 2`, and Node 2 at `t = 4`. +* For `i = 2`: + * Node 0 is marked at `t = 2`, and Node 1 at `t = 3`. + +**Example 2:** + +**Input:** edges = [[0,1]] + +**Output:** [1,2] + +**Explanation:** + +![](https://assets.leetcode.com/uploads/2024/06/01/screenshot-2024-06-02-122249.png) + +* For `i = 0`: + * Node 1 is marked at `t = 1`. +* For `i = 1`: + * Node 0 is marked at `t = 2`. + +**Example 3:** + +**Input:** edges = [[2,4],[0,1],[2,3],[0,2]] + +**Output:** [4,6,3,5,5] + +**Explanation:** + +![](https://assets.leetcode.com/uploads/2024/06/03/screenshot-2024-06-03-210550.png) + +**Constraints:** + +* 2 <= n <= 105 +* `edges.length == n - 1` +* `edges[i].length == 2` +* `0 <= edges[i][0], edges[i][1] <= n - 1` +* The input is generated such that `edges` represents a valid tree. \ No newline at end of file diff --git a/src/main/kotlin/g3201_3300/s3242_design_neighbor_sum_service/NeighborSum.kt b/src/main/kotlin/g3201_3300/s3242_design_neighbor_sum_service/NeighborSum.kt new file mode 100644 index 000000000..ad4d386ea --- /dev/null +++ b/src/main/kotlin/g3201_3300/s3242_design_neighbor_sum_service/NeighborSum.kt @@ -0,0 +1,73 @@ +package g3201_3300.s3242_design_neighbor_sum_service + +// #Easy #Array #Hash_Table #Matrix #Design #Simulation +// #2024_08_07_Time_333_ms_(75.00%)_Space_49.4_MB_(81.25%) + +class NeighborSum(private val grid: Array) { + private val n = grid.size + private val rowIndex = IntArray(n * n) + private val colIndex = IntArray(n * n) + + init { + // Precompute the positions of each value in the grid for quick access + for (i in 0 until n) { + for (j in 0 until n) { + rowIndex[grid[i][j]] = i + colIndex[grid[i][j]] = j + } + } + } + + fun adjacentSum(value: Int): Int { + var sum = 0 + val i = rowIndex[value] + val j = colIndex[value] + // Check up + if (i > 0) { + sum += grid[i - 1][j] + } + // Check down + if (i < n - 1) { + sum += grid[i + 1][j] + } + // Check left + if (j > 0) { + sum += grid[i][j - 1] + } + // Check right + if (j < n - 1) { + sum += grid[i][j + 1] + } + return sum + } + + fun diagonalSum(value: Int): Int { + var sum = 0 + val i = rowIndex[value] + val j = colIndex[value] + // Check top-left + if (i > 0 && j > 0) { + sum += grid[i - 1][j - 1] + } + // Check top-right + if (i > 0 && j < n - 1) { + sum += grid[i - 1][j + 1] + } + // Check bottom-left + if (i < n - 1 && j > 0) { + sum += grid[i + 1][j - 1] + } + // Check bottom-right + if (i < n - 1 && j < n - 1) { + sum += grid[i + 1][j + 1] + } + return sum + } +} + +/* + * Your neighborSum object will be instantiated and called as such: + * var obj = neighborSum(grid) + * var param_1 = obj.adjacentSum(value) + * var param_2 = obj.diagonalSum(value) + */ diff --git a/src/main/kotlin/g3201_3300/s3242_design_neighbor_sum_service/readme.md b/src/main/kotlin/g3201_3300/s3242_design_neighbor_sum_service/readme.md new file mode 100644 index 000000000..e84038b26 --- /dev/null +++ b/src/main/kotlin/g3201_3300/s3242_design_neighbor_sum_service/readme.md @@ -0,0 +1,57 @@ +3242\. Design Neighbor Sum Service + +Easy + +You are given a `n x n` 2D array `grid` containing **distinct** elements in the range [0, n2 - 1]. + +Implement the `neighborSum` class: + +* `neighborSum(int [][]grid)` initializes the object. +* `int adjacentSum(int value)` returns the **sum** of elements which are adjacent neighbors of `value`, that is either to the top, left, right, or bottom of `value` in `grid`. +* `int diagonalSum(int value)` returns the **sum** of elements which are diagonal neighbors of `value`, that is either to the top-left, top-right, bottom-left, or bottom-right of `value` in `grid`. + +![](https://assets.leetcode.com/uploads/2024/06/24/design.png) + +**Example 1:** + +**Input:** + +["neighborSum", "adjacentSum", "adjacentSum", "diagonalSum", "diagonalSum"] + +[[[[0, 1, 2], [3, 4, 5], [6, 7, 8]]], [1], [4], [4], [8]] + +**Output:** [null, 6, 16, 16, 4] + +**Explanation:** + +**![](https://assets.leetcode.com/uploads/2024/06/24/designexample0.png)** + +* The adjacent neighbors of 1 are 0, 2, and 4. +* The adjacent neighbors of 4 are 1, 3, 5, and 7. +* The diagonal neighbors of 4 are 0, 2, 6, and 8. +* The diagonal neighbor of 8 is 4. + +**Example 2:** + +**Input:** + +["neighborSum", "adjacentSum", "diagonalSum"] + +[[[[1, 2, 0, 3], [4, 7, 15, 6], [8, 9, 10, 11], [12, 13, 14, 5]]], [15], [9]] + +**Output:** [null, 23, 45] + +**Explanation:** + +**![](https://assets.leetcode.com/uploads/2024/06/24/designexample2.png)** + +* The adjacent neighbors of 15 are 0, 10, 7, and 6. +* The diagonal neighbors of 9 are 4, 12, 14, and 15. + +**Constraints:** + +* `3 <= n == grid.length == grid[0].length <= 10` +* 0 <= grid[i][j] <= n2 - 1 +* All `grid[i][j]` are distinct. +* `value` in `adjacentSum` and `diagonalSum` will be in the range [0, n2 - 1]. +* At most 2 * n2 calls will be made to `adjacentSum` and `diagonalSum`. \ No newline at end of file diff --git a/src/main/kotlin/g3201_3300/s3243_shortest_distance_after_road_addition_queries_i/Solution.kt b/src/main/kotlin/g3201_3300/s3243_shortest_distance_after_road_addition_queries_i/Solution.kt new file mode 100644 index 000000000..3ce6fe2d7 --- /dev/null +++ b/src/main/kotlin/g3201_3300/s3243_shortest_distance_after_road_addition_queries_i/Solution.kt @@ -0,0 +1,44 @@ +package g3201_3300.s3243_shortest_distance_after_road_addition_queries_i + +// #Medium #Array #Breadth_First_Search #Graph +// #2024_08_07_Time_313_ms_(97.06%)_Space_46.3_MB_(97.06%) + +class Solution { + fun shortestDistanceAfterQueries(n: Int, queries: Array): IntArray { + val dist = IntArray(n) + for (i in 0 until n) { + dist[i] = i + } + val parent: Array> = Array(n) { ArrayList() } + for (i in 0 until n) { + parent[i] = ArrayList() + if (i != n - 1) { + parent[i].add(i + 1) + } + } + val ans = IntArray(queries.size) + for (i in queries.indices) { + val u = queries[i][0] + val v = queries[i][1] + if (dist[v] > dist[u] + 1) { + dist[v] = dist[u] + 1 + parent[u].add(v) + updateDistance(v, dist, parent) + } else { + parent[u].add(v) + } + ans[i] = dist[n - 1] + } + return ans + } + + fun updateDistance(par: Int, dist: IntArray, parent: Array>) { + for (i in parent[par].indices) { + val child = parent[par][i] + if (dist[child] > dist[par] + 1) { + dist[child] = dist[par] + 1 + updateDistance(child, dist, parent) + } + } + } +} diff --git a/src/main/kotlin/g3201_3300/s3243_shortest_distance_after_road_addition_queries_i/readme.md b/src/main/kotlin/g3201_3300/s3243_shortest_distance_after_road_addition_queries_i/readme.md new file mode 100644 index 000000000..c1eab87a2 --- /dev/null +++ b/src/main/kotlin/g3201_3300/s3243_shortest_distance_after_road_addition_queries_i/readme.md @@ -0,0 +1,56 @@ +3243\. Shortest Distance After Road Addition Queries I + +Medium + +You are given an integer `n` and a 2D integer array `queries`. + +There are `n` cities numbered from `0` to `n - 1`. Initially, there is a **unidirectional** road from city `i` to city `i + 1` for all `0 <= i < n - 1`. + +queries[i] = [ui, vi] represents the addition of a new **unidirectional** road from city ui to city vi. After each query, you need to find the **length** of the **shortest path** from city `0` to city `n - 1`. + +Return an array `answer` where for each `i` in the range `[0, queries.length - 1]`, `answer[i]` is the _length of the shortest path_ from city `0` to city `n - 1` after processing the **first** `i + 1` queries. + +**Example 1:** + +**Input:** n = 5, queries = [[2,4],[0,2],[0,4]] + +**Output:** [3,2,1] + +**Explanation:** + +![](https://assets.leetcode.com/uploads/2024/06/28/image8.jpg) + +After the addition of the road from 2 to 4, the length of the shortest path from 0 to 4 is 3. + +![](https://assets.leetcode.com/uploads/2024/06/28/image9.jpg) + +After the addition of the road from 0 to 2, the length of the shortest path from 0 to 4 is 2. + +![](https://assets.leetcode.com/uploads/2024/06/28/image10.jpg) + +After the addition of the road from 0 to 4, the length of the shortest path from 0 to 4 is 1. + +**Example 2:** + +**Input:** n = 4, queries = [[0,3],[0,2]] + +**Output:** [1,1] + +**Explanation:** + +![](https://assets.leetcode.com/uploads/2024/06/28/image11.jpg) + +After the addition of the road from 0 to 3, the length of the shortest path from 0 to 3 is 1. + +![](https://assets.leetcode.com/uploads/2024/06/28/image12.jpg) + +After the addition of the road from 0 to 2, the length of the shortest path remains 1. + +**Constraints:** + +* `3 <= n <= 500` +* `1 <= queries.length <= 500` +* `queries[i].length == 2` +* `0 <= queries[i][0] < queries[i][1] < n` +* `1 < queries[i][1] - queries[i][0]` +* There are no repeated roads among the queries. \ No newline at end of file diff --git a/src/main/kotlin/g3201_3300/s3244_shortest_distance_after_road_addition_queries_ii/Solution.kt b/src/main/kotlin/g3201_3300/s3244_shortest_distance_after_road_addition_queries_ii/Solution.kt new file mode 100644 index 000000000..8723a1ed6 --- /dev/null +++ b/src/main/kotlin/g3201_3300/s3244_shortest_distance_after_road_addition_queries_ii/Solution.kt @@ -0,0 +1,31 @@ +package g3201_3300.s3244_shortest_distance_after_road_addition_queries_ii + +// #Hard #Array #Greedy #Graph #Ordered_Set #2024_08_07_Time_794_ms_(92.31%)_Space_109_MB_(15.38%) + +class Solution { + fun shortestDistanceAfterQueries(n: Int, queries: Array): IntArray { + val flag = IntArray(n) + val res = IntArray(queries.size) + for (i in 0 until n) { + flag[i] = i + 1 + } + for (k in queries.indices) { + val query = queries[k] + val preRes = if (k == 0) (n - 1) else res[k - 1] + if (flag[query[0]] >= query[1]) { + res[k] = preRes + continue + } + var subDis = 0 + var curr = query[0] + while (curr < query[1]) { + val next = flag[curr] + subDis += 1 + flag[curr] = query[1] + curr = next + } + res[k] = preRes + 1 - subDis + } + return res + } +} diff --git a/src/main/kotlin/g3201_3300/s3244_shortest_distance_after_road_addition_queries_ii/readme.md b/src/main/kotlin/g3201_3300/s3244_shortest_distance_after_road_addition_queries_ii/readme.md new file mode 100644 index 000000000..4d6f354f9 --- /dev/null +++ b/src/main/kotlin/g3201_3300/s3244_shortest_distance_after_road_addition_queries_ii/readme.md @@ -0,0 +1,59 @@ +3244\. Shortest Distance After Road Addition Queries II + +Hard + +You are given an integer `n` and a 2D integer array `queries`. + +There are `n` cities numbered from `0` to `n - 1`. Initially, there is a **unidirectional** road from city `i` to city `i + 1` for all `0 <= i < n - 1`. + +queries[i] = [ui, vi] represents the addition of a new **unidirectional** road from city ui to city vi. After each query, you need to find the **length** of the **shortest path** from city `0` to city `n - 1`. + +There are no two queries such that `queries[i][0] < queries[j][0] < queries[i][1] < queries[j][1]`. + +Return an array `answer` where for each `i` in the range `[0, queries.length - 1]`, `answer[i]` is the _length of the shortest path_ from city `0` to city `n - 1` after processing the **first** `i + 1` queries. + +**Example 1:** + +**Input:** n = 5, queries = [[2,4],[0,2],[0,4]] + +**Output:** [3,2,1] + +**Explanation:** + +![](https://assets.leetcode.com/uploads/2024/06/28/image8.jpg) + +After the addition of the road from 2 to 4, the length of the shortest path from 0 to 4 is 3. + +![](https://assets.leetcode.com/uploads/2024/06/28/image9.jpg) + +After the addition of the road from 0 to 2, the length of the shortest path from 0 to 4 is 2. + +![](https://assets.leetcode.com/uploads/2024/06/28/image10.jpg) + +After the addition of the road from 0 to 4, the length of the shortest path from 0 to 4 is 1. + +**Example 2:** + +**Input:** n = 4, queries = [[0,3],[0,2]] + +**Output:** [1,1] + +**Explanation:** + +![](https://assets.leetcode.com/uploads/2024/06/28/image11.jpg) + +After the addition of the road from 0 to 3, the length of the shortest path from 0 to 3 is 1. + +![](https://assets.leetcode.com/uploads/2024/06/28/image12.jpg) + +After the addition of the road from 0 to 2, the length of the shortest path remains 1. + +**Constraints:** + +* 3 <= n <= 105 +* 1 <= queries.length <= 105 +* `queries[i].length == 2` +* `0 <= queries[i][0] < queries[i][1] < n` +* `1 < queries[i][1] - queries[i][0]` +* There are no repeated roads among the queries. +* There are no two queries such that `i != j` and `queries[i][0] < queries[j][0] < queries[i][1] < queries[j][1]`. \ No newline at end of file diff --git a/src/main/kotlin/g3201_3300/s3245_alternating_groups_iii/Solution.kt b/src/main/kotlin/g3201_3300/s3245_alternating_groups_iii/Solution.kt new file mode 100644 index 000000000..6dbff5331 --- /dev/null +++ b/src/main/kotlin/g3201_3300/s3245_alternating_groups_iii/Solution.kt @@ -0,0 +1,260 @@ +package g3201_3300.s3245_alternating_groups_iii + +// #Hard #Array #Binary_Indexed_Tree #2024_08_07_Time_1072_ms_(100.00%)_Space_97.6_MB_(100.00%) + +@Suppress("NAME_SHADOWING") +class Solution { + private fun go(ind: Int, lst: LST, fs: IntArray, n: Int, ff: LST, c: IntArray) { + if (ind > 0) { + val pre = lst.prev(ind - 1) + var nex = lst.next(pre + 1) + if (nex == -1) { + nex = 2 * n + } + if (pre != -1 && pre < n && --fs[nex - pre] == 0) { + ff.unsetPos(nex - pre) + } + } + if (lst.get(ind)) { + val pre = ind + var nex = lst.next(ind + 1) + if (nex == -1) { + nex = 2 * n + } + if (pre != -1 && pre < n && --fs[nex - pre] == 0) { + ff.unsetPos(nex - pre) + } + } + if (lst.get(ind + 1)) { + val pre = ind + 1 + var nex = lst.next(ind + 2) + if (nex == -1) { + nex = 2 * n + } + if (pre != -1 && pre < n && --fs[nex - pre] == 0) { + ff.unsetPos(nex - pre) + } + } + lst.unsetPos(ind) + lst.unsetPos(ind + 1) + c[ind] = c[ind] xor 1 + if (ind > 0 && c[ind] != c[ind - 1]) { + lst.setPos(ind) + } + if (ind + 1 < c.size && c[ind + 1] != c[ind]) { + lst.setPos(ind + 1) + } + if (ind > 0) { + val pre = lst.prev(ind - 1) + var nex = lst.next(pre + 1) + if (nex == -1) { + nex = 2 * n + } + if (pre != -1 && pre < n && ++fs[nex - pre] == 1) { + ff.setPos(nex - pre) + } + } + if (lst.get(ind)) { + val pre = ind + var nex = lst.next(ind + 1) + if (nex == -1) { + nex = 2 * n + } + if (pre < n && ++fs[nex - pre] == 1) { + ff.setPos(nex - pre) + } + } + if (lst.get(ind + 1)) { + val pre = ind + 1 + var nex = lst.next(ind + 2) + if (nex == -1) { + nex = 2 * n + } + if (pre < n && ++fs[nex - pre] == 1) { + ff.setPos(nex - pre) + } + } + } + + fun numberOfAlternatingGroups(colors: IntArray, queries: Array): List { + val n = colors.size + val c = IntArray(2 * n) + for (i in 0 until 2 * n) { + c[i] = colors[i % n] xor (if (i % 2 == 0) 0 else 1) + } + val lst = LST(2 * n + 3) + for (i in 1 until 2 * n) { + if (c[i] != c[i - 1]) { + lst.setPos(i) + } + } + val fs = IntArray(2 * n + 1) + val ff = LST(2 * n + 1) + for (i in 0 until n) { + if (lst.get(i)) { + var ne = lst.next(i + 1) + if (ne == -1) { + ne = 2 * n + } + fs[ne - i]++ + ff.setPos(ne - i) + } + } + val ans: MutableList = ArrayList() + for (q in queries) { + if (q[0] == 1) { + if (lst.next(0) == -1) { + ans.add(n) + } else { + var lans = 0 + var i = ff.next(q[1]) + while (i != -1) { + lans += (i - q[1] + 1) * fs[i] + i = ff.next(i + 1) + } + if (c[2 * n - 1] != c[0]) { + val f = lst.next(0) + if (f >= q[1]) { + lans += (f - q[1] + 1) + } + } + ans.add(lans) + } + } else { + val ind = q[1] + val `val` = q[2] + if (colors[ind] == `val`) { + continue + } + colors[ind] = colors[ind] xor 1 + go(ind, lst, fs, n, ff, c) + go(ind + n, lst, fs, n, ff, c) + } + } + return ans + } + + private class LST(private val n: Int) { + private val set: Array + + init { + var d = 1 + d = getD(n, d) + set = arrayOfNulls(d) + var i = 0 + var m = n ushr 6 + while (i < d) { + set[i] = LongArray(m + 1) + i++ + m = m ushr 6 + } + } + + private fun getD(n: Int, d: Int): Int { + var d = d + var m = n + while (m > 1) { + m = m ushr 6 + d++ + } + return d + } + + fun setPos(pos: Int): LST { + var pos = pos + if (pos >= 0 && pos < n) { + var i = 0 + while (i < set.size) { + set[i]!![pos ushr 6] = set[i]!![pos ushr 6] or (1L shl pos) + i++ + pos = pos ushr 6 + } + } + return this + } + + fun unsetPos(pos: Int): LST { + var pos = pos + if (pos >= 0 && pos < n) { + var i = 0 + while (i < set.size && (i == 0 || set[i - 1]!![pos] == 0L) + ) { + set[i]!![pos ushr 6] = set[i]!![pos ushr 6] and (1L shl pos).inv() + i++ + pos = pos ushr 6 + } + } + return this + } + + fun get(pos: Int): Boolean { + return pos >= 0 && pos < n && set[0]!![pos ushr 6] shl pos.inv() < 0 + } + + fun prev(pos: Int): Int { + var pos = pos + var i = 0 + while (i < set.size && pos >= 0) { + val pre = prev(set[i]!![pos ushr 6], pos and 63) + if (pre != -1) { + pos = pos ushr 6 shl 6 or pre + while (i > 0) { + pos = pos shl 6 or 63 - java.lang.Long.numberOfLeadingZeros(set[--i]!![pos]) + } + return pos + } + i++ + pos = pos ushr 6 + pos-- + } + return -1 + } + + private fun prev(set: Long, n: Int): Int { + val h = set shl n.inv() + if (h == 0L) { + return -1 + } + return -java.lang.Long.numberOfLeadingZeros(h) + n + } + + fun next(pos: Int): Int { + var pos = pos + var i = 0 + while (i < set.size && pos ushr 6 < set[i]!!.size) { + val nex = next(set[i]!![pos ushr 6], pos and 63) + if (nex != -1) { + pos = pos ushr 6 shl 6 or nex + while (i > 0) { + pos = pos shl 6 or java.lang.Long.numberOfTrailingZeros(set[--i]!![pos]) + } + return pos + } + i++ + pos = pos ushr 6 + pos++ + } + return -1 + } + + override fun toString(): String { + val list: MutableList = ArrayList() + var pos = next(0) + while (pos != -1) { + list.add(pos) + pos = next(pos + 1) + } + return list.toString() + } + + companion object { + private fun next(set: Long, n: Int): Int { + val h = set ushr n + if (h == 0L) { + return -1 + } + return java.lang.Long.numberOfTrailingZeros(h) + n + } + } + } +} diff --git a/src/main/kotlin/g3201_3300/s3245_alternating_groups_iii/readme.md b/src/main/kotlin/g3201_3300/s3245_alternating_groups_iii/readme.md new file mode 100644 index 000000000..5e869ed84 --- /dev/null +++ b/src/main/kotlin/g3201_3300/s3245_alternating_groups_iii/readme.md @@ -0,0 +1,73 @@ +3245\. Alternating Groups III + +Hard + +There are some red and blue tiles arranged circularly. You are given an array of integers `colors` and a 2D integers array `queries`. + +The color of tile `i` is represented by `colors[i]`: + +* `colors[i] == 0` means that tile `i` is **red**. +* `colors[i] == 1` means that tile `i` is **blue**. + +An **alternating** group is a contiguous subset of tiles in the circle with **alternating** colors (each tile in the group except the first and last one has a different color from its **adjacent** tiles in the group). + +You have to process queries of two types: + +* queries[i] = [1, sizei], determine the count of **alternating** groups with size sizei. +* queries[i] = [2, indexi, colori], change colors[indexi] to colori. + +Return an array `answer` containing the results of the queries of the first type _in order_. + +**Note** that since `colors` represents a **circle**, the **first** and the **last** tiles are considered to be next to each other. + +**Example 1:** + +**Input:** colors = [0,1,1,0,1], queries = [[2,1,0],[1,4]] + +**Output:** [2] + +**Explanation:** + +**![](https://assets.leetcode.com/uploads/2024/06/03/screenshot-from-2024-06-03-20-14-44.png)** + +First query: + +Change `colors[1]` to 0. + +![](https://assets.leetcode.com/uploads/2024/06/03/screenshot-from-2024-06-03-20-20-25.png) + +Second query: + +Count of the alternating groups with size 4: + +![](https://assets.leetcode.com/uploads/2024/06/03/screenshot-from-2024-06-03-20-25-02-2.png)![](https://assets.leetcode.com/uploads/2024/06/03/screenshot-from-2024-06-03-20-24-12.png) + +**Example 2:** + +**Input:** colors = [0,0,1,0,1,1], queries = [[1,3],[2,3,0],[1,5]] + +**Output:** [2,0] + +**Explanation:** + +![](https://assets.leetcode.com/uploads/2024/06/03/screenshot-from-2024-06-03-20-35-50.png) + +First query: + +Count of the alternating groups with size 3: + +![](https://assets.leetcode.com/uploads/2024/06/03/screenshot-from-2024-06-03-20-37-13.png)![](https://assets.leetcode.com/uploads/2024/06/03/screenshot-from-2024-06-03-20-36-40.png) + +Second query: `colors` will not change. + +Third query: There is no alternating group with size 5. + +**Constraints:** + +* 4 <= colors.length <= 5 * 104 +* `0 <= colors[i] <= 1` +* 1 <= queries.length <= 5 * 104 +* `queries[i][0] == 1` or `queries[i][0] == 2` +* For all `i` that: + * `queries[i][0] == 1`: `queries[i].length == 2`, `3 <= queries[i][1] <= colors.length - 1` + * `queries[i][0] == 2`: `queries[i].length == 3`, `0 <= queries[i][1] <= colors.length - 1`, `0 <= queries[i][2] <= 1` \ No newline at end of file diff --git a/src/test/kotlin/g3201_3300/s3238_find_the_number_of_winning_players/SolutionTest.kt b/src/test/kotlin/g3201_3300/s3238_find_the_number_of_winning_players/SolutionTest.kt new file mode 100644 index 000000000..faaea86aa --- /dev/null +++ b/src/test/kotlin/g3201_3300/s3238_find_the_number_of_winning_players/SolutionTest.kt @@ -0,0 +1,48 @@ +package g3201_3300.s3238_find_the_number_of_winning_players + +import org.hamcrest.CoreMatchers.equalTo +import org.hamcrest.MatcherAssert.assertThat +import org.junit.jupiter.api.Test + +internal class SolutionTest { + @Test + fun winningPlayerCount() { + assertThat( + Solution() + .winningPlayerCount( + 4, + arrayOf( + intArrayOf(0, 0), + intArrayOf(1, 0), + intArrayOf(1, 0), + intArrayOf(2, 1), + intArrayOf(2, 1), + intArrayOf(2, 0) + ) + ), + equalTo(2) + ) + } + + @Test + fun winningPlayerCount2() { + assertThat( + Solution().winningPlayerCount( + 5, + arrayOf(intArrayOf(1, 1), intArrayOf(1, 2), intArrayOf(1, 3), intArrayOf(1, 4)) + ), + equalTo(0) + ) + } + + @Test + fun winningPlayerCount3() { + assertThat( + Solution().winningPlayerCount( + 5, + arrayOf(intArrayOf(1, 1), intArrayOf(2, 4), intArrayOf(2, 4), intArrayOf(2, 4)) + ), + equalTo(1) + ) + } +} diff --git a/src/test/kotlin/g3201_3300/s3239_minimum_number_of_flips_to_make_binary_grid_palindromic_i/SolutionTest.kt b/src/test/kotlin/g3201_3300/s3239_minimum_number_of_flips_to_make_binary_grid_palindromic_i/SolutionTest.kt new file mode 100644 index 000000000..7801b2408 --- /dev/null +++ b/src/test/kotlin/g3201_3300/s3239_minimum_number_of_flips_to_make_binary_grid_palindromic_i/SolutionTest.kt @@ -0,0 +1,28 @@ +package g3201_3300.s3239_minimum_number_of_flips_to_make_binary_grid_palindromic_i + +import org.hamcrest.CoreMatchers.equalTo +import org.hamcrest.MatcherAssert.assertThat +import org.junit.jupiter.api.Test + +internal class SolutionTest { + @Test + fun minFlips() { + assertThat( + Solution().minFlips(arrayOf(intArrayOf(1, 0, 0), intArrayOf(0, 0, 0), intArrayOf(0, 0, 1))), + equalTo(2) + ) + } + + @Test + fun minFlips2() { + assertThat( + Solution().minFlips(arrayOf(intArrayOf(0, 1), intArrayOf(0, 1), intArrayOf(0, 0))), + equalTo(1) + ) + } + + @Test + fun minFlips3() { + assertThat(Solution().minFlips(arrayOf(intArrayOf(1), intArrayOf(0))), equalTo(0)) + } +} diff --git a/src/test/kotlin/g3201_3300/s3240_minimum_number_of_flips_to_make_binary_grid_palindromic_ii/SolutionTest.kt b/src/test/kotlin/g3201_3300/s3240_minimum_number_of_flips_to_make_binary_grid_palindromic_ii/SolutionTest.kt new file mode 100644 index 000000000..89a612d37 --- /dev/null +++ b/src/test/kotlin/g3201_3300/s3240_minimum_number_of_flips_to_make_binary_grid_palindromic_ii/SolutionTest.kt @@ -0,0 +1,28 @@ +package g3201_3300.s3240_minimum_number_of_flips_to_make_binary_grid_palindromic_ii + +import org.hamcrest.CoreMatchers.equalTo +import org.hamcrest.MatcherAssert.assertThat +import org.junit.jupiter.api.Test + +internal class SolutionTest { + @Test + fun minFlips() { + assertThat( + Solution().minFlips(arrayOf(intArrayOf(1, 0, 0), intArrayOf(0, 1, 0), intArrayOf(0, 0, 1))), + equalTo(3) + ) + } + + @Test + fun minFlips2() { + assertThat( + Solution().minFlips(arrayOf(intArrayOf(0, 1), intArrayOf(0, 1), intArrayOf(0, 0))), + equalTo(2) + ) + } + + @Test + fun minFlips3() { + assertThat(Solution().minFlips(arrayOf(intArrayOf(1), intArrayOf(1))), equalTo(2)) + } +} diff --git a/src/test/kotlin/g3201_3300/s3241_time_taken_to_mark_all_nodes/SolutionTest.kt b/src/test/kotlin/g3201_3300/s3241_time_taken_to_mark_all_nodes/SolutionTest.kt new file mode 100644 index 000000000..44eaa9218 --- /dev/null +++ b/src/test/kotlin/g3201_3300/s3241_time_taken_to_mark_all_nodes/SolutionTest.kt @@ -0,0 +1,31 @@ +package g3201_3300.s3241_time_taken_to_mark_all_nodes + +import org.hamcrest.CoreMatchers.equalTo +import org.hamcrest.MatcherAssert.assertThat +import org.junit.jupiter.api.Test + +internal class SolutionTest { + @Test + fun timeTaken() { + assertThat( + Solution().timeTaken(arrayOf(intArrayOf(0, 1), intArrayOf(0, 2))), + equalTo(intArrayOf(2, 4, 3)) + ) + } + + @Test + fun timeTaken2() { + assertThat( + Solution().timeTaken(arrayOf(intArrayOf(0, 1))), + equalTo(intArrayOf(1, 2)) + ) + } + + @Test + fun timeTaken3() { + assertThat( + Solution().timeTaken(arrayOf(intArrayOf(2, 4), intArrayOf(0, 1), intArrayOf(2, 3), intArrayOf(0, 2))), + equalTo(intArrayOf(4, 6, 3, 5, 5)) + ) + } +} diff --git a/src/test/kotlin/g3201_3300/s3242_design_neighbor_sum_service/SolutionTest.kt b/src/test/kotlin/g3201_3300/s3242_design_neighbor_sum_service/SolutionTest.kt new file mode 100644 index 000000000..64de4cca0 --- /dev/null +++ b/src/test/kotlin/g3201_3300/s3242_design_neighbor_sum_service/SolutionTest.kt @@ -0,0 +1,31 @@ +package g3201_3300.s3242_design_neighbor_sum_service + +import org.hamcrest.CoreMatchers.equalTo +import org.hamcrest.MatcherAssert.assertThat +import org.junit.jupiter.api.Test + +internal class SolutionTest { + @Test + fun neighborSum() { + val neighborSum = NeighborSum(arrayOf(intArrayOf(0, 1, 2), intArrayOf(3, 4, 5), intArrayOf(6, 7, 8))) + assertThat(neighborSum.adjacentSum(1), equalTo(6)) + assertThat(neighborSum.adjacentSum(4), equalTo(16)) + assertThat(neighborSum.diagonalSum(4), equalTo(16)) + assertThat(neighborSum.diagonalSum(8), equalTo(4)) + } + + @Test + fun neighborSum2() { + val neighborSum = + NeighborSum( + arrayOf( + intArrayOf(1, 2, 0, 3), + intArrayOf(4, 7, 15, 6), + intArrayOf(8, 9, 10, 11), + intArrayOf(12, 13, 14, 5) + ) + ) + assertThat(neighborSum.adjacentSum(15), equalTo(23)) + assertThat(neighborSum.diagonalSum(9), equalTo(45)) + } +} diff --git a/src/test/kotlin/g3201_3300/s3243_shortest_distance_after_road_addition_queries_i/SolutionTest.kt b/src/test/kotlin/g3201_3300/s3243_shortest_distance_after_road_addition_queries_i/SolutionTest.kt new file mode 100644 index 000000000..1782125fe --- /dev/null +++ b/src/test/kotlin/g3201_3300/s3243_shortest_distance_after_road_addition_queries_i/SolutionTest.kt @@ -0,0 +1,24 @@ +package g3201_3300.s3243_shortest_distance_after_road_addition_queries_i + +import org.hamcrest.CoreMatchers.equalTo +import org.hamcrest.MatcherAssert.assertThat +import org.junit.jupiter.api.Test + +internal class SolutionTest { + @Test + fun shortestDistanceAfterQueries() { + assertThat( + Solution() + .shortestDistanceAfterQueries(5, arrayOf(intArrayOf(2, 4), intArrayOf(0, 2), intArrayOf(0, 4))), + equalTo(intArrayOf(3, 2, 1)) + ) + } + + @Test + fun shortestDistanceAfterQueries2() { + assertThat( + Solution().shortestDistanceAfterQueries(4, arrayOf(intArrayOf(0, 3), intArrayOf(0, 2))), + equalTo(intArrayOf(1, 1)) + ) + } +} diff --git a/src/test/kotlin/g3201_3300/s3244_shortest_distance_after_road_addition_queries_ii/SolutionTest.kt b/src/test/kotlin/g3201_3300/s3244_shortest_distance_after_road_addition_queries_ii/SolutionTest.kt new file mode 100644 index 000000000..78634c940 --- /dev/null +++ b/src/test/kotlin/g3201_3300/s3244_shortest_distance_after_road_addition_queries_ii/SolutionTest.kt @@ -0,0 +1,24 @@ +package g3201_3300.s3244_shortest_distance_after_road_addition_queries_ii + +import org.hamcrest.CoreMatchers.equalTo +import org.hamcrest.MatcherAssert.assertThat +import org.junit.jupiter.api.Test + +internal class SolutionTest { + @Test + fun shortestDistanceAfterQueries() { + assertThat( + Solution() + .shortestDistanceAfterQueries(5, arrayOf(intArrayOf(2, 4), intArrayOf(0, 2), intArrayOf(0, 4))), + equalTo(intArrayOf(3, 2, 1)) + ) + } + + @Test + fun shortestDistanceAfterQueries2() { + assertThat( + Solution().shortestDistanceAfterQueries(4, arrayOf(intArrayOf(0, 3), intArrayOf(0, 2))), + equalTo(intArrayOf(1, 1)) + ) + } +} diff --git a/src/test/kotlin/g3201_3300/s3245_alternating_groups_iii/SolutionTest.kt b/src/test/kotlin/g3201_3300/s3245_alternating_groups_iii/SolutionTest.kt new file mode 100644 index 000000000..d8352d3ec --- /dev/null +++ b/src/test/kotlin/g3201_3300/s3245_alternating_groups_iii/SolutionTest.kt @@ -0,0 +1,30 @@ +package g3201_3300.s3245_alternating_groups_iii + +import org.hamcrest.CoreMatchers.equalTo +import org.hamcrest.MatcherAssert.assertThat +import org.junit.jupiter.api.Test + +internal class SolutionTest { + @Test + fun numberOfAlternatingGroups() { + assertThat( + Solution() + .numberOfAlternatingGroups( + intArrayOf(0, 1, 1, 0, 1), arrayOf(intArrayOf(2, 1, 0), intArrayOf(1, 4)) + ), + equalTo(listOf(2)) + ) + } + + @Test + fun numberOfAlternatingGroups2() { + assertThat( + Solution() + .numberOfAlternatingGroups( + intArrayOf(0, 0, 1, 0, 1, 1), + arrayOf(intArrayOf(1, 3), intArrayOf(2, 3, 0), intArrayOf(1, 5)) + ), + equalTo(listOf(2, 0)) + ) + } +}