Skip to content

Commit 8cdabe7

Browse files
committed
Added tasks 3127-3134
1 parent 6f9e32f commit 8cdabe7

File tree

24 files changed

+956
-0
lines changed

24 files changed

+956
-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_0_ms_(100.00%)_Space_41.7_MB_(64.59%)
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: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package g3101_3200.s3128_right_triangles // #Medium #Array #Hash_Table #Math #Counting #Combinatorics
2+
// #2024_05_02_Time_6_ms_(100.00%)_Space_145.9_MB_(90.67%)
3+
4+
class Solution {
5+
fun numberOfRightTriangles(grid: Array<IntArray>): Long {
6+
val n = grid.size
7+
val m = grid[0].size
8+
val columns = IntArray(n)
9+
val rows = IntArray(m)
10+
var sum: Long = 0
11+
for (i in 0 until n) {
12+
for (j in 0 until m) {
13+
columns[i] += grid[i][j]
14+
rows[j] += grid[i][j]
15+
}
16+
}
17+
for (i in 0 until n) {
18+
for (j in 0 until m) {
19+
sum += grid[i][j].toLong() * (rows[j] - 1) * (columns[i] - 1)
20+
}
21+
}
22+
return sum
23+
}
24+
}
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+
import kotlin.math.abs
4+
import kotlin.math.max
5+
import kotlin.math.min
6+
7+
// #Medium #Dynamic_Programming #Prefix_Sum #2024_05_02_Time_3_ms_(100.00%)_Space_44.1_MB_(98.38%)
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).toDouble())).toDouble(), 0.0).toInt()
25+
}
26+
val max = max(zero.toDouble(), one.toDouble()).toInt()
27+
val min = min(zero.toDouble(), one.toDouble()).toInt()
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`

0 commit comments

Comments
 (0)