Skip to content

Commit cacab6a

Browse files
authored
Added tasks 3127-3134
1 parent f17dfa3 commit cacab6a

File tree

24 files changed

+916
-0
lines changed

24 files changed

+916
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package g3101_3200.s3127_make_a_square_with_the_same_color;
2+
3+
// #Easy #Array #Matrix #Enumeration #2024_05_02_Time_0_ms_(100.00%)_Space_41.7_MB_(64.59%)
4+
5+
public class Solution {
6+
public boolean canMakeSquare(char[][] grid) {
7+
int n = grid.length;
8+
int m = grid[0].length;
9+
for (int i = 0; i < n - 1; i++) {
10+
for (int j = 0; j < m - 1; j++) {
11+
int countBlack = 0;
12+
int countWhite = 0;
13+
for (int k = i; k <= i + 1; k++) {
14+
for (int l = j; l <= j + 1; l++) {
15+
if (grid[k][l] == 'W') {
16+
countWhite++;
17+
} else {
18+
countBlack++;
19+
}
20+
}
21+
}
22+
if (countBlack >= 3 || countWhite >= 3) {
23+
return true;
24+
}
25+
}
26+
}
27+
return false;
28+
}
29+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
3127\. Make a Square with the Same Color
2+
3+
Easy
4+
5+
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.
6+
7+
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.
8+
9+
Return `true` if it is possible to create a `2 x 2` square of the same color, otherwise, return `false`.
10+
11+
**Example 1:**
12+
13+
**Input:** grid = [["B","W","B"],["B","W","W"],["B","W","B"]]
14+
15+
**Output:** true
16+
17+
**Explanation:**
18+
19+
It can be done by changing the color of the `grid[0][2]`.
20+
21+
**Example 2:**
22+
23+
**Input:** grid = [["B","W","B"],["W","B","W"],["B","W","B"]]
24+
25+
**Output:** false
26+
27+
**Explanation:**
28+
29+
It cannot be done by changing at most one cell.
30+
31+
**Example 3:**
32+
33+
**Input:** grid = [["B","W","B"],["B","W","W"],["B","W","W"]]
34+
35+
**Output:** true
36+
37+
**Explanation:**
38+
39+
The `grid` already contains a `2 x 2` square of the same color.
40+
41+
**Constraints:**
42+
43+
* `grid.length == 3`
44+
* `grid[i].length == 3`
45+
* `grid[i][j]` is either `'W'` or `'B'`.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package g3101_3200.s3128_right_triangles;
2+
3+
// #Medium #Array #Hash_Table #Math #Counting #Combinatorics
4+
// #2024_05_02_Time_6_ms_(100.00%)_Space_145.9_MB_(90.67%)
5+
6+
public class Solution {
7+
public long numberOfRightTriangles(int[][] grid) {
8+
int n = grid.length;
9+
int m = grid[0].length;
10+
int[] columns = new int[n];
11+
int[] rows = new int[m];
12+
long sum = 0;
13+
for (int i = 0; i < n; i++) {
14+
for (int j = 0; j < m; j++) {
15+
columns[i] += grid[i][j];
16+
rows[j] += grid[i][j];
17+
}
18+
}
19+
for (int i = 0; i < n; i++) {
20+
for (int j = 0; j < m; j++) {
21+
sum += (long) grid[i][j] * (rows[j] - 1) * (columns[i] - 1);
22+
}
23+
}
24+
return sum;
25+
}
26+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
3128\. Right Triangles
2+
3+
Medium
4+
5+
You are given a 2D boolean matrix `grid`.
6+
7+
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.
8+
9+
**Note:**
10+
11+
* 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.
12+
13+
**Example 1:**
14+
15+
0 **1** 0
16+
17+
0 **1 1**
18+
19+
0 1 0
20+
21+
0 1 0
22+
23+
0 **1 1**
24+
25+
0 **1** 0
26+
27+
**Input:** grid = [[0,1,0],[0,1,1],[0,1,0]]
28+
29+
**Output:** 2
30+
31+
**Explanation:**
32+
33+
There are two right triangles.
34+
35+
**Example 2:**
36+
37+
1 0 0 0
38+
39+
0 1 0 1
40+
41+
1 0 0 0
42+
43+
**Input:** grid = [[1,0,0,0],[0,1,0,1],[1,0,0,0]]
44+
45+
**Output:** 0
46+
47+
**Explanation:**
48+
49+
There are no right triangles.
50+
51+
**Example 3:**
52+
53+
**1** 0 **1**
54+
55+
**1** 0 0
56+
57+
1 0 0
58+
59+
**1** 0 **1**
60+
61+
1 0 0
62+
63+
**1** 0 0
64+
65+
**Input:** grid = [[1,0,1],[1,0,0],[1,0,0]]
66+
67+
**Output: **2
68+
69+
**Explanation:**
70+
71+
There are two right triangles.
72+
73+
**Constraints:**
74+
75+
* `1 <= grid.length <= 1000`
76+
* `1 <= grid[i].length <= 1000`
77+
* `0 <= grid[i][j] <= 1`
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
package g3101_3200.s3129_find_all_possible_stable_binary_arrays_i;
2+
3+
// #Medium #Dynamic_Programming #Prefix_Sum #2024_05_02_Time_3_ms_(100.00%)_Space_44.1_MB_(98.38%)
4+
5+
public class Solution {
6+
private static final int MODULUS = (int) 1e9 + 7;
7+
8+
private int add(int x, int y) {
9+
return (x + y) % MODULUS;
10+
}
11+
12+
private int subtract(int x, int y) {
13+
return (x + MODULUS - y) % MODULUS;
14+
}
15+
16+
private int multiply(int x, int y) {
17+
return (int) ((long) x * y % MODULUS);
18+
}
19+
20+
public int numberOfStableArrays(int zero, int one, int limit) {
21+
if (limit == 1) {
22+
return Math.max(2 - Math.abs(zero - one), 0);
23+
}
24+
int max = Math.max(zero, one);
25+
int min = Math.min(zero, one);
26+
int[][] lcn = new int[max + 1][max + 1];
27+
int[] row0 = lcn[0];
28+
int[] row1;
29+
int[] row2;
30+
row0[0] = 1;
31+
for (int s = 1, sLim = s - limit; s <= max; s++, sLim++) {
32+
row2 = sLim > 0 ? lcn[sLim - 1] : new int[] {};
33+
row1 = row0;
34+
row0 = lcn[s];
35+
int c;
36+
for (c = (s - 1) / limit + 1; c <= sLim; c++) {
37+
row0[c] = subtract(add(row1[c], row1[c - 1]), row2[c - 1]);
38+
}
39+
for (; c <= s; c++) {
40+
row0[c] = add(row1[c], row1[c - 1]);
41+
}
42+
}
43+
row1 = lcn[min];
44+
int result = 0;
45+
int s0 = add(min < max ? row0[min + 1] : 0, row0[min]);
46+
for (int c = min; c > 0; c--) {
47+
int s1 = s0;
48+
s0 = add(row0[c], row0[c - 1]);
49+
result = add(result, multiply(row1[c], add(s0, s1)));
50+
}
51+
return result;
52+
}
53+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
3129\. Find All Possible Stable Binary Arrays I
2+
3+
Medium
4+
5+
You are given 3 positive integers `zero`, `one`, and `limit`.
6+
7+
A binary array `arr` is called **stable** if:
8+
9+
* The number of occurrences of 0 in `arr` is **exactly** `zero`.
10+
* The number of occurrences of 1 in `arr` is **exactly** `one`.
11+
* Each subarray of `arr` with a size greater than `limit` must contain **both** 0 and 1.
12+
13+
Return the _total_ number of **stable** binary arrays.
14+
15+
Since the answer may be very large, return it **modulo** <code>10<sup>9</sup> + 7</code>.
16+
17+
**Example 1:**
18+
19+
**Input:** zero = 1, one = 1, limit = 2
20+
21+
**Output:** 2
22+
23+
**Explanation:**
24+
25+
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.
26+
27+
**Example 2:**
28+
29+
**Input:** zero = 1, one = 2, limit = 1
30+
31+
**Output:** 1
32+
33+
**Explanation:**
34+
35+
The only possible stable binary array is `[1,0,1]`.
36+
37+
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.
38+
39+
**Example 3:**
40+
41+
**Input:** zero = 3, one = 3, limit = 2
42+
43+
**Output:** 14
44+
45+
**Explanation:**
46+
47+
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]`.
48+
49+
**Constraints:**
50+
51+
* `1 <= zero, one, limit <= 200`
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
package g3101_3200.s3130_find_all_possible_stable_binary_arrays_ii;
2+
3+
// #Hard #Dynamic_Programming #Prefix_Sum #2024_05_02_Time_3_ms_(100.00%)_Space_40.6_MB_(100.00%)
4+
5+
public class Solution {
6+
private static final int MOD = (int) 1e9 + 7;
7+
private static final int N = 1000;
8+
private long[] factorial;
9+
private long[] reverse;
10+
11+
public int numberOfStableArrays(int zero, int one, int limit) {
12+
if (factorial == null) {
13+
factorial = new long[N + 1];
14+
reverse = new long[N + 1];
15+
factorial[0] = 1;
16+
reverse[0] = 1;
17+
long x = 1;
18+
for (int i = 1; i <= N; ++i) {
19+
x = (x * i) % MOD;
20+
factorial[i] = (int) x;
21+
reverse[i] = getInverse(x, MOD);
22+
}
23+
}
24+
long ans = 0;
25+
long[] s = new long[one + 1];
26+
int n = Math.min(zero, one) + 1;
27+
for (int groups0 = (zero + limit - 1) / limit; groups0 <= Math.min(zero, n); ++groups0) {
28+
long s0 = calc(groups0, zero, limit);
29+
for (int groups1 = Math.max(groups0 - 1, (one + limit - 1) / limit);
30+
groups1 <= Math.min(groups0 + 1, one);
31+
++groups1) {
32+
long s1;
33+
if (s[groups1] != 0) {
34+
s1 = s[groups1];
35+
} else {
36+
s1 = s[groups1] = calc(groups1, one, limit);
37+
}
38+
ans = (ans + s0 * s1 * (groups1 == groups0 ? 2 : 1)) % MOD;
39+
}
40+
}
41+
return (int) ((ans + MOD) % MOD);
42+
}
43+
44+
long calc(int groups, int x, int limit) {
45+
long s = 0;
46+
int sign = 1;
47+
for (int k = 0; k * limit <= x - groups && k <= groups; k++) {
48+
s = (s + sign * comb(groups, k) * comb(x - k * limit - 1, groups - 1)) % MOD;
49+
sign *= -1;
50+
}
51+
return s;
52+
}
53+
54+
public long comb(int n, int k) {
55+
return (factorial[n] * reverse[k] % MOD) * reverse[n - k] % MOD;
56+
}
57+
58+
public long getInverse(long n, long mod) {
59+
long p = mod;
60+
long x = 1;
61+
long y = 0;
62+
while (p > 0) {
63+
long quotient = n / p;
64+
long remainder = n % p;
65+
long tempY = x - quotient * y;
66+
x = y;
67+
y = tempY;
68+
n = p;
69+
p = remainder;
70+
}
71+
return ((x % mod) + mod) % mod;
72+
}
73+
74+
public long quickPower(long base, long power, long p) {
75+
long result = 1;
76+
while (power > 0) {
77+
if ((power & 1) == 1) {
78+
result = result * base % p;
79+
}
80+
power >>= 1;
81+
base = base * base % p;
82+
}
83+
return result;
84+
}
85+
}

0 commit comments

Comments
 (0)