Skip to content

Added tasks 3127-3134 #641

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
May 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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_149_ms_(80.00%)_Space_35.1_MB_(40.00%)

class Solution {
fun canMakeSquare(grid: Array<CharArray>): Boolean {
val n = grid.size
val m = grid[0].size
for (i in 0 until n - 1) {
for (j in 0 until m - 1) {
var countBlack = 0
var countWhite = 0
for (k in i..i + 1) {
for (l in j..j + 1) {
if (grid[k][l] == 'W') {
countWhite++
} else {
countBlack++
}
}
}
if (countBlack >= 3 || countWhite >= 3) {
return true
}
}
}
return false
}
}
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 src/main/kotlin/g3101_3200/s3128_right_triangles/Solution.kt
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_975_ms_(40.63%)_Space_217.6_MB_(56.25%)

class Solution {
fun numberOfRightTriangles(grid: Array<IntArray>): Long {
val n = grid.size
val m = grid[0].size
val columns = IntArray(n)
val rows = IntArray(m)
var sum: Long = 0
for (i in 0 until n) {
for (j in 0 until m) {
columns[i] += grid[i][j]
rows[j] += grid[i][j]
}
}
for (i in 0 until n) {
for (j in 0 until m) {
sum += grid[i][j].toLong() * (rows[j] - 1) * (columns[i] - 1)
}
}
return sum
}
}
77 changes: 77 additions & 0 deletions src/main/kotlin/g3101_3200/s3128_right_triangles/readme.md
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`
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
package g3101_3200.s3129_find_all_possible_stable_binary_arrays_i

// #Medium #Dynamic_Programming #Prefix_Sum #2024_05_02_Time_169_ms_(92.86%)_Space_36.3_MB_(100.00%)

import kotlin.math.abs
import kotlin.math.max
import kotlin.math.min

class Solution {
private fun add(x: Int, y: Int): Int {
return (x + y) % MODULUS
}

private fun subtract(x: Int, y: Int): Int {
return (x + MODULUS - y) % MODULUS
}

private fun multiply(x: Int, y: Int): Int {
return (x.toLong() * y % MODULUS).toInt()
}

fun numberOfStableArrays(zero: Int, one: Int, limit: Int): Int {
if (limit == 1) {
return max((2 - abs((zero - one))), 0)
}
val max = max(zero, one)
val min = min(zero, one)
val lcn = Array(max + 1) { IntArray(max + 1) }
var row0 = lcn[0]
var row1: IntArray
var row2: IntArray
row0[0] = 1
var s = 1
var sLim = s - limit
while (s <= max) {
row2 = if (sLim > 0) lcn[sLim - 1] else intArrayOf()
row1 = row0
row0 = lcn[s]
var c = (s - 1) / limit + 1
while (c <= sLim) {
row0[c] = subtract(add(row1[c], row1[c - 1]), row2[c - 1])
c++
}
while (c <= s) {
row0[c] = add(row1[c], row1[c - 1])
c++
}
s++
sLim++
}
row1 = lcn[min]
var result = 0
var s0 = add(if (min < max) row0[min + 1] else 0, row0[min])
for (c in min downTo 1) {
val s1 = s0
s0 = add(row0[c], row0[c - 1])
result = add(result, multiply(row1[c], add(s0, s1)))
}
return result
}

companion object {
private const val MODULUS = 1e9.toInt() + 7
}
}
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`
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
package g3101_3200.s3130_find_all_possible_stable_binary_arrays_ii

// #Hard #Dynamic_Programming #Prefix_Sum #2024_05_02_Time_242_ms_(100.00%)_Space_36.7_MB_(100.00%)

import kotlin.math.max
import kotlin.math.min

class Solution {
private var factorial: LongArray? = null
private lateinit var reverse: LongArray

fun numberOfStableArrays(zero: Int, one: Int, limit: Int): Int {
if (factorial == null) {
factorial = LongArray(N + 1)
reverse = LongArray(N + 1)
factorial!![0] = 1
reverse[0] = 1
var x: Long = 1
for (i in 1..N) {
x = (x * i) % MOD
factorial!![i] = x.toInt().toLong()
reverse[i] = getInverse(x, MOD.toLong())
}
}
var ans: Long = 0
val s = LongArray(one + 1)
val n = (min(zero, one) + 1).toInt()
for (
groups0 in (zero + limit - 1) / limit..min(zero, n)
.toInt()
) {
val s0 = calc(groups0, zero, limit)
for (
groups1 in max(
groups0 - 1,
(one + limit - 1) / limit
)..min((groups0 + 1), one)
) {
var s1: Long
if (s[groups1] != 0L) {
s1 = s[groups1]
} else {
s[groups1] = calc(groups1, one, limit)
s1 = s[groups1]
}
ans = (ans + s0 * s1 * (if (groups1 == groups0) 2 else 1)) % MOD
}
}
return ((ans + MOD) % MOD).toInt()
}

fun calc(groups: Int, x: Int, limit: Int): Long {
var s: Long = 0
var sign = 1
var k = 0
while (k * limit <= x - groups && k <= groups) {
s = (s + sign * comb(groups, k) * comb(x - k * limit - 1, groups - 1)) % MOD
sign *= -1
k++
}
return s
}

fun comb(n: Int, k: Int): Long {
return (factorial!![n] * reverse[k] % MOD) * reverse[n - k] % MOD
}

fun getInverse(n: Long, mod: Long): Long {
var n = n
var p = mod
var x: Long = 1
var y: Long = 0
while (p > 0) {
val quotient = n / p
val remainder = n % p
val tempY = x - quotient * y
x = y
y = tempY
n = p
p = remainder
}
return ((x % mod) + mod) % mod
}

companion object {
private const val MOD = 1e9.toInt() + 7
private const val N = 1000
}
}
Loading