Skip to content

Commit 7e4890c

Browse files
authored
Added tasks 3238-3245
1 parent eb94f50 commit 7e4890c

File tree

24 files changed

+1317
-0
lines changed

24 files changed

+1317
-0
lines changed
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package g3201_3300.s3238_find_the_number_of_winning_players
2+
3+
// #Easy #Array #Hash_Table #Counting #2024_08_07_Time_207_ms_(90.38%)_Space_42_MB_(75.00%)
4+
5+
@Suppress("UNUSED_PARAMETER")
6+
class Solution {
7+
fun winningPlayerCount(n: Int, pick: Array<IntArray>): Int {
8+
val dp = Array(11) { IntArray(11) }
9+
for (ints in pick) {
10+
val p = ints[0]
11+
val pi = ints[1]
12+
dp[p][pi] += 1
13+
}
14+
var count = 0
15+
for (i in 0..10) {
16+
var win = false
17+
for (j in 0..10) {
18+
if (dp[i][j] >= i + 1) {
19+
win = true
20+
break
21+
}
22+
}
23+
if (win) {
24+
count += 1
25+
}
26+
}
27+
return count
28+
}
29+
}
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
3238\. Find the Number of Winning Players
2+
3+
Easy
4+
5+
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>.
6+
7+
Player `i` **wins** the game if they pick **strictly more** than `i` balls of the **same** color. In other words,
8+
9+
* Player 0 wins if they pick any ball.
10+
* Player 1 wins if they pick at least two balls of the _same_ color.
11+
* ...
12+
* Player `i` wins if they pick at least`i + 1` balls of the _same_ color.
13+
14+
Return the number of players who **win** the game.
15+
16+
**Note** that _multiple_ players can win the game.
17+
18+
**Example 1:**
19+
20+
**Input:** n = 4, pick = [[0,0],[1,0],[1,0],[2,1],[2,1],[2,0]]
21+
22+
**Output:** 2
23+
24+
**Explanation:**
25+
26+
Player 0 and player 1 win the game, while players 2 and 3 do not win.
27+
28+
**Example 2:**
29+
30+
**Input:** n = 5, pick = [[1,1],[1,2],[1,3],[1,4]]
31+
32+
**Output:** 0
33+
34+
**Explanation:**
35+
36+
No player wins the game.
37+
38+
**Example 3:**
39+
40+
**Input:** n = 5, pick = [[1,1],[2,4],[2,4],[2,4]]
41+
42+
**Output:** 1
43+
44+
**Explanation:**
45+
46+
Player 2 wins the game by picking 3 balls with color 4.
47+
48+
**Constraints:**
49+
50+
* `2 <= n <= 10`
51+
* `1 <= pick.length <= 100`
52+
* `pick[i].length == 2`
53+
* <code>0 <= x<sub>i</sub> <= n - 1</code>
54+
* <code>0 <= y<sub>i</sub> <= 10</code>
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package g3201_3300.s3239_minimum_number_of_flips_to_make_binary_grid_palindromic_i
2+
3+
// #Medium #Array #Matrix #Two_Pointers #2024_08_07_Time_856_ms_(87.50%)_Space_109.2_MB_(66.67%)
4+
5+
import kotlin.math.min
6+
7+
class Solution {
8+
fun minFlips(grid: Array<IntArray>): Int {
9+
val m = grid.size
10+
val n = grid[0].size
11+
var rowFlips = 0
12+
for (i in 0 until m / 2) {
13+
for (j in 0 until n) {
14+
val sum = grid[i][j] + grid[m - 1 - i][j]
15+
rowFlips += min(sum, (2 - sum))
16+
}
17+
}
18+
var columnFlips = 0
19+
for (j in 0 until n / 2) {
20+
for (ints in grid) {
21+
val sum = ints[j] + ints[n - 1 - j]
22+
columnFlips += min(sum, (2 - sum))
23+
}
24+
}
25+
return min(rowFlips, columnFlips)
26+
}
27+
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
3239\. Minimum Number of Flips to Make Binary Grid Palindromic I
2+
3+
Medium
4+
5+
You are given an `m x n` binary matrix `grid`.
6+
7+
A row or column is considered **palindromic** if its values read the same forward and backward.
8+
9+
You can **flip** any number of cells in `grid` from `0` to `1`, or from `1` to `0`.
10+
11+
Return the **minimum** number of cells that need to be flipped to make **either** all rows **palindromic** or all columns **palindromic**.
12+
13+
**Example 1:**
14+
15+
**Input:** grid = [[1,0,0],[0,0,0],[0,0,1]]
16+
17+
**Output:** 2
18+
19+
**Explanation:**
20+
21+
![](https://assets.leetcode.com/uploads/2024/07/07/screenshot-from-2024-07-08-00-20-10.png)
22+
23+
Flipping the highlighted cells makes all the rows palindromic.
24+
25+
**Example 2:**
26+
27+
**Input:** grid = [[0,1],[0,1],[0,0]]
28+
29+
**Output:** 1
30+
31+
**Explanation:**
32+
33+
![](https://assets.leetcode.com/uploads/2024/07/07/screenshot-from-2024-07-08-00-31-23.png)
34+
35+
Flipping the highlighted cell makes all the columns palindromic.
36+
37+
**Example 3:**
38+
39+
**Input:** grid = [[1],[0]]
40+
41+
**Output:** 0
42+
43+
**Explanation:**
44+
45+
All rows are already palindromic.
46+
47+
**Constraints:**
48+
49+
* `m == grid.length`
50+
* `n == grid[i].length`
51+
* <code>1 <= m * n <= 2 * 10<sup>5</sup></code>
52+
* `0 <= grid[i][j] <= 1`
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
package g3201_3300.s3240_minimum_number_of_flips_to_make_binary_grid_palindromic_ii
2+
3+
// #Medium #Array #Matrix #Two_Pointers #2024_08_07_Time_805_ms_(100.00%)_Space_99_MB_(100.00%)
4+
5+
import kotlin.math.min
6+
7+
class Solution {
8+
fun minFlips(grid: Array<IntArray>): Int {
9+
var res = 0
10+
var one = 0
11+
var diff = 0
12+
val m = grid.size
13+
val n = grid[0].size
14+
// Handle quadrants
15+
for (i in 0 until m / 2) {
16+
for (j in 0 until n / 2) {
17+
val v =
18+
(
19+
grid[i][j] +
20+
grid[i][n - 1 - j] +
21+
grid[m - 1 - i][j] +
22+
grid[m - 1 - i][n - 1 - j]
23+
)
24+
res += min(v, (4 - v))
25+
}
26+
}
27+
// Handle middle column
28+
if (n % 2 > 0) {
29+
for (i in 0 until m / 2) {
30+
diff += grid[i][n / 2] xor grid[m - 1 - i][n / 2]
31+
one += grid[i][n / 2] + grid[m - 1 - i][n / 2]
32+
}
33+
}
34+
// Handle middle row
35+
if (m % 2 > 0) {
36+
for (j in 0 until n / 2) {
37+
diff += grid[m / 2][j] xor grid[m / 2][n - 1 - j]
38+
one += grid[m / 2][j] + grid[m / 2][n - 1 - j]
39+
}
40+
}
41+
// Handle center point
42+
if (n % 2 > 0 && m % 2 > 0) {
43+
res += grid[m / 2][n / 2]
44+
}
45+
// Divisible by 4
46+
if (diff == 0 && one % 4 > 0) {
47+
res += 2
48+
}
49+
return res + diff
50+
}
51+
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
3240\. Minimum Number of Flips to Make Binary Grid Palindromic II
2+
3+
Medium
4+
5+
You are given an `m x n` binary matrix `grid`.
6+
7+
A row or column is considered **palindromic** if its values read the same forward and backward.
8+
9+
You can **flip** any number of cells in `grid` from `0` to `1`, or from `1` to `0`.
10+
11+
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`.
12+
13+
**Example 1:**
14+
15+
**Input:** grid = [[1,0,0],[0,1,0],[0,0,1]]
16+
17+
**Output:** 3
18+
19+
**Explanation:**
20+
21+
![](https://assets.leetcode.com/uploads/2024/08/01/image.png)
22+
23+
**Example 2:**
24+
25+
**Input:** grid = [[0,1],[0,1],[0,0]]
26+
27+
**Output:** 2
28+
29+
**Explanation:**
30+
31+
![](https://assets.leetcode.com/uploads/2024/07/08/screenshot-from-2024-07-09-01-37-48.png)
32+
33+
**Example 3:**
34+
35+
**Input:** grid = [[1],[1]]
36+
37+
**Output:** 2
38+
39+
**Explanation:**
40+
41+
![](https://assets.leetcode.com/uploads/2024/08/01/screenshot-from-2024-08-01-23-05-26.png)
42+
43+
**Constraints:**
44+
45+
* `m == grid.length`
46+
* `n == grid[i].length`
47+
* <code>1 <= m * n <= 2 * 10<sup>5</sup></code>
48+
* `0 <= grid[i][j] <= 1`
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
package g3201_3300.s3241_time_taken_to_mark_all_nodes
2+
3+
// #Hard #Dynamic_Programming #Depth_First_Search #Tree #Graph
4+
// #2024_08_07_Time_1228_ms_(100.00%)_Space_108.5_MB_(100.00%)
5+
6+
import kotlin.math.max
7+
8+
class Solution {
9+
private lateinit var head: IntArray
10+
private lateinit var nxt: IntArray
11+
private lateinit var to: IntArray
12+
private lateinit var last: IntArray
13+
private lateinit var lastNo: IntArray
14+
private lateinit var second: IntArray
15+
private lateinit var ans: IntArray
16+
17+
fun timeTaken(edges: Array<IntArray>): IntArray {
18+
val n = edges.size + 1
19+
head = IntArray(n)
20+
nxt = IntArray(n shl 1)
21+
to = IntArray(n shl 1)
22+
head.fill(-1)
23+
var i = 0
24+
var j = 2
25+
while (i < edges.size) {
26+
val u = edges[i][0]
27+
val v = edges[i][1]
28+
nxt[j] = head[u]
29+
head[u] = j
30+
to[j] = v
31+
j++
32+
nxt[j] = head[v]
33+
head[v] = j
34+
to[j] = u
35+
j++
36+
i++
37+
}
38+
last = IntArray(n)
39+
lastNo = IntArray(n)
40+
second = IntArray(n)
41+
ans = IntArray(n)
42+
dfs(-1, 0)
43+
System.arraycopy(last, 0, ans, 0, n)
44+
dfs2(-1, 0, 0)
45+
return ans
46+
}
47+
48+
private fun dfs2(f: Int, u: Int, preLast: Int) {
49+
var e = head[u]
50+
var v: Int
51+
while (e != -1) {
52+
v = to[e]
53+
if (f != v) {
54+
val pl = if (v == lastNo[u]) {
55+
(
56+
max(
57+
preLast,
58+
second[u]
59+
) + (if ((u and 1) == 0) 2 else 1)
60+
)
61+
} else {
62+
(
63+
max(
64+
preLast,
65+
last[u]
66+
) + (if ((u and 1) == 0) 2 else 1)
67+
)
68+
}
69+
ans[v] = max(ans[v], pl)
70+
dfs2(u, v, pl)
71+
}
72+
e = nxt[e]
73+
}
74+
}
75+
76+
private fun dfs(f: Int, u: Int) {
77+
var e = head[u]
78+
var v: Int
79+
while (e != -1) {
80+
v = to[e]
81+
if (f != v) {
82+
dfs(u, v)
83+
val t = last[v] + (if ((v and 1) == 0) 2 else 1)
84+
if (last[u] < t) {
85+
second[u] = last[u]
86+
last[u] = t
87+
lastNo[u] = v
88+
} else if (second[u] < t) {
89+
second[u] = t
90+
}
91+
}
92+
e = nxt[e]
93+
}
94+
}
95+
}

0 commit comments

Comments
 (0)