Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 69e913f

Browse files
authored
Added tasks 2333, 2334.
1 parent f54b094 commit 69e913f

File tree

7 files changed

+233
-0
lines changed

7 files changed

+233
-0
lines changed

‎README.md‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1848,6 +1848,8 @@ implementation 'com.github.javadev:leetcode-in-java:1.11'
18481848

18491849
| # | Title | Difficulty | Tag | Time, ms | Time, %
18501850
|------|----------------|-------------|-------------|----------|---------
1851+
| 2334 |[Subarray With Elements Greater Than Varying Threshold](src/main/java/g2301_2400/s2334_subarray_with_elements_greater_than_varying_threshold/Solution.java)| Hard || 385 | 31.56
1852+
| 2333 |[Minimum Sum of Squared Difference](src/main/java/g2301_2400/s2333_minimum_sum_of_squared_difference/Solution.java)| Medium || 18 | 89.38
18511853
| 2332 |[The Latest Time to Catch a Bus](src/main/java/g2301_2400/s2332_the_latest_time_to_catch_a_bus/Solution.java)| Medium || 28 | 100.00
18521854
| 2331 |[Evaluate Boolean Binary Tree](src/main/java/g2301_2400/s2331_evaluate_boolean_binary_tree/Solution.java)| Easy || 0 | 100.00
18531855
| 2328 |[Number of Increasing Paths in a Grid](src/main/java/g2301_2400/s2328_number_of_increasing_paths_in_a_grid/Solution.java)| Hard | Array, Dynamic_Programming, Depth_First_Search, Breadth_First_Search, Graph, Topological_Sort, Memoization, Matrix | 43 | 100.00
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
package g2301_2400.s2333_minimum_sum_of_squared_difference;
2+
3+
// #Medium #2022_07_12_Time_15_ms_(95.13%)_Space_106.4_MB_(46.91%)
4+
5+
public class Solution {
6+
public long minSumSquareDiff(int[] nums1, int[] nums2, int k1, int k2) {
7+
long minSumSquare = 0;
8+
int[] diffs = new int[100_001];
9+
long totalDiff = 0;
10+
long kSum = (long) k1 + k2;
11+
int currentDiff;
12+
int maxDiff = 0;
13+
for (int i = 0; i < nums1.length; i++) {
14+
// get current diff.
15+
currentDiff = Math.abs(nums1[i] - nums2[i]);
16+
// if current diff > 0, count/store it. If not,then ignore it.
17+
if (currentDiff > 0) {
18+
totalDiff += currentDiff;
19+
diffs[currentDiff]++;
20+
maxDiff = Math.max(maxDiff, currentDiff);
21+
}
22+
}
23+
// if kSum (k1 + k2) < totalDifferences, it means we can make all numbers/differences 0s
24+
if (totalDiff <= kSum) {
25+
return 0;
26+
}
27+
// starting from the back, from the highest difference, lower that group one by one to the
28+
// previous group.
29+
// we need to make all n diffs to n-1, then n-2, as long as kSum allows it.
30+
for (int i = maxDiff; i > 0 && kSum > 0; i--) {
31+
if (diffs[i] > 0) {
32+
// if current group has more differences than the totalK, we can only move k of them
33+
// to the lower level.
34+
if (diffs[i] >= kSum) {
35+
diffs[i] -= kSum;
36+
diffs[i - 1] += kSum;
37+
kSum = 0;
38+
} else {
39+
// else, we can make this whole group one level lower.
40+
diffs[i - 1] += diffs[i];
41+
kSum -= diffs[i];
42+
diffs[i] = 0;
43+
}
44+
}
45+
}
46+
for (int i = 0; i <= maxDiff; i++) {
47+
if (diffs[i] > 0) {
48+
minSumSquare += (long) (Math.pow(i, 2)) * diffs[i];
49+
}
50+
}
51+
return minSumSquare;
52+
}
53+
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
2333\. Minimum Sum of Squared Difference
2+
3+
Medium
4+
5+
You are given two positive **0-indexed** integer arrays `nums1` and `nums2`, both of length `n`.
6+
7+
The **sum of squared difference** of arrays `nums1` and `nums2` is defined as the **sum** of <code>(nums1[i] - nums2[i])<sup>2</sup></code> for each `0 <= i < n`.
8+
9+
You are also given two positive integers `k1` and `k2`. You can modify any of the elements of `nums1` by `+1` or `-1` at most `k1` times. Similarly, you can modify any of the elements of `nums2` by `+1` or `-1` at most `k2` times.
10+
11+
Return _the minimum **sum of squared difference** after modifying array_ `nums1` _at most_ `k1` _times and modifying array_ `nums2` _at most_ `k2` _times_.
12+
13+
**Note**: You are allowed to modify the array elements to become **negative** integers.
14+
15+
**Example 1:**
16+
17+
**Input:** nums1 = [1,2,3,4], nums2 = [2,10,20,19], k1 = 0, k2 = 0
18+
19+
**Output:** 579
20+
21+
**Explanation:** The elements in nums1 and nums2 cannot be modified because k1 = 0 and k2 = 0.
22+
23+
The sum of square difference will be: (1 - 2)<sup>2</sup> \+ (2 - 10)<sup>2</sup> \+ (3 - 20)<sup>2</sup> \+ (4 - 19)<sup>2</sup> = 579.
24+
25+
**Example 2:**
26+
27+
**Input:** nums1 = [1,4,10,12], nums2 = [5,8,6,9], k1 = 1, k2 = 1
28+
29+
**Output:** 43
30+
31+
**Explanation:** One way to obtain the minimum sum of square difference is:
32+
33+
- Increase nums1[0] once.
34+
35+
- Increase nums2[2] once.
36+
37+
The minimum of the sum of square difference will be: (2 - 5)<sup>2</sup> \+ (4 - 8)<sup>2</sup> \+ (10 - 7)<sup>2</sup> \+ (12 - 9)<sup>2</sup> = 43.
38+
39+
Note that, there are other ways to obtain the minimum of the sum of square difference, but there is no way to obtain a sum smaller than 43.
40+
41+
**Constraints:**
42+
43+
* `n == nums1.length == nums2.length`
44+
* <code>1 <= n <= 10<sup>5</sup></code>
45+
* <code>0 <= nums1[i], nums2[i] <= 10<sup>5</sup></code>
46+
* <code>0 <= k1, k2 <= 10<sup>9</sup></code>
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package g2301_2400.s2334_subarray_with_elements_greater_than_varying_threshold;
2+
3+
// #Hard #2022_07_12_Time_385_ms_(31.56%)_Space_135.2_MB_(5.78%)
4+
5+
import java.util.Arrays;
6+
import java.util.Comparator;
7+
import java.util.PriorityQueue;
8+
import java.util.TreeSet;
9+
10+
public class Solution {
11+
public int validSubarraySize(int[] nums, int threshold) {
12+
int n = nums.length;
13+
int[] min = new int[n];
14+
// base case
15+
TreeSet<Integer> dead = new TreeSet<>(Arrays.asList(n, -1));
16+
PriorityQueue<Integer> maxheap = new PriorityQueue<>(Comparator.comparingInt(o -> -min[o]));
17+
for (int i = 0; i < n; i++) {
18+
min[i] = threshold / nums[i] + 1;
19+
if (min[i] > nums.length) {
20+
// dead, this element should never appear in the answer
21+
dead.add(i);
22+
} else {
23+
maxheap.offer(i);
24+
}
25+
}
26+
while (!maxheap.isEmpty()) {
27+
int cur = maxheap.poll();
28+
if (dead.higher(cur) - dead.lower(cur) - 1 < min[cur]) {
29+
// widest open range < minimum required length, this index is also bad.
30+
dead.add(cur);
31+
} else {
32+
// otherwise we've found it!
33+
return min[cur];
34+
}
35+
}
36+
return -1;
37+
}
38+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
2334\. Subarray With Elements Greater Than Varying Threshold
2+
3+
Hard
4+
5+
You are given an integer array `nums` and an integer `threshold`.
6+
7+
Find any subarray of `nums` of length `k` such that **every** element in the subarray is **greater** than `threshold / k`.
8+
9+
Return _the **size** of **any** such subarray_. If there is no such subarray, return `-1`.
10+
11+
A **subarray** is a contiguous non-empty sequence of elements within an array.
12+
13+
**Example 1:**
14+
15+
**Input:** nums = [1,3,4,3,1], threshold = 6
16+
17+
**Output:** 3
18+
19+
**Explanation:** The subarray [3,4,3] has a size of 3, and every element is greater than 6 / 3 = 2.
20+
21+
Note that this is the only valid subarray.
22+
23+
**Example 2:**
24+
25+
**Input:** nums = [6,5,6,5,8], threshold = 7
26+
27+
**Output:** 1
28+
29+
**Explanation:** The subarray [8] has a size of 1, and 8 > 7 / 1 = 7. So 1 is returned.
30+
31+
Note that the subarray [6,5] has a size of 2, and every element is greater than 7 / 2 = 3.5.
32+
33+
Similarly, the subarrays [6,5,6], [6,5,6,5], [6,5,6,5,8] also satisfy the given conditions.
34+
35+
Therefore, 2, 3, 4, or 5 may also be returned.
36+
37+
**Constraints:**
38+
39+
* <code>1 <= nums.length <= 10<sup>5</sup></code>
40+
* <code>1 <= nums[i], threshold <= 10<sup>9</sup></code>
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package g2301_2400.s2333_minimum_sum_of_squared_difference;
2+
3+
import static org.hamcrest.CoreMatchers.equalTo;
4+
import static org.hamcrest.MatcherAssert.assertThat;
5+
6+
import org.junit.jupiter.api.Test;
7+
8+
class SolutionTest {
9+
@Test
10+
void minSumSquareDiff() {
11+
assertThat(
12+
new Solution()
13+
.minSumSquareDiff(new int[] {1, 2, 3, 4}, new int[] {2, 10, 20, 19}, 0, 0),
14+
equalTo(579L));
15+
}
16+
17+
@Test
18+
void minSumSquareDiff2() {
19+
assertThat(
20+
new Solution()
21+
.minSumSquareDiff(new int[] {1, 4, 10, 12}, new int[] {5, 8, 6, 9}, 1, 1),
22+
equalTo(43L));
23+
}
24+
25+
@Test
26+
void minSumSquareDiff3() {
27+
assertThat(
28+
new Solution()
29+
.minSumSquareDiff(
30+
new int[] {7, 11, 4, 19, 11, 5, 6, 1, 8},
31+
new int[] {4, 7, 6, 16, 12, 9, 10, 2, 10},
32+
3,
33+
6),
34+
equalTo(27L));
35+
}
36+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package g2301_2400.s2334_subarray_with_elements_greater_than_varying_threshold;
2+
3+
import static org.hamcrest.CoreMatchers.equalTo;
4+
import static org.hamcrest.MatcherAssert.assertThat;
5+
6+
import org.junit.jupiter.api.Test;
7+
8+
class SolutionTest {
9+
@Test
10+
void validSubarraySize() {
11+
assertThat(new Solution().validSubarraySize(new int[] {1, 3, 4, 3, 1}, 6), equalTo(3));
12+
}
13+
14+
@Test
15+
void validSubarraySize2() {
16+
assertThat(new Solution().validSubarraySize(new int[] {6, 5, 6, 5, 8}, 7), equalTo(2));
17+
}
18+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /