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
916 additions
and
0 deletions.
There are no files selected for viewing
29 changes: 29 additions & 0 deletions
29
src/main/java/g3101_3200/s3127_make_a_square_with_the_same_color/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 g3101_3200.s3127_make_a_square_with_the_same_color; | ||
|
||
// #Easy #Array #Matrix #Enumeration #2024_05_02_Time_0_ms_(100.00%)_Space_41.7_MB_(64.59%) | ||
|
||
public class Solution { | ||
public boolean canMakeSquare(char[][] grid) { | ||
int n = grid.length; | ||
int m = grid[0].length; | ||
for (int i = 0; i < n - 1; i++) { | ||
for (int j = 0; j < m - 1; j++) { | ||
int countBlack = 0; | ||
int countWhite = 0; | ||
for (int k = i; k <= i + 1; k++) { | ||
for (int l = j; l <= j + 1; l++) { | ||
if (grid[k][l] == 'W') { | ||
countWhite++; | ||
} else { | ||
countBlack++; | ||
} | ||
} | ||
} | ||
if (countBlack >= 3 || countWhite >= 3) { | ||
return true; | ||
} | ||
} | ||
} | ||
return false; | ||
} | ||
} |
45 changes: 45 additions & 0 deletions
45
src/main/java/g3101_3200/s3127_make_a_square_with_the_same_color/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,45 @@ | ||
3127\. Make a Square with the Same Color | ||
|
||
Easy | ||
|
||
You are given a 2D matrix `grid` of size `3 x 3` consisting only of characters `'B'` and `'W'`. Character `'W'` represents the white color, and character `'B'` represents the black color. | ||
|
||
Your task is to change the color of **at most one** cell so that the matrix has a `2 x 2` square where all cells are of the same color. | ||
|
||
Return `true` if it is possible to create a `2 x 2` square of the same color, otherwise, return `false`. | ||
|
||
**Example 1:** | ||
|
||
**Input:** grid = [["B","W","B"],["B","W","W"],["B","W","B"]] | ||
|
||
**Output:** true | ||
|
||
**Explanation:** | ||
|
||
It can be done by changing the color of the `grid[0][2]`. | ||
|
||
**Example 2:** | ||
|
||
**Input:** grid = [["B","W","B"],["W","B","W"],["B","W","B"]] | ||
|
||
**Output:** false | ||
|
||
**Explanation:** | ||
|
||
It cannot be done by changing at most one cell. | ||
|
||
**Example 3:** | ||
|
||
**Input:** grid = [["B","W","B"],["B","W","W"],["B","W","W"]] | ||
|
||
**Output:** true | ||
|
||
**Explanation:** | ||
|
||
The `grid` already contains a `2 x 2` square of the same color. | ||
|
||
**Constraints:** | ||
|
||
* `grid.length == 3` | ||
* `grid[i].length == 3` | ||
* `grid[i][j]` is either `'W'` or `'B'`. |
26 changes: 26 additions & 0 deletions
26
src/main/java/g3101_3200/s3128_right_triangles/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,26 @@ | ||
package g3101_3200.s3128_right_triangles; | ||
|
||
// #Medium #Array #Hash_Table #Math #Counting #Combinatorics | ||
// #2024_05_02_Time_6_ms_(100.00%)_Space_145.9_MB_(90.67%) | ||
|
||
public class Solution { | ||
public long numberOfRightTriangles(int[][] grid) { | ||
int n = grid.length; | ||
int m = grid[0].length; | ||
int[] columns = new int[n]; | ||
int[] rows = new int[m]; | ||
long sum = 0; | ||
for (int i = 0; i < n; i++) { | ||
for (int j = 0; j < m; j++) { | ||
columns[i] += grid[i][j]; | ||
rows[j] += grid[i][j]; | ||
} | ||
} | ||
for (int i = 0; i < n; i++) { | ||
for (int j = 0; j < m; j++) { | ||
sum += (long) grid[i][j] * (rows[j] - 1) * (columns[i] - 1); | ||
} | ||
} | ||
return sum; | ||
} | ||
} |
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,77 @@ | ||
3128\. Right Triangles | ||
|
||
Medium | ||
|
||
You are given a 2D boolean matrix `grid`. | ||
|
||
Return an integer that is the number of **right triangles** that can be made with the 3 elements of `grid` such that **all** of them have a value of 1. | ||
|
||
**Note:** | ||
|
||
* A collection of 3 elements of `grid` is a **right triangle** if one of its elements is in the **same row** with another element and in the **same column** with the third element. The 3 elements do not have to be next to each other. | ||
|
||
**Example 1:** | ||
|
||
0 **1** 0 | ||
|
||
0 **1 1** | ||
|
||
0 1 0 | ||
|
||
0 1 0 | ||
|
||
0 **1 1** | ||
|
||
0 **1** 0 | ||
|
||
**Input:** grid = [[0,1,0],[0,1,1],[0,1,0]] | ||
|
||
**Output:** 2 | ||
|
||
**Explanation:** | ||
|
||
There are two right triangles. | ||
|
||
**Example 2:** | ||
|
||
1 0 0 0 | ||
|
||
0 1 0 1 | ||
|
||
1 0 0 0 | ||
|
||
**Input:** grid = [[1,0,0,0],[0,1,0,1],[1,0,0,0]] | ||
|
||
**Output:** 0 | ||
|
||
**Explanation:** | ||
|
||
There are no right triangles. | ||
|
||
**Example 3:** | ||
|
||
**1** 0 **1** | ||
|
||
**1** 0 0 | ||
|
||
1 0 0 | ||
|
||
**1** 0 **1** | ||
|
||
1 0 0 | ||
|
||
**1** 0 0 | ||
|
||
**Input:** grid = [[1,0,1],[1,0,0],[1,0,0]] | ||
|
||
**Output: **2 | ||
|
||
**Explanation:** | ||
|
||
There are two right triangles. | ||
|
||
**Constraints:** | ||
|
||
* `1 <= grid.length <= 1000` | ||
* `1 <= grid[i].length <= 1000` | ||
* `0 <= grid[i][j] <= 1` |
53 changes: 53 additions & 0 deletions
53
src/main/java/g3101_3200/s3129_find_all_possible_stable_binary_arrays_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,53 @@ | ||
package g3101_3200.s3129_find_all_possible_stable_binary_arrays_i; | ||
|
||
// #Medium #Dynamic_Programming #Prefix_Sum #2024_05_02_Time_3_ms_(100.00%)_Space_44.1_MB_(98.38%) | ||
|
||
public class Solution { | ||
private static final int MODULUS = (int) 1e9 + 7; | ||
|
||
private int add(int x, int y) { | ||
return (x + y) % MODULUS; | ||
} | ||
|
||
private int subtract(int x, int y) { | ||
return (x + MODULUS - y) % MODULUS; | ||
} | ||
|
||
private int multiply(int x, int y) { | ||
return (int) ((long) x * y % MODULUS); | ||
} | ||
|
||
public int numberOfStableArrays(int zero, int one, int limit) { | ||
if (limit == 1) { | ||
return Math.max(2 - Math.abs(zero - one), 0); | ||
} | ||
int max = Math.max(zero, one); | ||
int min = Math.min(zero, one); | ||
int[][] lcn = new int[max + 1][max + 1]; | ||
int[] row0 = lcn[0]; | ||
int[] row1; | ||
int[] row2; | ||
row0[0] = 1; | ||
for (int s = 1, sLim = s - limit; s <= max; s++, sLim++) { | ||
row2 = sLim > 0 ? lcn[sLim - 1] : new int[] {}; | ||
row1 = row0; | ||
row0 = lcn[s]; | ||
int c; | ||
for (c = (s - 1) / limit + 1; c <= sLim; c++) { | ||
row0[c] = subtract(add(row1[c], row1[c - 1]), row2[c - 1]); | ||
} | ||
for (; c <= s; c++) { | ||
row0[c] = add(row1[c], row1[c - 1]); | ||
} | ||
} | ||
row1 = lcn[min]; | ||
int result = 0; | ||
int s0 = add(min < max ? row0[min + 1] : 0, row0[min]); | ||
for (int c = min; c > 0; c--) { | ||
int s1 = s0; | ||
s0 = add(row0[c], row0[c - 1]); | ||
result = add(result, multiply(row1[c], add(s0, s1))); | ||
} | ||
return result; | ||
} | ||
} |
51 changes: 51 additions & 0 deletions
51
src/main/java/g3101_3200/s3129_find_all_possible_stable_binary_arrays_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,51 @@ | ||
3129\. Find All Possible Stable Binary Arrays I | ||
|
||
Medium | ||
|
||
You are given 3 positive integers `zero`, `one`, and `limit`. | ||
|
||
A binary array `arr` is called **stable** if: | ||
|
||
* The number of occurrences of 0 in `arr` is **exactly** `zero`. | ||
* The number of occurrences of 1 in `arr` is **exactly** `one`. | ||
* Each subarray of `arr` with a size greater than `limit` must contain **both** 0 and 1. | ||
|
||
Return the _total_ number of **stable** binary arrays. | ||
|
||
Since the answer may be very large, return it **modulo** <code>10<sup>9</sup> + 7</code>. | ||
|
||
**Example 1:** | ||
|
||
**Input:** zero = 1, one = 1, limit = 2 | ||
|
||
**Output:** 2 | ||
|
||
**Explanation:** | ||
|
||
The two possible stable binary arrays are `[1,0]` and `[0,1]`, as both arrays have a single 0 and a single 1, and no subarray has a length greater than 2. | ||
|
||
**Example 2:** | ||
|
||
**Input:** zero = 1, one = 2, limit = 1 | ||
|
||
**Output:** 1 | ||
|
||
**Explanation:** | ||
|
||
The only possible stable binary array is `[1,0,1]`. | ||
|
||
Note that the binary arrays `[1,1,0]` and `[0,1,1]` have subarrays of length 2 with identical elements, hence, they are not stable. | ||
|
||
**Example 3:** | ||
|
||
**Input:** zero = 3, one = 3, limit = 2 | ||
|
||
**Output:** 14 | ||
|
||
**Explanation:** | ||
|
||
All the possible stable binary arrays are `[0,0,1,0,1,1]`, `[0,0,1,1,0,1]`, `[0,1,0,0,1,1]`, `[0,1,0,1,0,1]`, `[0,1,0,1,1,0]`, `[0,1,1,0,0,1]`, `[0,1,1,0,1,0]`, `[1,0,0,1,0,1]`, `[1,0,0,1,1,0]`, `[1,0,1,0,0,1]`, `[1,0,1,0,1,0]`, `[1,0,1,1,0,0]`, `[1,1,0,0,1,0]`, and `[1,1,0,1,0,0]`. | ||
|
||
**Constraints:** | ||
|
||
* `1 <= zero, one, limit <= 200` |
85 changes: 85 additions & 0 deletions
85
src/main/java/g3101_3200/s3130_find_all_possible_stable_binary_arrays_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,85 @@ | ||
package g3101_3200.s3130_find_all_possible_stable_binary_arrays_ii; | ||
|
||
// #Hard #Dynamic_Programming #Prefix_Sum #2024_05_02_Time_3_ms_(100.00%)_Space_40.6_MB_(100.00%) | ||
|
||
public class Solution { | ||
private static final int MOD = (int) 1e9 + 7; | ||
private static final int N = 1000; | ||
private long[] factorial; | ||
private long[] reverse; | ||
|
||
public int numberOfStableArrays(int zero, int one, int limit) { | ||
if (factorial == null) { | ||
factorial = new long[N + 1]; | ||
reverse = new long[N + 1]; | ||
factorial[0] = 1; | ||
reverse[0] = 1; | ||
long x = 1; | ||
for (int i = 1; i <= N; ++i) { | ||
x = (x * i) % MOD; | ||
factorial[i] = (int) x; | ||
reverse[i] = getInverse(x, MOD); | ||
} | ||
} | ||
long ans = 0; | ||
long[] s = new long[one + 1]; | ||
int n = Math.min(zero, one) + 1; | ||
for (int groups0 = (zero + limit - 1) / limit; groups0 <= Math.min(zero, n); ++groups0) { | ||
long s0 = calc(groups0, zero, limit); | ||
for (int groups1 = Math.max(groups0 - 1, (one + limit - 1) / limit); | ||
groups1 <= Math.min(groups0 + 1, one); | ||
++groups1) { | ||
long s1; | ||
if (s[groups1] != 0) { | ||
s1 = s[groups1]; | ||
} else { | ||
s1 = s[groups1] = calc(groups1, one, limit); | ||
} | ||
ans = (ans + s0 * s1 * (groups1 == groups0 ? 2 : 1)) % MOD; | ||
} | ||
} | ||
return (int) ((ans + MOD) % MOD); | ||
} | ||
|
||
long calc(int groups, int x, int limit) { | ||
long s = 0; | ||
int sign = 1; | ||
for (int k = 0; k * limit <= x - groups && k <= groups; k++) { | ||
s = (s + sign * comb(groups, k) * comb(x - k * limit - 1, groups - 1)) % MOD; | ||
sign *= -1; | ||
} | ||
return s; | ||
} | ||
|
||
public long comb(int n, int k) { | ||
return (factorial[n] * reverse[k] % MOD) * reverse[n - k] % MOD; | ||
} | ||
|
||
public long getInverse(long n, long mod) { | ||
long p = mod; | ||
long x = 1; | ||
long y = 0; | ||
while (p > 0) { | ||
long quotient = n / p; | ||
long remainder = n % p; | ||
long tempY = x - quotient * y; | ||
x = y; | ||
y = tempY; | ||
n = p; | ||
p = remainder; | ||
} | ||
return ((x % mod) + mod) % mod; | ||
} | ||
|
||
public long quickPower(long base, long power, long p) { | ||
long result = 1; | ||
while (power > 0) { | ||
if ((power & 1) == 1) { | ||
result = result * base % p; | ||
} | ||
power >>= 1; | ||
base = base * base % p; | ||
} | ||
return result; | ||
} | ||
} |
Oops, something went wrong.