Skip to content

Commit 6a18290

Browse files
authored
Added tasks 3392-3405
1 parent 82507ca commit 6a18290

File tree

36 files changed

+1541
-0
lines changed

36 files changed

+1541
-0
lines changed
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package g3301_3400.s3392_count_subarrays_of_length_three_with_a_condition
2+
3+
// #Easy #2024_12_22_Time_3_ms_(100.00%)_Space_45_MB_(100.00%)
4+
5+
class Solution {
6+
fun countSubarrays(nums: IntArray): Int {
7+
val window = 3
8+
var cnt = 0
9+
for (i in 0..nums.size - window) {
10+
val first = nums[i].toFloat()
11+
val second = nums[i + 1].toFloat()
12+
val third = nums[i + 2].toFloat()
13+
if (second / 2 == first + third) {
14+
cnt++
15+
}
16+
}
17+
return cnt
18+
}
19+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
3392\. Count Subarrays of Length Three With a Condition
2+
3+
Easy
4+
5+
Given an integer array `nums`, return the number of subarrays of length 3 such that the sum of the first and third numbers equals _exactly_ half of the second number.
6+
7+
A **subarray** is a contiguous **non-empty** sequence of elements within an array.
8+
9+
**Example 1:**
10+
11+
**Input:** nums = [1,2,1,4,1]
12+
13+
**Output:** 1
14+
15+
**Explanation:**
16+
17+
Only the subarray `[1,4,1]` contains exactly 3 elements where the sum of the first and third numbers equals half the middle number.
18+
19+
**Example 2:**
20+
21+
**Input:** nums = [1,1,1]
22+
23+
**Output:** 0
24+
25+
**Explanation:**
26+
27+
`[1,1,1]` is the only subarray of length 3. However, its first and third numbers do not add to half the middle number.
28+
29+
**Constraints:**
30+
31+
* `3 <= nums.length <= 100`
32+
* `-100 <= nums[i] <= 100`
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package g3301_3400.s3393_count_paths_with_the_given_xor_value
2+
3+
// #Medium #2024_12_30_Time_57_(68.42%)_Space_73.12_(52.63%)
4+
5+
class Solution {
6+
private var m = -1
7+
private var n = -1
8+
private lateinit var dp: Array<Array<IntArray>>
9+
10+
fun countPathsWithXorValue(grid: Array<IntArray>, k: Int): Int {
11+
m = grid.size
12+
n = grid[0].size
13+
dp = Array(m) { Array(n) { IntArray(16) { -1 } } }
14+
return dfs(grid, 0, k, 0, 0)
15+
}
16+
17+
private fun dfs(grid: Array<IntArray>, xorVal: Int, k: Int, i: Int, j: Int): Int {
18+
var xorVal = xorVal
19+
if (i < 0 || j < 0 || j >= n || i >= m) {
20+
return 0
21+
}
22+
xorVal = xorVal xor grid[i][j]
23+
if (dp[i][j][xorVal] != -1) {
24+
return dp[i][j][xorVal]
25+
}
26+
if (i == m - 1 && j == n - 1 && xorVal == k) {
27+
return 1
28+
}
29+
val down = dfs(grid, xorVal, k, i + 1, j)
30+
val right = dfs(grid, xorVal, k, i, j + 1)
31+
dp[i][j][xorVal] = (down + right) % MOD
32+
return dp[i][j][xorVal]
33+
}
34+
35+
companion object {
36+
private val MOD = (1e9 + 7).toInt()
37+
}
38+
}
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
3393\. Count Paths With the Given XOR Value
2+
3+
Medium
4+
5+
You are given a 2D integer array `grid` with size `m x n`. You are also given an integer `k`.
6+
7+
Your task is to calculate the number of paths you can take from the top-left cell `(0, 0)` to the bottom-right cell `(m - 1, n - 1)` satisfying the following **constraints**:
8+
9+
* You can either move to the right or down. Formally, from the cell `(i, j)` you may move to the cell `(i, j + 1)` or to the cell `(i + 1, j)` if the target cell _exists_.
10+
* The `XOR` of all the numbers on the path must be **equal** to `k`.
11+
12+
Return the total number of such paths.
13+
14+
Since the answer can be very large, return the result **modulo** <code>10<sup>9</sup> + 7</code>.
15+
16+
**Example 1:**
17+
18+
**Input:** grid = [[2, 1, 5], [7, 10, 0], [12, 6, 4]], k = 11
19+
20+
**Output:** 3
21+
22+
**Explanation:**
23+
24+
The 3 paths are:
25+
26+
* `(0, 0) → (1, 0) → (2, 0) → (2, 1) → (2, 2)`
27+
* `(0, 0) → (1, 0) → (1, 1) → (1, 2) → (2, 2)`
28+
* `(0, 0) → (0, 1) → (1, 1) → (2, 1) → (2, 2)`
29+
30+
**Example 2:**
31+
32+
**Input:** grid = [[1, 3, 3, 3], [0, 3, 3, 2], [3, 0, 1, 1]], k = 2
33+
34+
**Output:** 5
35+
36+
**Explanation:**
37+
38+
The 5 paths are:
39+
40+
* `(0, 0) → (1, 0) → (2, 0) → (2, 1) → (2, 2) → (2, 3)`
41+
* `(0, 0) → (1, 0) → (1, 1) → (2, 1) → (2, 2) → (2, 3)`
42+
* `(0, 0) → (1, 0) → (1, 1) → (1, 2) → (1, 3) → (2, 3)`
43+
* `(0, 0) → (0, 1) → (1, 1) → (1, 2) → (2, 2) → (2, 3)`
44+
* `(0, 0) → (0, 1) → (0, 2) → (1, 2) → (2, 2) → (2, 3)`
45+
46+
**Example 3:**
47+
48+
**Input:** grid = [[1, 1, 1, 2], [3, 0, 3, 2], [3, 0, 2, 2]], k = 10
49+
50+
**Output:** 0
51+
52+
**Constraints:**
53+
54+
* `1 <= m == grid.length <= 300`
55+
* `1 <= n == grid[r].length <= 300`
56+
* `0 <= grid[r][c] < 16`
57+
* `0 <= k < 16`
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
package g3301_3400.s3394_check_if_grid_can_be_cut_into_sections
2+
3+
// #Medium #2024_12_22_Time_298_ms_(100.00%)_Space_132.4_MB_(100.00%)
4+
5+
import kotlin.math.max
6+
7+
@Suppress("unused")
8+
class Solution {
9+
fun checkValidCuts(n: Int, rectangles: Array<IntArray>): Boolean {
10+
val m = rectangles.size
11+
val xAxis = Array<IntArray>(m) { IntArray(2) }
12+
val yAxis = Array<IntArray>(m) { IntArray(2) }
13+
var ind = 0
14+
for (axis in rectangles) {
15+
val startX = axis[0]
16+
val startY = axis[1]
17+
val endX = axis[2]
18+
val endY = axis[3]
19+
xAxis[ind] = intArrayOf(startX, endX)
20+
yAxis[ind] = intArrayOf(startY, endY)
21+
ind++
22+
}
23+
24+
xAxis.sortWith<IntArray>(
25+
Comparator { a: IntArray, b: IntArray -> if (a[0] == b[0]) a[1] - b[1] else a[0] - b[0] },
26+
)
27+
28+
yAxis.sortWith<IntArray>(
29+
Comparator { a: IntArray, b: IntArray -> if (a[0] == b[0]) a[1] - b[1] else a[0] - b[0] },
30+
)
31+
val verticalCuts = findSections(xAxis)
32+
if (verticalCuts > 2) {
33+
return true
34+
}
35+
val horizontalCuts = findSections(yAxis)
36+
return horizontalCuts > 2
37+
}
38+
39+
private fun findSections(axis: Array<IntArray>): Int {
40+
var end = axis[0][1]
41+
var sections = 1
42+
for (i in 1..<axis.size) {
43+
if (end > axis[i][0]) {
44+
end = max(end, axis[i][1])
45+
} else {
46+
sections++
47+
end = axis[i][1]
48+
}
49+
if (sections > 2) {
50+
return sections
51+
}
52+
}
53+
return sections
54+
}
55+
}
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
3394\. Check if Grid can be Cut into Sections
2+
3+
Medium
4+
5+
You are given an integer `n` representing the dimensions of an `n x n` grid, with the origin at the bottom-left corner of the grid. You are also given a 2D array of coordinates `rectangles`, where `rectangles[i]` is in the form <code>[start<sub>x</sub>, start<sub>y</sub>, end<sub>x</sub>, end<sub>y</sub>]</code>, representing a rectangle on the grid. Each rectangle is defined as follows:
6+
7+
* <code>(start<sub>x</sub>, start<sub>y</sub>)</code>: The bottom-left corner of the rectangle.
8+
* <code>(end<sub>x</sub>, end<sub>y</sub>)</code>: The top-right corner of the rectangle.
9+
10+
**Note** that the rectangles do not overlap. Your task is to determine if it is possible to make **either two horizontal or two vertical cuts** on the grid such that:
11+
12+
* Each of the three resulting sections formed by the cuts contains **at least** one rectangle.
13+
* Every rectangle belongs to **exactly** one section.
14+
15+
Return `true` if such cuts can be made; otherwise, return `false`.
16+
17+
**Example 1:**
18+
19+
**Input:** n = 5, rectangles = [[1,0,5,2],[0,2,2,4],[3,2,5,3],[0,4,4,5]]
20+
21+
**Output:** true
22+
23+
**Explanation:**
24+
25+
![](https://assets.leetcode.com/uploads/2024/10/23/tt1drawio.png)
26+
27+
The grid is shown in the diagram. We can make horizontal cuts at `y = 2` and `y = 4`. Hence, output is true.
28+
29+
**Example 2:**
30+
31+
**Input:** n = 4, rectangles = [[0,0,1,1],[2,0,3,4],[0,2,2,3],[3,0,4,3]]
32+
33+
**Output:** true
34+
35+
**Explanation:**
36+
37+
![](https://assets.leetcode.com/uploads/2024/10/23/tc2drawio.png)
38+
39+
We can make vertical cuts at `x = 2` and `x = 3`. Hence, output is true.
40+
41+
**Example 3:**
42+
43+
**Input:** n = 4, rectangles = [[0,2,2,4],[1,0,3,2],[2,2,3,4],[3,0,4,2],[3,2,4,4]]
44+
45+
**Output:** false
46+
47+
**Explanation:**
48+
49+
We cannot make two horizontal or two vertical cuts that satisfy the conditions. Hence, output is false.
50+
51+
**Constraints:**
52+
53+
* <code>3 <= n <= 10<sup>9</sup></code>
54+
* <code>3 <= rectangles.length <= 10<sup>5</sup></code>
55+
* `0 <= rectangles[i][0] < rectangles[i][2] <= n`
56+
* `0 <= rectangles[i][1] < rectangles[i][3] <= n`
57+
* No two rectangles overlap.

0 commit comments

Comments
 (0)