Skip to content

Commit 9c273a8

Browse files
authored
Added tasks 3127-3134
1 parent 6f9e32f commit 9c273a8

File tree

24 files changed

+952
-0
lines changed

24 files changed

+952
-0
lines changed
Lines changed: 29 additions & 0 deletions
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_149_ms_(80.00%)_Space_35.1_MB_(40.00%)
4+
5+
class Solution {
6+
fun canMakeSquare(grid: Array<CharArray>): Boolean {
7+
val n = grid.size
8+
val m = grid[0].size
9+
for (i in 0 until n - 1) {
10+
for (j in 0 until m - 1) {
11+
var countBlack = 0
12+
var countWhite = 0
13+
for (k in i..i + 1) {
14+
for (l in j..j + 1) {
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+
}
Lines changed: 45 additions & 0 deletions
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'`.
Lines changed: 26 additions & 0 deletions
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_975_ms_(40.63%)_Space_217.6_MB_(56.25%)
5+
6+
class Solution {
7+
fun numberOfRightTriangles(grid: Array<IntArray>): Long {
8+
val n = grid.size
9+
val m = grid[0].size
10+
val columns = IntArray(n)
11+
val rows = IntArray(m)
12+
var sum: Long = 0
13+
for (i in 0 until n) {
14+
for (j in 0 until m) {
15+
columns[i] += grid[i][j]
16+
rows[j] += grid[i][j]
17+
}
18+
}
19+
for (i in 0 until n) {
20+
for (j in 0 until m) {
21+
sum += grid[i][j].toLong() * (rows[j] - 1) * (columns[i] - 1)
22+
}
23+
}
24+
return sum
25+
}
26+
}
Lines changed: 77 additions & 0 deletions
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`
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
package g3101_3200.s3129_find_all_possible_stable_binary_arrays_i
2+
3+
// #Medium #Dynamic_Programming #Prefix_Sum #2024_05_02_Time_169_ms_(92.86%)_Space_36.3_MB_(100.00%)
4+
5+
import kotlin.math.abs
6+
import kotlin.math.max
7+
import kotlin.math.min
8+
9+
class Solution {
10+
private fun add(x: Int, y: Int): Int {
11+
return (x + y) % MODULUS
12+
}
13+
14+
private fun subtract(x: Int, y: Int): Int {
15+
return (x + MODULUS - y) % MODULUS
16+
}
17+
18+
private fun multiply(x: Int, y: Int): Int {
19+
return (x.toLong() * y % MODULUS).toInt()
20+
}
21+
22+
fun numberOfStableArrays(zero: Int, one: Int, limit: Int): Int {
23+
if (limit == 1) {
24+
return max((2 - abs((zero - one))), 0)
25+
}
26+
val max = max(zero, one)
27+
val min = min(zero, one)
28+
val lcn = Array(max + 1) { IntArray(max + 1) }
29+
var row0 = lcn[0]
30+
var row1: IntArray
31+
var row2: IntArray
32+
row0[0] = 1
33+
var s = 1
34+
var sLim = s - limit
35+
while (s <= max) {
36+
row2 = if (sLim > 0) lcn[sLim - 1] else intArrayOf()
37+
row1 = row0
38+
row0 = lcn[s]
39+
var c = (s - 1) / limit + 1
40+
while (c <= sLim) {
41+
row0[c] = subtract(add(row1[c], row1[c - 1]), row2[c - 1])
42+
c++
43+
}
44+
while (c <= s) {
45+
row0[c] = add(row1[c], row1[c - 1])
46+
c++
47+
}
48+
s++
49+
sLim++
50+
}
51+
row1 = lcn[min]
52+
var result = 0
53+
var s0 = add(if (min < max) row0[min + 1] else 0, row0[min])
54+
for (c in min downTo 1) {
55+
val s1 = s0
56+
s0 = add(row0[c], row0[c - 1])
57+
result = add(result, multiply(row1[c], add(s0, s1)))
58+
}
59+
return result
60+
}
61+
62+
companion object {
63+
private const val MODULUS = 1e9.toInt() + 7
64+
}
65+
}
Lines changed: 51 additions & 0 deletions
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`
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
package g3101_3200.s3130_find_all_possible_stable_binary_arrays_ii
2+
3+
// #Hard #Dynamic_Programming #Prefix_Sum #2024_05_02_Time_242_ms_(100.00%)_Space_36.7_MB_(100.00%)
4+
5+
import kotlin.math.max
6+
import kotlin.math.min
7+
8+
class Solution {
9+
private var factorial: LongArray? = null
10+
private lateinit var reverse: LongArray
11+
12+
fun numberOfStableArrays(zero: Int, one: Int, limit: Int): Int {
13+
if (factorial == null) {
14+
factorial = LongArray(N + 1)
15+
reverse = LongArray(N + 1)
16+
factorial!![0] = 1
17+
reverse[0] = 1
18+
var x: Long = 1
19+
for (i in 1..N) {
20+
x = (x * i) % MOD
21+
factorial!![i] = x.toInt().toLong()
22+
reverse[i] = getInverse(x, MOD.toLong())
23+
}
24+
}
25+
var ans: Long = 0
26+
val s = LongArray(one + 1)
27+
val n = (min(zero, one) + 1).toInt()
28+
for (
29+
groups0 in (zero + limit - 1) / limit..min(zero, n)
30+
.toInt()
31+
) {
32+
val s0 = calc(groups0, zero, limit)
33+
for (
34+
groups1 in max(
35+
groups0 - 1,
36+
(one + limit - 1) / limit
37+
)..min((groups0 + 1), one)
38+
) {
39+
var s1: Long
40+
if (s[groups1] != 0L) {
41+
s1 = s[groups1]
42+
} else {
43+
s[groups1] = calc(groups1, one, limit)
44+
s1 = s[groups1]
45+
}
46+
ans = (ans + s0 * s1 * (if (groups1 == groups0) 2 else 1)) % MOD
47+
}
48+
}
49+
return ((ans + MOD) % MOD).toInt()
50+
}
51+
52+
fun calc(groups: Int, x: Int, limit: Int): Long {
53+
var s: Long = 0
54+
var sign = 1
55+
var k = 0
56+
while (k * limit <= x - groups && k <= groups) {
57+
s = (s + sign * comb(groups, k) * comb(x - k * limit - 1, groups - 1)) % MOD
58+
sign *= -1
59+
k++
60+
}
61+
return s
62+
}
63+
64+
fun comb(n: Int, k: Int): Long {
65+
return (factorial!![n] * reverse[k] % MOD) * reverse[n - k] % MOD
66+
}
67+
68+
fun getInverse(n: Long, mod: Long): Long {
69+
var n = n
70+
var p = mod
71+
var x: Long = 1
72+
var y: Long = 0
73+
while (p > 0) {
74+
val quotient = n / p
75+
val remainder = n % p
76+
val tempY = x - quotient * y
77+
x = y
78+
y = tempY
79+
n = p
80+
p = remainder
81+
}
82+
return ((x % mod) + mod) % mod
83+
}
84+
85+
companion object {
86+
private const val MOD = 1e9.toInt() + 7
87+
private const val N = 1000
88+
}
89+
}

0 commit comments

Comments
 (0)