Skip to content

Added tasks 3392-3405 #1887

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 13 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
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -706,7 +706,7 @@ implementation 'com.github.javadev:leetcode-in-java:1.39'

| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
|-|-|-|-|-|-
| 0976 |[Largest Perimeter Triangle](src/main/java/g0901_1000/s0976_largest_perimeter_triangle/Solution.java)| Easy | Array, Math, Sorting, Greedy | 12 | 26.01
| 0976 |[Largest Perimeter Triangle](src/main/java/g0901_1000/s0976_largest_perimeter_triangle/Solution.java)| Easy | Array, Math, Sorting, Greedy | 7 | 99.33
| 1779 |[Find Nearest Point That Has the Same X or Y Coordinate](src/main/java/g1701_1800/s1779_find_nearest_point_that_has_the_same_x_or_y_coordinate/Solution.java)| Easy | Array | 1 | 100.00

#### Day 4 Loop
Expand Down
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_1_ms_(100.00%)_Space_45.5_MB_(100.00%)

public class Solution {
public int countSubarrays(int[] nums) {
int window = 3;
int cnt = 0;
for (int i = 0; i <= nums.length - window; i++) {
float first = nums[i];
float second = nums[i + 1];
float third = nums[i + 2];
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,41 @@
package g3301_3400.s3393_count_paths_with_the_given_xor_value;

// #Medium #2024_12_22_Time_83_ms_(100.00%)_Space_57_MB_(100.00%)

import java.util.Arrays;

public class Solution {
private static final int MOD = (int) (1e9 + 7);
private int m = -1;
private int n = -1;
private int[][][] dp;

public int countPathsWithXorValue(int[][] grid, int k) {
m = grid.length;
n = grid[0].length;
dp = new int[m][n][16];
for (int[][] a : dp) {
for (int[] b : a) {
Arrays.fill(b, -1);
}
}
return dfs(grid, 0, k, 0, 0);
}

private int dfs(int[][] grid, int xorVal, int k, int i, int j) {
if (i < 0 || j < 0 || j >= n || i >= m) {
return 0;
}
xorVal ^= 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;
}
int down = dfs(grid, xorVal, k, i + 1, j);
int right = dfs(grid, xorVal, k, i, j + 1);
dp[i][j][xorVal] = (down + right) % MOD;
return dp[i][j][xorVal];
}
}
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,49 @@
package g3301_3400.s3394_check_if_grid_can_be_cut_into_sections;

// #Medium #2024_12_22_Time_136_ms_(100.00%)_Space_128.7_MB_(100.00%)

import java.util.Arrays;

@SuppressWarnings({"unused", "java:S1172"})
public class Solution {
public boolean checkValidCuts(int n, int[][] rectangles) {
int m = rectangles.length;
int[][] xAxis = new int[m][2];
int[][] yAxis = new int[m][2];
int ind = 0;
for (int[] axis : rectangles) {
int startX = axis[0];
int startY = axis[1];
int endX = axis[2];
int endY = axis[3];
xAxis[ind] = new int[] {startX, endX};
yAxis[ind] = new int[] {startY, endY};
ind++;
}
Arrays.sort(xAxis, (a, b) -> a[0] == b[0] ? a[1] - b[1] : a[0] - b[0]);
Arrays.sort(yAxis, (a, b) -> a[0] == b[0] ? a[1] - b[1] : a[0] - b[0]);
int verticalCuts = findSections(xAxis);
if (verticalCuts > 2) {
return true;
}
int horizontalCuts = findSections(yAxis);
return horizontalCuts > 2;
}

private int findSections(int[][] axis) {
int end = axis[0][1];
int sections = 1;
for (int i = 1; i < axis.length; i++) {
if (end > axis[i][0]) {
end = Math.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