Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 54d263d

Browse files
authored
Added tasks 3238-3245
1 parent 6b1ee62 commit 54d263d

File tree

24 files changed

+1257
-0
lines changed

24 files changed

+1257
-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_06_Time_1_ms_(100.00%)_Space_44.5_MB_(99.46%)
4+
5+
@SuppressWarnings({"unused", "java:S1172"})
6+
public class Solution {
7+
public int winningPlayerCount(int n, int[][] pick) {
8+
int[][] dp = new int[11][11];
9+
for (int[] ints : pick) {
10+
int p = ints[0];
11+
int pi = ints[1];
12+
dp[p][pi] += 1;
13+
}
14+
int count = 0;
15+
for (int i = 0; i < 11; i++) {
16+
boolean win = false;
17+
for (int j = 0; j < 11; j++) {
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: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package g3201_3300.s3239_minimum_number_of_flips_to_make_binary_grid_palindromic_i;
2+
3+
// #Medium #Array #Matrix #Two_Pointers #2024_08_06_Time_3_ms_(100.00%)_Space_111.4_MB_(41.81%)
4+
5+
public class Solution {
6+
public int minFlips(int[][] grid) {
7+
int m = grid.length;
8+
int n = grid[0].length;
9+
int rowFlips = 0;
10+
for (int i = 0; i < m / 2; i++) {
11+
for (int j = 0; j < n; j++) {
12+
int sum = grid[i][j] + grid[m - 1 - i][j];
13+
rowFlips += Math.min(sum, 2 - sum);
14+
}
15+
}
16+
int columnFlips = 0;
17+
for (int j = 0; j < n / 2; j++) {
18+
for (int[] ints : grid) {
19+
int sum = ints[j] + ints[n - 1 - j];
20+
columnFlips += Math.min(sum, 2 - sum);
21+
}
22+
}
23+
return Math.min(rowFlips, columnFlips);
24+
}
25+
}
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: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package g3201_3300.s3240_minimum_number_of_flips_to_make_binary_grid_palindromic_ii;
2+
3+
// #Medium #Array #Matrix #Two_Pointers #2024_08_06_Time_3_ms_(96.90%)_Space_93.8_MB_(76.14%)
4+
5+
public class Solution {
6+
public int minFlips(int[][] grid) {
7+
int res = 0;
8+
int one = 0;
9+
int diff = 0;
10+
int m = grid.length;
11+
int n = grid[0].length;
12+
// Handle quadrants
13+
for (int i = 0; i < m / 2; ++i) {
14+
for (int j = 0; j < n / 2; ++j) {
15+
int v =
16+
grid[i][j]
17+
+ grid[i][n - 1 - j]
18+
+ grid[m - 1 - i][j]
19+
+ grid[m - 1 - i][n - 1 - j];
20+
res += Math.min(v, 4 - v);
21+
}
22+
}
23+
// Handle middle column
24+
if (n % 2 > 0) {
25+
for (int i = 0; i < m / 2; ++i) {
26+
diff += grid[i][n / 2] ^ grid[m - 1 - i][n / 2];
27+
one += grid[i][n / 2] + grid[m - 1 - i][n / 2];
28+
}
29+
}
30+
// Handle middle row
31+
if (m % 2 > 0) {
32+
for (int j = 0; j < n / 2; ++j) {
33+
diff += grid[m / 2][j] ^ grid[m / 2][n - 1 - j];
34+
one += grid[m / 2][j] + grid[m / 2][n - 1 - j];
35+
}
36+
}
37+
// Handle center point
38+
if (n % 2 > 0 && m % 2 > 0) {
39+
res += grid[m / 2][n / 2];
40+
}
41+
// Divisible by 4
42+
if (diff == 0 && one % 4 > 0) {
43+
res += 2;
44+
}
45+
return res + diff;
46+
}
47+
}
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: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
package g3201_3300.s3241_time_taken_to_mark_all_nodes;
2+
3+
// #Hard #Dynamic_Programming #Tree #Graph #Depth_First_Search
4+
// #2024_08_06_Time_39_ms_(100.00%)_Space_115.8_MB_(83.90%)
5+
6+
import java.util.Arrays;
7+
8+
public class Solution {
9+
private int[] head;
10+
private int[] nxt;
11+
private int[] to;
12+
private int[] last;
13+
private int[] lastNo;
14+
private int[] second;
15+
private int[] ans;
16+
17+
public int[] timeTaken(int[][] edges) {
18+
int n = edges.length + 1;
19+
head = new int[n];
20+
nxt = new int[n << 1];
21+
to = new int[n << 1];
22+
Arrays.fill(head, -1);
23+
int i = 0;
24+
int j = 2;
25+
while (i < edges.length) {
26+
int u = edges[i][0];
27+
int 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 = new int[n];
39+
lastNo = new int[n];
40+
second = new int[n];
41+
ans = new int[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 void dfs2(int f, int u, int preLast) {
49+
int e = head[u];
50+
int v;
51+
while (e != -1) {
52+
v = to[e];
53+
if (f != v) {
54+
int pl;
55+
if (v == lastNo[u]) {
56+
pl = Math.max(preLast, second[u]) + ((u & 1) == 0 ? 2 : 1);
57+
} else {
58+
pl = Math.max(preLast, last[u]) + ((u & 1) == 0 ? 2 : 1);
59+
}
60+
ans[v] = Math.max(ans[v], pl);
61+
dfs2(u, v, pl);
62+
}
63+
e = nxt[e];
64+
}
65+
}
66+
67+
private void dfs(int f, int u) {
68+
int e = head[u];
69+
int v;
70+
while (e != -1) {
71+
v = to[e];
72+
if (f != v) {
73+
dfs(u, v);
74+
int t = last[v] + ((v & 1) == 0 ? 2 : 1);
75+
if (last[u] < t) {
76+
second[u] = last[u];
77+
last[u] = t;
78+
lastNo[u] = v;
79+
} else if (second[u] < t) {
80+
second[u] = t;
81+
}
82+
}
83+
e = nxt[e];
84+
}
85+
}
86+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /