forked from javadev/LeetCode-in-Java
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
24 changed files
with
1,257 additions
and
0 deletions.
There are no files selected for viewing
29 changes: 29 additions & 0 deletions
29
src/main/java/g3201_3300/s3238_find_the_number_of_winning_players/Solution.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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_06_Time_1_ms_(100.00%)_Space_44.5_MB_(99.46%) | ||
|
||
@SuppressWarnings({"unused", "java:S1172"}) | ||
public class Solution { | ||
public int winningPlayerCount(int n, int[][] pick) { | ||
int[][] dp = new int[11][11]; | ||
for (int[] ints : pick) { | ||
int p = ints[0]; | ||
int pi = ints[1]; | ||
dp[p][pi] += 1; | ||
} | ||
int count = 0; | ||
for (int i = 0; i < 11; i++) { | ||
boolean win = false; | ||
for (int j = 0; j < 11; j++) { | ||
if (dp[i][j] >= i + 1) { | ||
win = true; | ||
break; | ||
} | ||
} | ||
if (win) { | ||
count += 1; | ||
} | ||
} | ||
return count; | ||
} | ||
} |
54 changes: 54 additions & 0 deletions
54
src/main/java/g3201_3300/s3238_find_the_number_of_winning_players/readme.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
25 changes: 25 additions & 0 deletions
25
.../g3201_3300/s3239_minimum_number_of_flips_to_make_binary_grid_palindromic_i/Solution.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package g3201_3300.s3239_minimum_number_of_flips_to_make_binary_grid_palindromic_i; | ||
|
||
// #Medium #Array #Matrix #Two_Pointers #2024_08_06_Time_3_ms_(100.00%)_Space_111.4_MB_(41.81%) | ||
|
||
public class Solution { | ||
public int minFlips(int[][] grid) { | ||
int m = grid.length; | ||
int n = grid[0].length; | ||
int rowFlips = 0; | ||
for (int i = 0; i < m / 2; i++) { | ||
for (int j = 0; j < n; j++) { | ||
int sum = grid[i][j] + grid[m - 1 - i][j]; | ||
rowFlips += Math.min(sum, 2 - sum); | ||
} | ||
} | ||
int columnFlips = 0; | ||
for (int j = 0; j < n / 2; j++) { | ||
for (int[] ints : grid) { | ||
int sum = ints[j] + ints[n - 1 - j]; | ||
columnFlips += Math.min(sum, 2 - sum); | ||
} | ||
} | ||
return Math.min(rowFlips, columnFlips); | ||
} | ||
} |
52 changes: 52 additions & 0 deletions
52
..._3300/s3239_minimum_number_of_flips_to_make_binary_grid_palindromic_i/readme.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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` |
47 changes: 47 additions & 0 deletions
47
...g3201_3300/s3240_minimum_number_of_flips_to_make_binary_grid_palindromic_ii/Solution.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
package g3201_3300.s3240_minimum_number_of_flips_to_make_binary_grid_palindromic_ii; | ||
|
||
// #Medium #Array #Matrix #Two_Pointers #2024_08_06_Time_3_ms_(96.90%)_Space_93.8_MB_(76.14%) | ||
|
||
public class Solution { | ||
public int minFlips(int[][] grid) { | ||
int res = 0; | ||
int one = 0; | ||
int diff = 0; | ||
int m = grid.length; | ||
int n = grid[0].length; | ||
// Handle quadrants | ||
for (int i = 0; i < m / 2; ++i) { | ||
for (int j = 0; j < n / 2; ++j) { | ||
int v = | ||
grid[i][j] | ||
+ grid[i][n - 1 - j] | ||
+ grid[m - 1 - i][j] | ||
+ grid[m - 1 - i][n - 1 - j]; | ||
res += Math.min(v, 4 - v); | ||
} | ||
} | ||
// Handle middle column | ||
if (n % 2 > 0) { | ||
for (int i = 0; i < m / 2; ++i) { | ||
diff += grid[i][n / 2] ^ 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 (int j = 0; j < n / 2; ++j) { | ||
diff += grid[m / 2][j] ^ 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; | ||
} | ||
} |
48 changes: 48 additions & 0 deletions
48
...3300/s3240_minimum_number_of_flips_to_make_binary_grid_palindromic_ii/readme.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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` |
86 changes: 86 additions & 0 deletions
86
src/main/java/g3201_3300/s3241_time_taken_to_mark_all_nodes/Solution.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
package g3201_3300.s3241_time_taken_to_mark_all_nodes; | ||
|
||
// #Hard #Dynamic_Programming #Tree #Graph #Depth_First_Search | ||
// #2024_08_06_Time_39_ms_(100.00%)_Space_115.8_MB_(83.90%) | ||
|
||
import java.util.Arrays; | ||
|
||
public class Solution { | ||
private int[] head; | ||
private int[] nxt; | ||
private int[] to; | ||
private int[] last; | ||
private int[] lastNo; | ||
private int[] second; | ||
private int[] ans; | ||
|
||
public int[] timeTaken(int[][] edges) { | ||
int n = edges.length + 1; | ||
head = new int[n]; | ||
nxt = new int[n << 1]; | ||
to = new int[n << 1]; | ||
Arrays.fill(head, -1); | ||
int i = 0; | ||
int j = 2; | ||
while (i < edges.length) { | ||
int u = edges[i][0]; | ||
int 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 = new int[n]; | ||
lastNo = new int[n]; | ||
second = new int[n]; | ||
ans = new int[n]; | ||
dfs(-1, 0); | ||
System.arraycopy(last, 0, ans, 0, n); | ||
dfs2(-1, 0, 0); | ||
return ans; | ||
} | ||
|
||
private void dfs2(int f, int u, int preLast) { | ||
int e = head[u]; | ||
int v; | ||
while (e != -1) { | ||
v = to[e]; | ||
if (f != v) { | ||
int pl; | ||
if (v == lastNo[u]) { | ||
pl = Math.max(preLast, second[u]) + ((u & 1) == 0 ? 2 : 1); | ||
} else { | ||
pl = Math.max(preLast, last[u]) + ((u & 1) == 0 ? 2 : 1); | ||
} | ||
ans[v] = Math.max(ans[v], pl); | ||
dfs2(u, v, pl); | ||
} | ||
e = nxt[e]; | ||
} | ||
} | ||
|
||
private void dfs(int f, int u) { | ||
int e = head[u]; | ||
int v; | ||
while (e != -1) { | ||
v = to[e]; | ||
if (f != v) { | ||
dfs(u, v); | ||
int t = last[v] + ((v & 1) == 0 ? 2 : 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]; | ||
} | ||
} | ||
} |
Oops, something went wrong.