Skip to content

Commit 6663cc4

Browse files
authored
Added tasks 3070-3081
1 parent 3b1aacf commit 6663cc4

File tree

30 files changed

+1211
-0
lines changed

30 files changed

+1211
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package g3001_3100.s3070_count_submatrices_with_top_left_element_and_sum_less_than_k
2+
3+
// #Medium #Array #Matrix #Prefix_Sum #2024_04_16_Time_773_ms_(85.71%)_Space_134.2_MB_(71.43%)
4+
5+
class Solution {
6+
fun countSubmatrices(grid: Array<IntArray>, k: Int): Int {
7+
val n = grid[0].size
8+
val sums = IntArray(n)
9+
var ans = 0
10+
for (ints in grid) {
11+
var sum = 0
12+
for (col in 0 until n) {
13+
sum += ints[col]
14+
sums[col] += sum
15+
if (sums[col] <= k) {
16+
ans++
17+
} else {
18+
break
19+
}
20+
}
21+
}
22+
return ans
23+
}
24+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
3070\. Count Submatrices with Top-Left Element and Sum Less Than k
2+
3+
Medium
4+
5+
You are given a **0-indexed** integer matrix `grid` and an integer `k`.
6+
7+
Return _the **number** of submatrices that contain the top-left element of the_ `grid`, _and have a sum less than or equal to_ `k`.
8+
9+
**Example 1:**
10+
11+
![](https://assets.leetcode.com/uploads/2024/01/01/example1.png)
12+
13+
**Input:** grid = [[7,6,3],[6,6,1]], k = 18
14+
15+
**Output:** 4
16+
17+
**Explanation:** There are only 4 submatrices, shown in the image above, that contain the top-left element of grid, and have a sum less than or equal to 18.
18+
19+
**Example 2:**
20+
21+
![](https://assets.leetcode.com/uploads/2024/01/01/example21.png)
22+
23+
**Input:** grid = [[7,2,9],[1,5,0],[2,6,6]], k = 20
24+
25+
**Output:** 6
26+
27+
**Explanation:** There are only 6 submatrices, shown in the image above, that contain the top-left element of grid, and have a sum less than or equal to 20.
28+
29+
**Constraints:**
30+
31+
* `m == grid.length`
32+
* `n == grid[i].length`
33+
* `1 <= n, m <= 1000`
34+
* `0 <= grid[i][j] <= 1000`
35+
* <code>1 <= k <= 10<sup>9</sup></code>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
package g3001_3100.s3071_minimum_operations_to_write_the_letter_y_on_a_grid
2+
3+
// #Medium #Array #Hash_Table #Matrix #Counting
4+
// #2024_04_16_Time_268_ms_(91.11%)_Space_42.6_MB_(93.33%)
5+
6+
import kotlin.math.min
7+
8+
class Solution {
9+
fun minimumOperationsToWriteY(arr: Array<IntArray>): Int {
10+
val n = arr.size
11+
val cnt1 = IntArray(3)
12+
val cnt2 = IntArray(3)
13+
val x = n / 2
14+
val y = n / 2
15+
for (j in x until n) {
16+
cnt1[arr[j][y]]++
17+
arr[j][y] = 3
18+
}
19+
for (j in x downTo 0) {
20+
if (arr[j][j] != 3) {
21+
cnt1[arr[j][j]]++
22+
}
23+
arr[j][j] = 3
24+
}
25+
for (j in x downTo 0) {
26+
if (arr[j][n - 1 - j] != 3) {
27+
cnt1[arr[j][n - 1 - j]]++
28+
}
29+
arr[j][n - 1 - j] = 3
30+
}
31+
for (ints in arr) {
32+
for (j in 0 until n) {
33+
if (ints[j] != 3) {
34+
cnt2[ints[j]]++
35+
}
36+
}
37+
}
38+
val s1 = cnt1[0] + cnt1[1] + cnt1[2]
39+
val s2 = cnt2[0] + cnt2[1] + cnt2[2]
40+
var min = Int.MAX_VALUE
41+
for (i in 0..2) {
42+
for (j in 0..2) {
43+
if (i != j) {
44+
min = min((s1 - cnt1[i] + s2 - cnt2[j]), min)
45+
}
46+
}
47+
}
48+
return min
49+
}
50+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
3071\. Minimum Operations to Write the Letter Y on a Grid
2+
3+
Medium
4+
5+
You are given a **0-indexed** `n x n` grid where `n` is odd, and `grid[r][c]` is `0`, `1`, or `2`.
6+
7+
We say that a cell belongs to the Letter **Y** if it belongs to one of the following:
8+
9+
* The diagonal starting at the top-left cell and ending at the center cell of the grid.
10+
* The diagonal starting at the top-right cell and ending at the center cell of the grid.
11+
* The vertical line starting at the center cell and ending at the bottom border of the grid.
12+
13+
The Letter **Y** is written on the grid if and only if:
14+
15+
* All values at cells belonging to the Y are equal.
16+
* All values at cells not belonging to the Y are equal.
17+
* The values at cells belonging to the Y are different from the values at cells not belonging to the Y.
18+
19+
Return _the **minimum** number of operations needed to write the letter Y on the grid given that in one operation you can change the value at any cell to_ `0`_,_ `1`_,_ _or_ `2`_._
20+
21+
**Example 1:**
22+
23+
![](https://assets.leetcode.com/uploads/2024/01/22/y2.png)
24+
25+
**Input:** grid = [[1,2,2],[1,1,0],[0,1,0]]
26+
27+
**Output:** 3
28+
29+
**Explanation:** We can write Y on the grid by applying the changes highlighted in blue in the image above. After the operations, all cells that belong to Y, denoted in bold, have the same value of 1 while those that do not belong to Y are equal to 0. It can be shown that 3 is the minimum number of operations needed to write Y on the grid.
30+
31+
**Example 2:**
32+
33+
![](https://assets.leetcode.com/uploads/2024/01/22/y3.png)
34+
35+
**Input:** grid = [[0,1,0,1,0],[2,1,0,1,2],[2,2,2,0,1],[2,2,2,2,2],[2,1,2,2,2]]
36+
37+
**Output:** 12
38+
39+
**Explanation:** We can write Y on the grid by applying the changes highlighted in blue in the image above. After the operations, all cells that belong to Y, denoted in bold, have the same value of 0 while those that do not belong to Y are equal to 2. It can be shown that 12 is the minimum number of operations needed to write Y on the grid.
40+
41+
**Constraints:**
42+
43+
* `3 <= n <= 49`
44+
* `n == grid.length == grid[i].length`
45+
* `0 <= grid[i][j] <= 2`
46+
* `n` is odd.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
package g3001_3100.s3072_distribute_elements_into_two_arrays_ii
2+
3+
// #Hard #Array #Simulation #Segment_Tree #Binary_Indexed_Tree
4+
// #2024_04_16_Time_890_ms_(100.00%)_Space_77.6_MB_(54.17%)
5+
6+
@Suppress("NAME_SHADOWING", "kotlin:S1871")
7+
class Solution {
8+
internal inner class BIT(size: Int) {
9+
private val tree = IntArray(size + 1)
10+
11+
fun update(ind: Int) {
12+
var ind = ind
13+
while (ind < tree.size) {
14+
tree[ind]++
15+
ind += lsb(ind)
16+
}
17+
}
18+
19+
fun rsq(ind: Int): Int {
20+
var ind = ind
21+
var sum = 0
22+
while (ind > 0) {
23+
sum += tree[ind]
24+
ind -= lsb(ind)
25+
}
26+
return sum
27+
}
28+
29+
private fun lsb(n: Int): Int {
30+
return n and (-n)
31+
}
32+
}
33+
34+
fun resultArray(source: IntArray): IntArray {
35+
val nums = shrink(source)
36+
val arr1 = IntArray(nums.size)
37+
val arr2 = IntArray(nums.size)
38+
arr1[0] = source[0]
39+
arr2[0] = source[1]
40+
var p1 = 0
41+
var p2 = 0
42+
val bit1 = BIT(nums.size)
43+
bit1.update(nums[0])
44+
val bit2 = BIT(nums.size)
45+
bit2.update(nums[1])
46+
for (i in 2 until nums.size) {
47+
val g1 = p1 + 1 - bit1.rsq(nums[i])
48+
val g2 = p2 + 1 - bit2.rsq(nums[i])
49+
if (g1 > g2) {
50+
p1++
51+
arr1[p1] = source[i]
52+
bit1.update(nums[i])
53+
} else if (g1 < g2) {
54+
p2++
55+
arr2[p2] = source[i]
56+
bit2.update(nums[i])
57+
} else if (p1 < p2) {
58+
p1++
59+
arr1[p1] = source[i]
60+
bit1.update(nums[i])
61+
} else if (p1 > p2) {
62+
p2++
63+
arr2[p2] = source[i]
64+
bit2.update(nums[i])
65+
} else {
66+
p1++
67+
arr1[p1] = source[i]
68+
bit1.update(nums[i])
69+
}
70+
}
71+
for (i in p1 + 1 until arr1.size) {
72+
arr1[i] = arr2[i - p1 - 1]
73+
}
74+
return arr1
75+
}
76+
77+
private fun shrink(nums: IntArray): IntArray {
78+
val b = LongArray(nums.size)
79+
for (i in nums.indices) {
80+
b[i] = nums[i].toLong() shl 32 or i.toLong()
81+
}
82+
b.sort()
83+
val result = IntArray(nums.size)
84+
var p = 1
85+
for (i in nums.indices) {
86+
if (i > 0 && (b[i] xor b[i - 1]) shr 32 != 0L) {
87+
p++
88+
}
89+
result[b[i].toInt()] = p
90+
}
91+
return result
92+
}
93+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
3072\. Distribute Elements Into Two Arrays II
2+
3+
Hard
4+
5+
You are given a **1-indexed** array of integers `nums` of length `n`.
6+
7+
We define a function `greaterCount` such that `greaterCount(arr, val)` returns the number of elements in `arr` that are **strictly greater** than `val`.
8+
9+
You need to distribute all the elements of `nums` between two arrays `arr1` and `arr2` using `n` operations. In the first operation, append `nums[1]` to `arr1`. In the second operation, append `nums[2]` to `arr2`. Afterwards, in the <code>i<sup>th</sup></code> operation:
10+
11+
* If `greaterCount(arr1, nums[i]) > greaterCount(arr2, nums[i])`, append `nums[i]` to `arr1`.
12+
* If `greaterCount(arr1, nums[i]) < greaterCount(arr2, nums[i])`, append `nums[i]` to `arr2`.
13+
* If `greaterCount(arr1, nums[i]) == greaterCount(arr2, nums[i])`, append `nums[i]` to the array with a **lesser** number of elements.
14+
* If there is still a tie, append `nums[i]` to `arr1`.
15+
16+
The array `result` is formed by concatenating the arrays `arr1` and `arr2`. For example, if `arr1 == [1,2,3]` and `arr2 == [4,5,6]`, then `result = [1,2,3,4,5,6]`.
17+
18+
Return _the integer array_ `result`.
19+
20+
**Example 1:**
21+
22+
**Input:** nums = [2,1,3,3]
23+
24+
**Output:** [2,3,1,3]
25+
26+
**Explanation:** After the first 2 operations, arr1 = [2] and arr2 = [1].
27+
28+
In the 3<sup>rd</sup> operation, the number of elements greater than 3 is zero in both arrays.
29+
30+
Also, the lengths are equal, hence, append nums[3] to arr1. In the 4<sup>th</sup> operation, the number of elements greater than 3 is zero in both arrays. As the length of arr2 is lesser, hence, append nums[4] to arr2.
31+
32+
After 4 operations, arr1 = [2,3] and arr2 = [1,3]. Hence, the array result formed by concatenation is [2,3,1,3].
33+
34+
**Example 2:**
35+
36+
**Input:** nums = [5,14,3,1,2]
37+
38+
**Output:** [5,3,1,2,14]
39+
40+
**Explanation:** After the first 2 operations, arr1 = [5] and arr2 = [14].
41+
42+
In the 3<sup>rd</sup> operation, the number of elements greater than 3 is one in both arrays. Also, the lengths are equal, hence, append nums[3] to arr1.
43+
44+
In the 4<sup>th</sup> operation, the number of elements greater than 1 is greater in arr1 than arr2 (2 > 1). Hence, append nums[4] to arr1. In the 5<sup>th</sup> operation, the number of elements greater than 2 is greater in arr1 than arr2 (2 > 1). Hence, append nums[5] to arr1.
45+
46+
zAfter 5 operations, arr1 = [5,3,1,2] and arr2 = [14]. Hence, the array result formed by concatenation is [5,3,1,2,14].
47+
48+
**Example 3:**
49+
50+
**Input:** nums = [3,3,3,3]
51+
52+
**Output:** [3,3,3,3]
53+
54+
**Explanation:** At the end of 4 operations, arr1 = [3,3] and arr2 = [3,3]. Hence, the array result formed by concatenation is [3,3,3,3].
55+
56+
**Constraints:**
57+
58+
* <code>3 <= n <= 10<sup>5</sup></code>
59+
* <code>1 <= nums[i] <= 10<sup>9</sup></code>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package g3001_3100.s3074_apple_redistribution_into_boxes
2+
3+
// #Easy #Array #Sorting #Greedy #2024_04_16_Time_168_ms_(97.37%)_Space_35.4_MB_(100.00%)
4+
5+
import kotlin.math.max
6+
7+
class Solution {
8+
fun minimumBoxes(apple: IntArray, capacity: IntArray): Int {
9+
val count = IntArray(51)
10+
var appleSum = 0
11+
for (j in apple) {
12+
appleSum += j
13+
}
14+
var reqCapacity = 0
15+
var max = 0
16+
for (j in capacity) {
17+
count[j]++
18+
max = max(max.toDouble(), j.toDouble()).toInt()
19+
}
20+
for (i in max downTo 0) {
21+
if (count[i] >= 1) {
22+
while (count[i] != 0) {
23+
appleSum -= i
24+
reqCapacity++
25+
if (appleSum <= 0) {
26+
return reqCapacity
27+
}
28+
count[i]--
29+
}
30+
}
31+
}
32+
return reqCapacity
33+
}
34+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
3074\. Apple Redistribution into Boxes
2+
3+
Easy
4+
5+
You are given an array `apple` of size `n` and an array `capacity` of size `m`.
6+
7+
There are `n` packs where the <code>i<sup>th</sup></code> pack contains `apple[i]` apples. There are `m` boxes as well, and the <code>i<sup>th</sup></code> box has a capacity of `capacity[i]` apples.
8+
9+
Return _the **minimum** number of boxes you need to select to redistribute these_ `n` _packs of apples into boxes_.
10+
11+
**Note** that, apples from the same pack can be distributed into different boxes.
12+
13+
**Example 1:**
14+
15+
**Input:** apple = [1,3,2], capacity = [4,3,1,5,2]
16+
17+
**Output:** 2
18+
19+
**Explanation:** We will use boxes with capacities 4 and 5. It is possible to distribute the apples as the total capacity is greater than or equal to the total number of apples.
20+
21+
**Example 2:**
22+
23+
**Input:** apple = [5,5,5], capacity = [2,4,2,7]
24+
25+
**Output:** 4
26+
27+
**Explanation:** We will need to use all the boxes.
28+
29+
**Constraints:**
30+
31+
* `1 <= n == apple.length <= 50`
32+
* `1 <= m == capacity.length <= 50`
33+
* `1 <= apple[i], capacity[i] <= 50`
34+
* The input is generated such that it's possible to redistribute packs of apples into boxes.

0 commit comments

Comments
 (0)