Skip to content

Added tasks 3392-3405 #737

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 10 commits into from
Jan 4, 2025
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,19 @@
package g3301_3400.s3392_count_subarrays_of_length_three_with_a_condition

// #Easy #2024_12_22_Time_3_ms_(100.00%)_Space_45_MB_(100.00%)

class Solution {
fun countSubarrays(nums: IntArray): Int {
val window = 3
var cnt = 0
for (i in 0..nums.size - window) {
val first = nums[i].toFloat()
val second = nums[i + 1].toFloat()
val third = nums[i + 2].toFloat()
if (second / 2 == first + third) {
cnt++
}
}
return cnt
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
3392\. Count Subarrays of Length Three With a Condition

Easy

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.

A **subarray** is a contiguous **non-empty** sequence of elements within an array.

**Example 1:**

**Input:** nums = [1,2,1,4,1]

**Output:** 1

**Explanation:**

Only the subarray `[1,4,1]` contains exactly 3 elements where the sum of the first and third numbers equals half the middle number.

**Example 2:**

**Input:** nums = [1,1,1]

**Output:** 0

**Explanation:**

`[1,1,1]` is the only subarray of length 3. However, its first and third numbers do not add to half the middle number.

**Constraints:**

* `3 <= nums.length <= 100`
* `-100 <= nums[i] <= 100`
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package g3301_3400.s3393_count_paths_with_the_given_xor_value

// #Medium #2024_12_30_Time_57_(68.42%)_Space_73.12_(52.63%)

class Solution {
private var m = -1
private var n = -1
private lateinit var dp: Array<Array<IntArray>>

fun countPathsWithXorValue(grid: Array<IntArray>, k: Int): Int {
m = grid.size
n = grid[0].size
dp = Array(m) { Array(n) { IntArray(16) { -1 } } }
return dfs(grid, 0, k, 0, 0)
}

private fun dfs(grid: Array<IntArray>, xorVal: Int, k: Int, i: Int, j: Int): Int {
var xorVal = xorVal
if (i < 0 || j < 0 || j >= n || i >= m) {
return 0
}
xorVal = xorVal xor grid[i][j]
if (dp[i][j][xorVal] != -1) {
return dp[i][j][xorVal]
}
if (i == m - 1 && j == n - 1 && xorVal == k) {
return 1
}
val down = dfs(grid, xorVal, k, i + 1, j)
val right = dfs(grid, xorVal, k, i, j + 1)
dp[i][j][xorVal] = (down + right) % MOD
return dp[i][j][xorVal]
}

companion object {
private val MOD = (1e9 + 7).toInt()
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
3393\. Count Paths With the Given XOR Value

Medium

You are given a 2D integer array `grid` with size `m x n`. You are also given an integer `k`.

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**:

* 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_.
* The `XOR` of all the numbers on the path must be **equal** to `k`.

Return the total number of such paths.

Since the answer can be very large, return the result **modulo** <code>10<sup>9</sup> + 7</code>.

**Example 1:**

**Input:** grid = [[2, 1, 5], [7, 10, 0], [12, 6, 4]], k = 11

**Output:** 3

**Explanation:**

The 3 paths are:

* `(0, 0) → (1, 0) → (2, 0) → (2, 1) → (2, 2)`
* `(0, 0) → (1, 0) → (1, 1) → (1, 2) → (2, 2)`
* `(0, 0) → (0, 1) → (1, 1) → (2, 1) → (2, 2)`

**Example 2:**

**Input:** grid = [[1, 3, 3, 3], [0, 3, 3, 2], [3, 0, 1, 1]], k = 2

**Output:** 5

**Explanation:**

The 5 paths are:

* `(0, 0) → (1, 0) → (2, 0) → (2, 1) → (2, 2) → (2, 3)`
* `(0, 0) → (1, 0) → (1, 1) → (2, 1) → (2, 2) → (2, 3)`
* `(0, 0) → (1, 0) → (1, 1) → (1, 2) → (1, 3) → (2, 3)`
* `(0, 0) → (0, 1) → (1, 1) → (1, 2) → (2, 2) → (2, 3)`
* `(0, 0) → (0, 1) → (0, 2) → (1, 2) → (2, 2) → (2, 3)`

**Example 3:**

**Input:** grid = [[1, 1, 1, 2], [3, 0, 3, 2], [3, 0, 2, 2]], k = 10

**Output:** 0

**Constraints:**

* `1 <= m == grid.length <= 300`
* `1 <= n == grid[r].length <= 300`
* `0 <= grid[r][c] < 16`
* `0 <= k < 16`
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package g3301_3400.s3394_check_if_grid_can_be_cut_into_sections

// #Medium #2024_12_22_Time_298_ms_(100.00%)_Space_132.4_MB_(100.00%)

import kotlin.math.max

@Suppress("unused")
class Solution {
fun checkValidCuts(n: Int, rectangles: Array<IntArray>): Boolean {
val m = rectangles.size
val xAxis = Array<IntArray>(m) { IntArray(2) }
val yAxis = Array<IntArray>(m) { IntArray(2) }
var ind = 0
for (axis in rectangles) {
val startX = axis[0]
val startY = axis[1]
val endX = axis[2]
val endY = axis[3]
xAxis[ind] = intArrayOf(startX, endX)
yAxis[ind] = intArrayOf(startY, endY)
ind++
}

xAxis.sortWith<IntArray>(
Comparator { a: IntArray, b: IntArray -> if (a[0] == b[0]) a[1] - b[1] else a[0] - b[0] },
)

yAxis.sortWith<IntArray>(
Comparator { a: IntArray, b: IntArray -> if (a[0] == b[0]) a[1] - b[1] else a[0] - b[0] },
)
val verticalCuts = findSections(xAxis)
if (verticalCuts > 2) {
return true
}
val horizontalCuts = findSections(yAxis)
return horizontalCuts > 2
}

private fun findSections(axis: Array<IntArray>): Int {
var end = axis[0][1]
var sections = 1
for (i in 1..<axis.size) {
if (end > axis[i][0]) {
end = max(end, axis[i][1])
} else {
sections++
end = axis[i][1]
}
if (sections > 2) {
return sections
}
}
return sections
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
3394\. Check if Grid can be Cut into Sections

Medium

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:

* <code>(start<sub>x</sub>, start<sub>y</sub>)</code>: The bottom-left corner of the rectangle.
* <code>(end<sub>x</sub>, end<sub>y</sub>)</code>: The top-right corner of the rectangle.

**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:

* Each of the three resulting sections formed by the cuts contains **at least** one rectangle.
* Every rectangle belongs to **exactly** one section.

Return `true` if such cuts can be made; otherwise, return `false`.

**Example 1:**

**Input:** n = 5, rectangles = [[1,0,5,2],[0,2,2,4],[3,2,5,3],[0,4,4,5]]

**Output:** true

**Explanation:**

![](https://assets.leetcode.com/uploads/2024/10/23/tt1drawio.png)

The grid is shown in the diagram. We can make horizontal cuts at `y = 2` and `y = 4`. Hence, output is true.

**Example 2:**

**Input:** n = 4, rectangles = [[0,0,1,1],[2,0,3,4],[0,2,2,3],[3,0,4,3]]

**Output:** true

**Explanation:**

![](https://assets.leetcode.com/uploads/2024/10/23/tc2drawio.png)

We can make vertical cuts at `x = 2` and `x = 3`. Hence, output is true.

**Example 3:**

**Input:** n = 4, rectangles = [[0,2,2,4],[1,0,3,2],[2,2,3,4],[3,0,4,2],[3,2,4,4]]

**Output:** false

**Explanation:**

We cannot make two horizontal or two vertical cuts that satisfy the conditions. Hence, output is false.

**Constraints:**

* <code>3 <= n <= 10<sup>9</sup></code>
* <code>3 <= rectangles.length <= 10<sup>5</sup></code>
* `0 <= rectangles[i][0] < rectangles[i][2] <= n`
* `0 <= rectangles[i][1] < rectangles[i][3] <= n`
* No two rectangles overlap.
Loading
Loading