Skip to content

Commit 725bb07

Browse files
committed
Added tasks 3379-3382
1 parent 8f72bed commit 725bb07

File tree

16 files changed

+513
-31
lines changed

16 files changed

+513
-31
lines changed

src/main/java/g3301_3400/s3375_minimum_operations_to_make_array_values_equal_to_k/Solution.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package g3301_3400.s3375_minimum_operations_to_make_array_values_equal_to_k;
22

3-
// #Easy #2024_12_07_Time_3_ms_(100.00%)_Space_44.5_MB_(100.00%)
3+
// #Easy #Array #Hash_Table #2024_12_10_Time_3_ms_(78.92%)_Space_44.6_MB_(67.39%)
44

55
import java.util.HashSet;
66
import java.util.Set;
Lines changed: 38 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,43 +1,53 @@
11
package g3301_3400.s3376_minimum_time_to_break_locks_i;
22

3-
// #Medium #2024_12_07_Time_760_ms_(100.00%)_Space_45_MB_(100.00%)
3+
// #Medium #Array #Dynamic_Programming #Bit_Manipulation #Backtracking #Bitmask
4+
// #2024_12_10_Time_3_ms_(99.63%)_Space_42_MB_(92.34%)
45

56
import java.util.ArrayList;
67
import java.util.Collections;
78
import java.util.List;
89

910
public class Solution {
1011
public int findMinimumTime(List<Integer> strength, int k) {
11-
List<Integer> perm = new ArrayList<>(strength);
12-
Collections.sort(perm);
13-
int minTime = Integer.MAX_VALUE;
14-
do {
15-
int time = 0;
16-
int factor = 1;
17-
for (int required : perm) {
18-
int neededTime = (required + factor - 1) / factor;
19-
time += neededTime;
20-
factor += k;
12+
List<Integer> strengthLocal = new ArrayList<>(strength);
13+
Collections.sort(strengthLocal);
14+
int res = strengthLocal.get(0);
15+
strengthLocal.remove(0);
16+
int x = 1;
17+
while (!strengthLocal.isEmpty()) {
18+
x += k;
19+
int nextTime = (strengthLocal.get(0) - 1) / x + 1;
20+
int canBreak = nextTime * x;
21+
int indexRemove = findIndex(strengthLocal, canBreak);
22+
if (strengthLocal.size() > 1) {
23+
int nextTime1 = (strengthLocal.get(1) - 1) / x + 1;
24+
int canBreak1 = nextTime1 * x;
25+
int indexRemove1 = findIndex(strengthLocal, canBreak1);
26+
if (nextTime1 + (strengthLocal.get(0) - 1) / (x + k)
27+
< nextTime + (strengthLocal.get(1) - 1) / (x + k)) {
28+
nextTime = nextTime1;
29+
indexRemove = indexRemove1;
30+
}
2131
}
22-
minTime = Math.min(minTime, time);
23-
} while (nextPermutation(perm));
24-
return minTime;
32+
res += nextTime;
33+
strengthLocal.remove(indexRemove);
34+
}
35+
return res;
2536
}
2637

27-
private boolean nextPermutation(List<Integer> nums) {
28-
int i = nums.size() - 2;
29-
while (i >= 0 && nums.get(i) >= nums.get(i + 1)) {
30-
i--;
31-
}
32-
if (i < 0) {
33-
return false;
34-
}
35-
int j = nums.size() - 1;
36-
while (nums.get(j) <= nums.get(i)) {
37-
j--;
38+
private int findIndex(List<Integer> strength, int canBreak) {
39+
int l = 0;
40+
int r = strength.size() - 1;
41+
int res = -1;
42+
while (l <= r) {
43+
int mid = (l + r) / 2;
44+
if (strength.get(mid) <= canBreak) {
45+
res = mid;
46+
l = mid + 1;
47+
} else {
48+
r = mid - 1;
49+
}
3850
}
39-
Collections.swap(nums, i, j);
40-
Collections.reverse(nums.subList(i + 1, nums.size()));
41-
return true;
51+
return res;
4252
}
4353
}

src/main/java/g3301_3400/s3377_digit_operations_to_make_two_integers_equal/Solution.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package g3301_3400.s3377_digit_operations_to_make_two_integers_equal;
22

3-
// #Medium #2024_12_07_Time_255_ms_(100.00%)_Space_44.9_MB_(100.00%)
3+
// #Medium #Math #Heap_Priority_Queue #Graph #Shortest_Path #Number_Theory
4+
// #2024_12_10_Time_246_ms_(38.59%)_Space_45.2_MB_(73.52%)
45

56
import java.util.Arrays;
67
import java.util.PriorityQueue;

src/main/java/g3301_3400/s3378_count_connected_components_in_lcm_graph/Solution.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package g3301_3400.s3378_count_connected_components_in_lcm_graph;
22

3-
// #Hard #2024_12_07_Time_48_ms_(100.00%)_Space_59.5_MB_(100.00%)
3+
// #Hard #Array #Hash_Table #Math #Union_Find #Number_Theory
4+
// #2024_12_10_Time_68_ms_(67.83%)_Space_59.8_MB_(62.24%)
45

56
import java.util.ArrayList;
67
import java.util.Arrays;
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package g3301_3400.s3379_transformed_array;
2+
3+
// #Easy #Array #Simulation #2024_12_10_Time_1_ms_(99.87%)_Space_44.9_MB_(75.08%)
4+
5+
public class Solution {
6+
public int[] constructTransformedArray(int[] nums) {
7+
int n = nums.length;
8+
int[] res = new int[n];
9+
for (int i = 0; i < n; i++) {
10+
if (nums[i] == 0) {
11+
res[i] = nums[i];
12+
}
13+
if (nums[i] > 0) {
14+
res[i] = nums[(i + nums[i]) % n];
15+
} else if (nums[i] < 0) {
16+
int r = (Math.abs(nums[i])) / n;
17+
res[i] = nums[Math.abs((i + nums[i] + r * n + n)) % n];
18+
}
19+
}
20+
return res;
21+
}
22+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
3379\. Transformed Array
2+
3+
Easy
4+
5+
You are given an integer array `nums` that represents a circular array. Your task is to create a new array `result` of the **same** size, following these rules:
6+
7+
For each index `i` (where `0 <= i < nums.length`), perform the following **independent** actions:
8+
9+
* If `nums[i] > 0`: Start at index `i` and move `nums[i]` steps to the **right** in the circular array. Set `result[i]` to the value of the index where you land.
10+
* If `nums[i] < 0`: Start at index `i` and move `abs(nums[i])` steps to the **left** in the circular array. Set `result[i]` to the value of the index where you land.
11+
* If `nums[i] == 0`: Set `result[i]` to `nums[i]`.
12+
13+
Return the new array `result`.
14+
15+
**Note:** Since `nums` is circular, moving past the last element wraps around to the beginning, and moving before the first element wraps back to the end.
16+
17+
**Example 1:**
18+
19+
**Input:** nums = [3,-2,1,1]
20+
21+
**Output:** [1,1,1,3]
22+
23+
**Explanation:**
24+
25+
* For `nums[0]` that is equal to 3, If we move 3 steps to right, we reach `nums[3]`. So `result[0]` should be 1.
26+
* For `nums[1]` that is equal to -2, If we move 2 steps to left, we reach `nums[3]`. So `result[1]` should be 1.
27+
* For `nums[2]` that is equal to 1, If we move 1 step to right, we reach `nums[3]`. So `result[2]` should be 1.
28+
* For `nums[3]` that is equal to 1, If we move 1 step to right, we reach `nums[0]`. So `result[3]` should be 3.
29+
30+
**Example 2:**
31+
32+
**Input:** nums = [-1,4,-1]
33+
34+
**Output:** [-1,-1,4]
35+
36+
**Explanation:**
37+
38+
* For `nums[0]` that is equal to -1, If we move 1 step to left, we reach `nums[2]`. So `result[0]` should be -1.
39+
* For `nums[1]` that is equal to 4, If we move 4 steps to right, we reach `nums[2]`. So `result[1]` should be -1.
40+
* For `nums[2]` that is equal to -1, If we move 1 step to left, we reach `nums[1]`. So `result[2]` should be 4.
41+
42+
**Constraints:**
43+
44+
* `1 <= nums.length <= 100`
45+
* `-100 <= nums[i] <= 100`
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package g3301_3400.s3380_maximum_area_rectangle_with_point_constraints_i;
2+
3+
// #Medium #Array #Math #Sorting #Enumeration #Geometry #Segment_Tree #Binary_Indexed_Tree
4+
// #2024_12_10_Time_8_ms_(81.05%)_Space_45_MB_(66.32%)
5+
6+
import java.util.Arrays;
7+
import java.util.HashSet;
8+
import java.util.Set;
9+
10+
public class Solution {
11+
public int maxRectangleArea(int[][] points) {
12+
Set<String> set = new HashSet<>();
13+
for (int[] p : points) {
14+
set.add(Arrays.toString(p));
15+
}
16+
int maxArea = -1;
17+
for (int[] point : points) {
18+
for (int j = 1; j < points.length; j++) {
19+
int[] p2 = points[j];
20+
if (point[0] == p2[0]
21+
|| point[1] == p2[1]
22+
|| !set.contains(Arrays.toString(new int[] {point[0], p2[1]}))
23+
|| !set.contains(Arrays.toString(new int[] {p2[0], point[1]}))
24+
|| !validate(points, point, p2)) {
25+
continue;
26+
}
27+
maxArea = Math.max(maxArea, (p2[1] - point[1]) * (p2[0] - point[0]));
28+
}
29+
}
30+
return maxArea;
31+
}
32+
33+
private boolean validate(int[][] points, int[] p1, int[] p2) {
34+
int top = Math.max(p1[1], p2[1]);
35+
int bot = Math.min(p1[1], p2[1]);
36+
int left = Math.min(p1[0], p2[0]);
37+
int right = Math.max(p1[0], p2[0]);
38+
for (int[] p : points) {
39+
int x = p[0];
40+
int y = p[1];
41+
if ((y == top || y == bot) && x > left && x < right
42+
|| (x == left || x == right) && y > bot && y < top
43+
|| (x > left && x < right && y > bot && y < top)) {
44+
return false;
45+
}
46+
}
47+
return true;
48+
}
49+
}
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
3380\. Maximum Area Rectangle With Point Constraints I
2+
3+
Medium
4+
5+
You are given an array `points` where <code>points[i] = [x<sub>i</sub>, y<sub>i</sub>]</code> represents the coordinates of a point on an infinite plane.
6+
7+
Your task is to find the **maximum** area of a rectangle that:
8+
9+
* Can be formed using **four** of these points as its corners.
10+
* Does **not** contain any other point inside or on its border.
11+
* Has its edges **parallel** to the axes.
12+
13+
Return the **maximum area** that you can obtain or -1 if no such rectangle is possible.
14+
15+
**Example 1:**
16+
17+
**Input:** points = [[1,1],[1,3],[3,1],[3,3]]
18+
19+
**Output:** 4
20+
21+
**Explanation:**
22+
23+
**![Example 1 diagram](https://assets.leetcode.com/uploads/2024/11/02/example1.png)**
24+
25+
We can make a rectangle with these 4 points as corners and there is no other point that lies inside or on the border. Hence, the maximum possible area would be 4.
26+
27+
**Example 2:**
28+
29+
**Input:** points = [[1,1],[1,3],[3,1],[3,3],[2,2]]
30+
31+
**Output:** \-1
32+
33+
**Explanation:**
34+
35+
**![Example 2 diagram](https://assets.leetcode.com/uploads/2024/11/02/example2.png)**
36+
37+
There is only one rectangle possible is with points `[1,1], [1,3], [3,1]` and `[3,3]` but `[2,2]` will always lie inside it. Hence, returning -1.
38+
39+
**Example 3:**
40+
41+
**Input:** points = [[1,1],[1,3],[3,1],[3,3],[1,2],[3,2]]
42+
43+
**Output:** 2
44+
45+
**Explanation:**
46+
47+
**![Example 3 diagram](https://assets.leetcode.com/uploads/2024/11/02/example3.png)**
48+
49+
The maximum area rectangle is formed by the points `[1,3], [1,2], [3,2], [3,3]`, which has an area of 2. Additionally, the points `[1,1], [1,2], [3,1], [3,2]` also form a valid rectangle with the same area.
50+
51+
**Constraints:**
52+
53+
* `1 <= points.length <= 10`
54+
* `points[i].length == 2`
55+
* <code>0 <= x<sub>i</sub>, y<sub>i</sub> <= 100</code>
56+
* All the given points are **unique**.
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package g3301_3400.s3381_maximum_subarray_sum_with_length_divisible_by_k;
2+
3+
// #Medium #Array #Hash_Table #Prefix_Sum #2024_12_10_Time_4_ms_(100.00%)_Space_80.2_MB_(22.05%)
4+
5+
public class Solution {
6+
public long maxSubarraySum(int[] nums, int k) {
7+
int n = nums.length;
8+
long[] maxSum = new long[n];
9+
long minSum = 0;
10+
for (int i = n - 1; i > n - k; i--) {
11+
maxSum[i] = Integer.MIN_VALUE;
12+
minSum += nums[i];
13+
}
14+
minSum += nums[n - k];
15+
maxSum[n - k] = minSum;
16+
long ans = minSum;
17+
for (int i = n - k - 1; i >= 0; i--) {
18+
minSum = minSum + nums[i] - nums[i + k];
19+
maxSum[i] = Math.max(minSum, minSum + maxSum[i + k]);
20+
ans = Math.max(maxSum[i], ans);
21+
}
22+
return ans;
23+
}
24+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
3381\. Maximum Subarray Sum With Length Divisible by K
2+
3+
Medium
4+
5+
You are given an array of integers `nums` and an integer `k`.
6+
7+
Return the **maximum** sum of a **non-empty subarray** of `nums`, such that the size of the subarray is **divisible** by `k`.
8+
9+
A **subarray** is a contiguous **non-empty** sequence of elements within an array.
10+
11+
**Example 1:**
12+
13+
**Input:** nums = [1,2], k = 1
14+
15+
**Output:** 3
16+
17+
**Explanation:**
18+
19+
The subarray `[1, 2]` with sum 3 has length equal to 2 which is divisible by 1.
20+
21+
**Example 2:**
22+
23+
**Input:** nums = [-1,-2,-3,-4,-5], k = 4
24+
25+
**Output:** \-10
26+
27+
**Explanation:**
28+
29+
The maximum sum subarray is `[-1, -2, -3, -4]` which has length equal to 4 which is divisible by 4.
30+
31+
**Example 3:**
32+
33+
**Input:** nums = [-5,1,2,-3,4], k = 2
34+
35+
**Output:** 4
36+
37+
**Explanation:**
38+
39+
The maximum sum subarray is `[1, 2, -3, 4]` which has length equal to 4 which is divisible by 2.
40+
41+
**Constraints:**
42+
43+
* <code>1 <= k <= nums.length <= 2 * 10<sup>5</sup></code>
44+
* <code>-10<sup>9</sup> <= nums[i] <= 10<sup>9</sup></code>

0 commit comments

Comments
 (0)