Skip to content

Commit 81f9c9c

Browse files
authored
Added tasks 3238-3251
1 parent b9dd9ca commit 81f9c9c

File tree

13 files changed

+1478
-0
lines changed
  • src/main/kotlin/g3201_3300
    • s3238_find_the_number_of_winning_players
    • s3239_minimum_number_of_flips_to_make_binary_grid_palindromic_i
    • s3240_minimum_number_of_flips_to_make_binary_grid_palindromic_ii
    • s3241_time_taken_to_mark_all_nodes
    • s3242_design_neighbor_sum_service
    • s3243_shortest_distance_after_road_addition_queries_i
    • s3244_shortest_distance_after_road_addition_queries_ii
    • s3245_alternating_groups_iii
    • s3248_snake_in_matrix
    • s3249_count_the_number_of_good_nodes
    • s3250_find_the_count_of_monotonic_pairs_i
    • s3251_find_the_count_of_monotonic_pairs_ii

13 files changed

+1478
-0
lines changed

README.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1816,6 +1816,18 @@
18161816

18171817
| # | Title | Difficulty | Tag | Time, ms | Time, %
18181818
|------|----------------|-------------|-------------|----------|--------
1819+
| 3251 |[Find the Count of Monotonic Pairs II](src/main/kotlin/g3201_3300/s3251_find_the_count_of_monotonic_pairs_ii)| Hard | Array, Dynamic_Programming, Math, Prefix_Sum, Combinatorics | 291 | 100.00
1820+
| 3250 |[Find the Count of Monotonic Pairs I](src/main/kotlin/g3201_3300/s3250_find_the_count_of_monotonic_pairs_i)| Hard | Array, Dynamic_Programming, Math, Prefix_Sum, Combinatorics | 241 | 100.00
1821+
| 3249 |[Count the Number of Good Nodes](src/main/kotlin/g3201_3300/s3249_count_the_number_of_good_nodes)| Medium | Depth_First_Search, Tree | 1190 | 100.00
1822+
| 3248 |[Snake in Matrix](src/main/kotlin/g3201_3300/s3248_snake_in_matrix)| Easy | Array, String, Simulation | 174 | 90.91
1823+
| 3245 |[Alternating Groups III](src/main/kotlin/g3201_3300/s3245_alternating_groups_iii)| Hard | Array, Binary_Indexed_Tree | 1072 | 100.00
1824+
| 3244 |[Shortest Distance After Road Addition Queries II](src/main/kotlin/g3201_3300/s3244_shortest_distance_after_road_addition_queries_ii)| Hard | Array, Greedy, Graph, Ordered_Set | 794 | 92.31
1825+
| 3243 |[Shortest Distance After Road Addition Queries I](src/main/kotlin/g3201_3300/s3243_shortest_distance_after_road_addition_queries_i)| Medium | Array, Breadth_First_Search, Graph | 313 | 97.06
1826+
| 3242 |[Design Neighbor Sum Service](src/main/kotlin/g3201_3300/s3242_design_neighbor_sum_service)| Easy | Array, Hash_Table, Matrix, Design, Simulation | 333 | 75.00
1827+
| 3241 |[Time Taken to Mark All Nodes](src/main/kotlin/g3201_3300/s3241_time_taken_to_mark_all_nodes)| Hard | Dynamic_Programming, Depth_First_Search, Tree, Graph | 1228 | 100.00
1828+
| 3240 |[Minimum Number of Flips to Make Binary Grid Palindromic II](src/main/kotlin/g3201_3300/s3240_minimum_number_of_flips_to_make_binary_grid_palindromic_ii)| Medium | Array, Matrix, Two_Pointers | 805 | 100.00
1829+
| 3239 |[Minimum Number of Flips to Make Binary Grid Palindromic I](src/main/kotlin/g3201_3300/s3239_minimum_number_of_flips_to_make_binary_grid_palindromic_i)| Medium | Array, Matrix, Two_Pointers | 856 | 87.50
1830+
| 3238 |[Find the Number of Winning Players](src/main/kotlin/g3201_3300/s3238_find_the_number_of_winning_players)| Easy | Array, Hash_Table, Counting | 207 | 90.38
18191831
| 3235 |[Check if the Rectangle Corner Is Reachable](src/main/kotlin/g3201_3300/s3235_check_if_the_rectangle_corner_is_reachable)| Hard | Array, Math, Depth_First_Search, Breadth_First_Search, Union_Find, Geometry | 612 | 66.67
18201832
| 3234 |[Count the Number of Substrings With Dominant Ones](src/main/kotlin/g3201_3300/s3234_count_the_number_of_substrings_with_dominant_ones)| Medium | String, Sliding_Window, Enumeration | 356 | 100.00
18211833
| 3233 |[Find the Count of Numbers Which Are Not Special](src/main/kotlin/g3201_3300/s3233_find_the_count_of_numbers_which_are_not_special)| Medium | Array, Math, Number_Theory | 215 | 76.19
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
[![](https://img.shields.io/github/stars/javadev/LeetCode-in-Kotlin?label=Stars&style=flat-square)](https://github.com/javadev/LeetCode-in-Kotlin)
2+
[![](https://img.shields.io/github/forks/javadev/LeetCode-in-Kotlin?label=Fork%20me%20on%20GitHub%20&style=flat-square)](https://github.com/javadev/LeetCode-in-Kotlin/fork)
3+
4+
## 3238\. Find the Number of Winning Players
5+
6+
Easy
7+
8+
You are given an integer `n` representing the number of players in a game and a 2D array `pick` where <code>pick[i] = [x<sub>i</sub>, y<sub>i</sub>]</code> represents that the player <code>x<sub>i</sub></code> picked a ball of color <code>y<sub>i</sub></code>.
9+
10+
Player `i` **wins** the game if they pick **strictly more** than `i` balls of the **same** color. In other words,
11+
12+
* Player 0 wins if they pick any ball.
13+
* Player 1 wins if they pick at least two balls of the _same_ color.
14+
* ...
15+
* Player `i` wins if they pick at least`i + 1` balls of the _same_ color.
16+
17+
Return the number of players who **win** the game.
18+
19+
**Note** that _multiple_ players can win the game.
20+
21+
**Example 1:**
22+
23+
**Input:** n = 4, pick = \[\[0,0],[1,0],[1,0],[2,1],[2,1],[2,0]]
24+
25+
**Output:** 2
26+
27+
**Explanation:**
28+
29+
Player 0 and player 1 win the game, while players 2 and 3 do not win.
30+
31+
**Example 2:**
32+
33+
**Input:** n = 5, pick = \[\[1,1],[1,2],[1,3],[1,4]]
34+
35+
**Output:** 0
36+
37+
**Explanation:**
38+
39+
No player wins the game.
40+
41+
**Example 3:**
42+
43+
**Input:** n = 5, pick = \[\[1,1],[2,4],[2,4],[2,4]]
44+
45+
**Output:** 1
46+
47+
**Explanation:**
48+
49+
Player 2 wins the game by picking 3 balls with color 4.
50+
51+
**Constraints:**
52+
53+
* `2 <= n <= 10`
54+
* `1 <= pick.length <= 100`
55+
* `pick[i].length == 2`
56+
* <code>0 <= x<sub>i</sub> <= n - 1</code>
57+
* <code>0 <= y<sub>i</sub> <= 10</code>
58+
59+
## Solution
60+
61+
```kotlin
62+
@Suppress("UNUSED_PARAMETER")
63+
class Solution {
64+
fun winningPlayerCount(n: Int, pick: Array<IntArray>): Int {
65+
val dp = Array(11) { IntArray(11) }
66+
for (ints in pick) {
67+
val p = ints[0]
68+
val pi = ints[1]
69+
dp[p][pi] += 1
70+
}
71+
var count = 0
72+
for (i in 0..10) {
73+
var win = false
74+
for (j in 0..10) {
75+
if (dp[i][j] >= i + 1) {
76+
win = true
77+
break
78+
}
79+
}
80+
if (win) {
81+
count += 1
82+
}
83+
}
84+
return count
85+
}
86+
}
87+
```
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
[![](https://img.shields.io/github/stars/javadev/LeetCode-in-Kotlin?label=Stars&style=flat-square)](https://github.com/javadev/LeetCode-in-Kotlin)
2+
[![](https://img.shields.io/github/forks/javadev/LeetCode-in-Kotlin?label=Fork%20me%20on%20GitHub%20&style=flat-square)](https://github.com/javadev/LeetCode-in-Kotlin/fork)
3+
4+
## 3239\. Minimum Number of Flips to Make Binary Grid Palindromic I
5+
6+
Medium
7+
8+
You are given an `m x n` binary matrix `grid`.
9+
10+
A row or column is considered **palindromic** if its values read the same forward and backward.
11+
12+
You can **flip** any number of cells in `grid` from `0` to `1`, or from `1` to `0`.
13+
14+
Return the **minimum** number of cells that need to be flipped to make **either** all rows **palindromic** or all columns **palindromic**.
15+
16+
**Example 1:**
17+
18+
**Input:** grid = \[\[1,0,0],[0,0,0],[0,0,1]]
19+
20+
**Output:** 2
21+
22+
**Explanation:**
23+
24+
![](https://assets.leetcode.com/uploads/2024/07/07/screenshot-from-2024-07-08-00-20-10.png)
25+
26+
Flipping the highlighted cells makes all the rows palindromic.
27+
28+
**Example 2:**
29+
30+
**Input:** grid = \[\[0,1],[0,1],[0,0]]
31+
32+
**Output:** 1
33+
34+
**Explanation:**
35+
36+
![](https://assets.leetcode.com/uploads/2024/07/07/screenshot-from-2024-07-08-00-31-23.png)
37+
38+
Flipping the highlighted cell makes all the columns palindromic.
39+
40+
**Example 3:**
41+
42+
**Input:** grid = \[\[1],[0]]
43+
44+
**Output:** 0
45+
46+
**Explanation:**
47+
48+
All rows are already palindromic.
49+
50+
**Constraints:**
51+
52+
* `m == grid.length`
53+
* `n == grid[i].length`
54+
* <code>1 <= m * n <= 2 * 10<sup>5</sup></code>
55+
* `0 <= grid[i][j] <= 1`
56+
57+
## Solution
58+
59+
```kotlin
60+
import kotlin.math.min
61+
62+
class Solution {
63+
fun minFlips(grid: Array<IntArray>): Int {
64+
val m = grid.size
65+
val n = grid[0].size
66+
var rowFlips = 0
67+
for (i in 0 until m / 2) {
68+
for (j in 0 until n) {
69+
val sum = grid[i][j] + grid[m - 1 - i][j]
70+
rowFlips += min(sum, (2 - sum))
71+
}
72+
}
73+
var columnFlips = 0
74+
for (j in 0 until n / 2) {
75+
for (ints in grid) {
76+
val sum = ints[j] + ints[n - 1 - j]
77+
columnFlips += min(sum, (2 - sum))
78+
}
79+
}
80+
return min(rowFlips, columnFlips)
81+
}
82+
}
83+
```
Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
[![](https://img.shields.io/github/stars/javadev/LeetCode-in-Kotlin?label=Stars&style=flat-square)](https://github.com/javadev/LeetCode-in-Kotlin)
2+
[![](https://img.shields.io/github/forks/javadev/LeetCode-in-Kotlin?label=Fork%20me%20on%20GitHub%20&style=flat-square)](https://github.com/javadev/LeetCode-in-Kotlin/fork)
3+
4+
## 3240\. Minimum Number of Flips to Make Binary Grid Palindromic II
5+
6+
Medium
7+
8+
You are given an `m x n` binary matrix `grid`.
9+
10+
A row or column is considered **palindromic** if its values read the same forward and backward.
11+
12+
You can **flip** any number of cells in `grid` from `0` to `1`, or from `1` to `0`.
13+
14+
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`.
15+
16+
**Example 1:**
17+
18+
**Input:** grid = \[\[1,0,0],[0,1,0],[0,0,1]]
19+
20+
**Output:** 3
21+
22+
**Explanation:**
23+
24+
![](https://assets.leetcode.com/uploads/2024/08/01/image.png)
25+
26+
**Example 2:**
27+
28+
**Input:** grid = \[\[0,1],[0,1],[0,0]]
29+
30+
**Output:** 2
31+
32+
**Explanation:**
33+
34+
![](https://assets.leetcode.com/uploads/2024/07/08/screenshot-from-2024-07-09-01-37-48.png)
35+
36+
**Example 3:**
37+
38+
**Input:** grid = \[\[1],[1]]
39+
40+
**Output:** 2
41+
42+
**Explanation:**
43+
44+
![](https://assets.leetcode.com/uploads/2024/08/01/screenshot-from-2024-08-01-23-05-26.png)
45+
46+
**Constraints:**
47+
48+
* `m == grid.length`
49+
* `n == grid[i].length`
50+
* <code>1 <= m * n <= 2 * 10<sup>5</sup></code>
51+
* `0 <= grid[i][j] <= 1`
52+
53+
## Solution
54+
55+
```kotlin
56+
import kotlin.math.min
57+
58+
class Solution {
59+
fun minFlips(grid: Array<IntArray>): Int {
60+
var res = 0
61+
var one = 0
62+
var diff = 0
63+
val m = grid.size
64+
val n = grid[0].size
65+
// Handle quadrants
66+
for (i in 0 until m / 2) {
67+
for (j in 0 until n / 2) {
68+
val v =
69+
(
70+
grid[i][j] +
71+
grid[i][n - 1 - j] +
72+
grid[m - 1 - i][j] +
73+
grid[m - 1 - i][n - 1 - j]
74+
)
75+
res += min(v, (4 - v))
76+
}
77+
}
78+
// Handle middle column
79+
if (n % 2 > 0) {
80+
for (i in 0 until m / 2) {
81+
diff += grid[i][n / 2] xor grid[m - 1 - i][n / 2]
82+
one += grid[i][n / 2] + grid[m - 1 - i][n / 2]
83+
}
84+
}
85+
// Handle middle row
86+
if (m % 2 > 0) {
87+
for (j in 0 until n / 2) {
88+
diff += grid[m / 2][j] xor grid[m / 2][n - 1 - j]
89+
one += grid[m / 2][j] + grid[m / 2][n - 1 - j]
90+
}
91+
}
92+
// Handle center point
93+
if (n % 2 > 0 && m % 2 > 0) {
94+
res += grid[m / 2][n / 2]
95+
}
96+
// Divisible by 4
97+
if (diff == 0 && one % 4 > 0) {
98+
res += 2
99+
}
100+
return res + diff
101+
}
102+
}
103+
```

0 commit comments

Comments
 (0)