Skip to content

Added tasks 3238-3245 #675

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 1 commit into from
Aug 7, 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
Original file line number Diff line number Diff line change
@@ -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<IntArray>): 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
}
}
Original file line number Diff line number Diff line change
@@ -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 <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>.

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`
* <code>0 <= x<sub>i</sub> <= n - 1</code>
* <code>0 <= y<sub>i</sub> <= 10</code>
Original file line number Diff line number Diff line change
@@ -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<IntArray>): 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)
}
}
Original file line number Diff line number Diff line change
@@ -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`
* <code>1 <= m * n <= 2 * 10<sup>5</sup></code>
* `0 <= grid[i][j] <= 1`
Original file line number Diff line number Diff line change
@@ -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<IntArray>): 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
}
}
Original file line number Diff line number Diff line change
@@ -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`
* <code>1 <= m * n <= 2 * 10<sup>5</sup></code>
* `0 <= grid[i][j] <= 1`
Original file line number Diff line number Diff line change
@@ -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>): 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]
}
}
}
Loading