Skip to content

Commit ed25757

Browse files
committed
Added tasks 3392-3405
1 parent 11eddef commit ed25757

File tree

13 files changed

+1341
-104
lines changed
  • src/main/kotlin
    • g3301_3400
      • s3392_count_subarrays_of_length_three_with_a_condition
      • s3393_count_paths_with_the_given_xor_value
      • s3394_check_if_grid_can_be_cut_into_sections
      • s3395_subsequences_with_a_unique_middle_mode_i
      • s3396_minimum_number_of_operations_to_make_elements_in_array_distinct
      • s3397_maximum_number_of_distinct_elements_after_operations
      • s3398_smallest_substring_with_identical_characters_i
      • s3399_smallest_substring_with_identical_characters_ii
    • g3401_3500
      • s3402_minimum_operations_to_make_columns_strictly_increasing
      • s3403_find_the_lexicographically_largest_string_from_the_box_i
      • s3404_count_special_subsequences
      • s3405_count_the_number_of_arrays_with_k_matching_adjacent_elements

13 files changed

+1341
-104
lines changed

README.md

Lines changed: 116 additions & 104 deletions
Large diffs are not rendered by default.
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
[![](https://img.shields.io/github/stars/javadev/LeetCode-in-Kotlin?label=Stars&style=flat-square)](https://github.com/javadev/LeetCode-in-Kotlin)
2+
[![](https://img.shields.io/github/forks/javadev/LeetCode-in-Kotlin?label=Fork%20me%20on%20GitHub%20&style=flat-square)](https://github.com/javadev/LeetCode-in-Kotlin/fork)
3+
4+
## 3392\. Count Subarrays of Length Three With a Condition
5+
6+
Easy
7+
8+
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.
9+
10+
A **subarray** is a contiguous **non-empty** sequence of elements within an array.
11+
12+
**Example 1:**
13+
14+
**Input:** nums = [1,2,1,4,1]
15+
16+
**Output:** 1
17+
18+
**Explanation:**
19+
20+
Only the subarray `[1,4,1]` contains exactly 3 elements where the sum of the first and third numbers equals half the middle number.
21+
22+
**Example 2:**
23+
24+
**Input:** nums = [1,1,1]
25+
26+
**Output:** 0
27+
28+
**Explanation:**
29+
30+
`[1,1,1]` is the only subarray of length 3. However, its first and third numbers do not add to half the middle number.
31+
32+
**Constraints:**
33+
34+
* `3 <= nums.length <= 100`
35+
* `-100 <= nums[i] <= 100`
36+
37+
## Solution
38+
39+
```kotlin
40+
class Solution {
41+
fun countSubarrays(nums: IntArray): Int {
42+
val window = 3
43+
var cnt = 0
44+
for (i in 0..nums.size - window) {
45+
val first = nums[i].toFloat()
46+
val second = nums[i + 1].toFloat()
47+
val third = nums[i + 2].toFloat()
48+
if (second / 2 == first + third) {
49+
cnt++
50+
}
51+
}
52+
return cnt
53+
}
54+
}
55+
```
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
[![](https://img.shields.io/github/stars/javadev/LeetCode-in-Kotlin?label=Stars&style=flat-square)](https://github.com/javadev/LeetCode-in-Kotlin)
2+
[![](https://img.shields.io/github/forks/javadev/LeetCode-in-Kotlin?label=Fork%20me%20on%20GitHub%20&style=flat-square)](https://github.com/javadev/LeetCode-in-Kotlin/fork)
3+
4+
## 3393\. Count Paths With the Given XOR Value
5+
6+
Medium
7+
8+
You are given a 2D integer array `grid` with size `m x n`. You are also given an integer `k`.
9+
10+
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**:
11+
12+
* 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_.
13+
* The `XOR` of all the numbers on the path must be **equal** to `k`.
14+
15+
Return the total number of such paths.
16+
17+
Since the answer can be very large, return the result **modulo** <code>10<sup>9</sup> + 7</code>.
18+
19+
**Example 1:**
20+
21+
**Input:** grid = \[\[2, 1, 5], [7, 10, 0], [12, 6, 4]], k = 11
22+
23+
**Output:** 3
24+
25+
**Explanation:**
26+
27+
The 3 paths are:
28+
29+
* `(0, 0) → (1, 0) → (2, 0) → (2, 1) → (2, 2)`
30+
* `(0, 0) → (1, 0) → (1, 1) → (1, 2) → (2, 2)`
31+
* `(0, 0) → (0, 1) → (1, 1) → (2, 1) → (2, 2)`
32+
33+
**Example 2:**
34+
35+
**Input:** grid = \[\[1, 3, 3, 3], [0, 3, 3, 2], [3, 0, 1, 1]], k = 2
36+
37+
**Output:** 5
38+
39+
**Explanation:**
40+
41+
The 5 paths are:
42+
43+
* `(0, 0) → (1, 0) → (2, 0) → (2, 1) → (2, 2) → (2, 3)`
44+
* `(0, 0) → (1, 0) → (1, 1) → (2, 1) → (2, 2) → (2, 3)`
45+
* `(0, 0) → (1, 0) → (1, 1) → (1, 2) → (1, 3) → (2, 3)`
46+
* `(0, 0) → (0, 1) → (1, 1) → (1, 2) → (2, 2) → (2, 3)`
47+
* `(0, 0) → (0, 1) → (0, 2) → (1, 2) → (2, 2) → (2, 3)`
48+
49+
**Example 3:**
50+
51+
**Input:** grid = \[\[1, 1, 1, 2], [3, 0, 3, 2], [3, 0, 2, 2]], k = 10
52+
53+
**Output:** 0
54+
55+
**Constraints:**
56+
57+
* `1 <= m == grid.length <= 300`
58+
* `1 <= n == grid[r].length <= 300`
59+
* `0 <= grid[r][c] < 16`
60+
* `0 <= k < 16`
61+
62+
## Solution
63+
64+
```kotlin
65+
class Solution {
66+
private var m = -1
67+
private var n = -1
68+
private lateinit var dp: Array<Array<IntArray>>
69+
70+
fun countPathsWithXorValue(grid: Array<IntArray>, k: Int): Int {
71+
m = grid.size
72+
n = grid[0].size
73+
dp = Array(m) { Array(n) { IntArray(16) { -1 } } }
74+
return dfs(grid, 0, k, 0, 0)
75+
}
76+
77+
private fun dfs(grid: Array<IntArray>, xorVal: Int, k: Int, i: Int, j: Int): Int {
78+
var xorVal = xorVal
79+
if (i < 0 || j < 0 || j >= n || i >= m) {
80+
return 0
81+
}
82+
xorVal = xorVal xor grid[i][j]
83+
if (dp[i][j][xorVal] != -1) {
84+
return dp[i][j][xorVal]
85+
}
86+
if (i == m - 1 && j == n - 1 && xorVal == k) {
87+
return 1
88+
}
89+
val down = dfs(grid, xorVal, k, i + 1, j)
90+
val right = dfs(grid, xorVal, k, i, j + 1)
91+
dp[i][j][xorVal] = (down + right) % MOD
92+
return dp[i][j][xorVal]
93+
}
94+
95+
companion object {
96+
private val MOD = (1e9 + 7).toInt()
97+
}
98+
}
99+
```
Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
[![](https://img.shields.io/github/stars/javadev/LeetCode-in-Kotlin?label=Stars&style=flat-square)](https://github.com/javadev/LeetCode-in-Kotlin)
2+
[![](https://img.shields.io/github/forks/javadev/LeetCode-in-Kotlin?label=Fork%20me%20on%20GitHub%20&style=flat-square)](https://github.com/javadev/LeetCode-in-Kotlin/fork)
3+
4+
## 3394\. Check if Grid can be Cut into Sections
5+
6+
Medium
7+
8+
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:
9+
10+
* <code>(start<sub>x</sub>, start<sub>y</sub>)</code>: The bottom-left corner of the rectangle.
11+
* <code>(end<sub>x</sub>, end<sub>y</sub>)</code>: The top-right corner of the rectangle.
12+
13+
**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:
14+
15+
* Each of the three resulting sections formed by the cuts contains **at least** one rectangle.
16+
* Every rectangle belongs to **exactly** one section.
17+
18+
Return `true` if such cuts can be made; otherwise, return `false`.
19+
20+
**Example 1:**
21+
22+
**Input:** n = 5, rectangles = \[\[1,0,5,2],[0,2,2,4],[3,2,5,3],[0,4,4,5]]
23+
24+
**Output:** true
25+
26+
**Explanation:**
27+
28+
![](https://assets.leetcode.com/uploads/2024/10/23/tt1drawio.png)
29+
30+
The grid is shown in the diagram. We can make horizontal cuts at `y = 2` and `y = 4`. Hence, output is true.
31+
32+
**Example 2:**
33+
34+
**Input:** n = 4, rectangles = \[\[0,0,1,1],[2,0,3,4],[0,2,2,3],[3,0,4,3]]
35+
36+
**Output:** true
37+
38+
**Explanation:**
39+
40+
![](https://assets.leetcode.com/uploads/2024/10/23/tc2drawio.png)
41+
42+
We can make vertical cuts at `x = 2` and `x = 3`. Hence, output is true.
43+
44+
**Example 3:**
45+
46+
**Input:** n = 4, rectangles = \[\[0,2,2,4],[1,0,3,2],[2,2,3,4],[3,0,4,2],[3,2,4,4]]
47+
48+
**Output:** false
49+
50+
**Explanation:**
51+
52+
We cannot make two horizontal or two vertical cuts that satisfy the conditions. Hence, output is false.
53+
54+
**Constraints:**
55+
56+
* <code>3 <= n <= 10<sup>9</sup></code>
57+
* <code>3 <= rectangles.length <= 10<sup>5</sup></code>
58+
* `0 <= rectangles[i][0] < rectangles[i][2] <= n`
59+
* `0 <= rectangles[i][1] < rectangles[i][3] <= n`
60+
* No two rectangles overlap.
61+
62+
## Solution
63+
64+
```kotlin
65+
import kotlin.math.max
66+
67+
@Suppress("unused")
68+
class Solution {
69+
fun checkValidCuts(n: Int, rectangles: Array<IntArray>): Boolean {
70+
val m = rectangles.size
71+
val xAxis = Array<IntArray>(m) { IntArray(2) }
72+
val yAxis = Array<IntArray>(m) { IntArray(2) }
73+
var ind = 0
74+
for (axis in rectangles) {
75+
val startX = axis[0]
76+
val startY = axis[1]
77+
val endX = axis[2]
78+
val endY = axis[3]
79+
xAxis[ind] = intArrayOf(startX, endX)
80+
yAxis[ind] = intArrayOf(startY, endY)
81+
ind++
82+
}
83+
84+
xAxis.sortWith<IntArray>(
85+
Comparator { a: IntArray, b: IntArray -> if (a[0] == b[0]) a[1] - b[1] else a[0] - b[0] },
86+
)
87+
88+
yAxis.sortWith<IntArray>(
89+
Comparator { a: IntArray, b: IntArray -> if (a[0] == b[0]) a[1] - b[1] else a[0] - b[0] },
90+
)
91+
val verticalCuts = findSections(xAxis)
92+
if (verticalCuts > 2) {
93+
return true
94+
}
95+
val horizontalCuts = findSections(yAxis)
96+
return horizontalCuts > 2
97+
}
98+
99+
private fun findSections(axis: Array<IntArray>): Int {
100+
var end = axis[0][1]
101+
var sections = 1
102+
for (i in 1..<axis.size) {
103+
if (end > axis[i][0]) {
104+
end = max(end, axis[i][1])
105+
} else {
106+
sections++
107+
end = axis[i][1]
108+
}
109+
if (sections > 2) {
110+
return sections
111+
}
112+
}
113+
return sections
114+
}
115+
}
116+
```

0 commit comments

Comments
 (0)