Skip to content

Commit d215170

Browse files
authored
Added tasks 3392-3405
1 parent 52b45c6 commit d215170

File tree

37 files changed

+1441
-1
lines changed

37 files changed

+1441
-1
lines changed

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -706,7 +706,7 @@ implementation 'com.github.javadev:leetcode-in-java:1.39'
706706

707707
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
708708
|-|-|-|-|-|-
709-
| 0976 |[Largest Perimeter Triangle](src/main/java/g0901_1000/s0976_largest_perimeter_triangle/Solution.java)| Easy | Array, Math, Sorting, Greedy | 12 | 26.01
709+
| 0976 |[Largest Perimeter Triangle](src/main/java/g0901_1000/s0976_largest_perimeter_triangle/Solution.java)| Easy | Array, Math, Sorting, Greedy | 7 | 99.33
710710
| 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
711711

712712
#### Day 4 Loop
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_1_ms_(100.00%)_Space_45.5_MB_(100.00%)
4+
5+
public class Solution {
6+
public int countSubarrays(int[] nums) {
7+
int window = 3;
8+
int cnt = 0;
9+
for (int i = 0; i <= nums.length - window; i++) {
10+
float first = nums[i];
11+
float second = nums[i + 1];
12+
float third = nums[i + 2];
13+
if (second / 2 == first + third) {
14+
cnt++;
15+
}
16+
}
17+
return cnt;
18+
}
19+
}
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`
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package g3301_3400.s3393_count_paths_with_the_given_xor_value;
2+
3+
// #Medium #2024_12_22_Time_83_ms_(100.00%)_Space_57_MB_(100.00%)
4+
5+
import java.util.Arrays;
6+
7+
public class Solution {
8+
private static final int MOD = (int) (1e9 + 7);
9+
private int m = -1;
10+
private int n = -1;
11+
private int[][][] dp;
12+
13+
public int countPathsWithXorValue(int[][] grid, int k) {
14+
m = grid.length;
15+
n = grid[0].length;
16+
dp = new int[m][n][16];
17+
for (int[][] a : dp) {
18+
for (int[] b : a) {
19+
Arrays.fill(b, -1);
20+
}
21+
}
22+
return dfs(grid, 0, k, 0, 0);
23+
}
24+
25+
private int dfs(int[][] grid, int xorVal, int k, int i, int j) {
26+
if (i < 0 || j < 0 || j >= n || i >= m) {
27+
return 0;
28+
}
29+
xorVal ^= grid[i][j];
30+
if (dp[i][j][xorVal] != -1) {
31+
return dp[i][j][xorVal];
32+
}
33+
if (i == m - 1 && j == n - 1 && xorVal == k) {
34+
return 1;
35+
}
36+
int down = dfs(grid, xorVal, k, i + 1, j);
37+
int right = dfs(grid, xorVal, k, i, j + 1);
38+
dp[i][j][xorVal] = (down + right) % MOD;
39+
return dp[i][j][xorVal];
40+
}
41+
}
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`
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package g3301_3400.s3394_check_if_grid_can_be_cut_into_sections;
2+
3+
// #Medium #2024_12_22_Time_136_ms_(100.00%)_Space_128.7_MB_(100.00%)
4+
5+
import java.util.Arrays;
6+
7+
@SuppressWarnings({"unused", "java:S1172"})
8+
public class Solution {
9+
public boolean checkValidCuts(int n, int[][] rectangles) {
10+
int m = rectangles.length;
11+
int[][] xAxis = new int[m][2];
12+
int[][] yAxis = new int[m][2];
13+
int ind = 0;
14+
for (int[] axis : rectangles) {
15+
int startX = axis[0];
16+
int startY = axis[1];
17+
int endX = axis[2];
18+
int endY = axis[3];
19+
xAxis[ind] = new int[] {startX, endX};
20+
yAxis[ind] = new int[] {startY, endY};
21+
ind++;
22+
}
23+
Arrays.sort(xAxis, (a, b) -> a[0] == b[0] ? a[1] - b[1] : a[0] - b[0]);
24+
Arrays.sort(yAxis, (a, b) -> a[0] == b[0] ? a[1] - b[1] : a[0] - b[0]);
25+
int verticalCuts = findSections(xAxis);
26+
if (verticalCuts > 2) {
27+
return true;
28+
}
29+
int horizontalCuts = findSections(yAxis);
30+
return horizontalCuts > 2;
31+
}
32+
33+
private int findSections(int[][] axis) {
34+
int end = axis[0][1];
35+
int sections = 1;
36+
for (int i = 1; i < axis.length; i++) {
37+
if (end > axis[i][0]) {
38+
end = Math.max(end, axis[i][1]);
39+
} else {
40+
sections++;
41+
end = axis[i][1];
42+
}
43+
if (sections > 2) {
44+
return sections;
45+
}
46+
}
47+
return sections;
48+
}
49+
}
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)