Skip to content

Commit 7b53699

Browse files
[LEET-3318] add 3318
1 parent 824958c commit 7b53699

File tree

3 files changed

+84
-0
lines changed
  • paginated_contents/algorithms/4th_thousand
  • src

3 files changed

+84
-0
lines changed

Diff for: paginated_contents/algorithms/4th_thousand/README.md

+1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
| # | Title | Solutions | Video | Difficulty | Tag
22
|------|---------------------------------------------------------------------------------------------------------------------------------------------------------------------|-----------------------------------------------------------------------------------------------------------------------------------|-------------------------------------------------------------------------------|------------|----------------------------------------------------------------------
3+
| 3318 | [Find X-Sum of All K-Long Subarrays I](https://leetcode.com/problems/find-x-sum-of-all-k-long-subarrays-i/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/fourththousand/_3318.java) | | Easy |
34
| 3285 | [Find Indices of Stable Mountains](https://leetcode.com/problems/find-indices-of-stable-mountains/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/fourththousand/_3285.java) | | Easy |
45
| 3264 | [Final Array State After K Multiplication Operations I](https://leetcode.com/problems/final-array-state-after-k-multiplication-operations-i/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/fourththousand/_3264.java) | | Easy |
56
| 3263 | [Convert Doubly Linked List to Array I](https://leetcode.com/problems/convert-doubly-linked-list-to-array-i/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/fourththousand/_3263.java) | | Easy |
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
package com.fishercoder.solutions.fourththousand;
2+
3+
import java.util.HashMap;
4+
import java.util.Map;
5+
import java.util.PriorityQueue;
6+
7+
public class _3318 {
8+
public static class Solution1 {
9+
public int[] findXSum(int[] nums, int k, int x) {
10+
PriorityQueue<int[]> maxHeap = new PriorityQueue<>((a, b) -> a[1] != b[1] ? b[1] - a[1] : b[0] - a[0]);//a[0] is the number itself, a[1] is the frequency
11+
Map<Integer, int[]> map = new HashMap<>();
12+
int i = 0;
13+
for (; i < k; i++) {
14+
int[] a = map.getOrDefault(nums[i], new int[2]);
15+
a[0] = nums[i];
16+
a[1]++;
17+
map.put(nums[i], a);
18+
}
19+
maxHeap.addAll(map.values());
20+
int[] ans = new int[nums.length - k + 1];
21+
for (int j = i - 1, p = 0; j < nums.length; ) {
22+
ans[p++] = computeTopX(new PriorityQueue<>(maxHeap), x);
23+
24+
j++;
25+
if (j >= nums.length) {
26+
break;
27+
}
28+
int[] a = map.getOrDefault(nums[j], new int[2]);
29+
a[0] = nums[j];
30+
a[1]++;
31+
map.put(nums[j], a);
32+
33+
a = map.getOrDefault(nums[j - k], new int[2]);
34+
a[0] = nums[j - k];
35+
a[1]--;
36+
map.put(nums[j - k], a);
37+
38+
maxHeap.clear();
39+
maxHeap.addAll(map.values());
40+
}
41+
return ans;
42+
}
43+
44+
private int computeTopX(PriorityQueue<int[]> maxHeap, int x) {
45+
int sum = 0;
46+
while (!maxHeap.isEmpty() && x-- > 0) {
47+
int[] a = maxHeap.poll();
48+
sum += a[0] * a[1];
49+
}
50+
return sum;
51+
}
52+
}
53+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package com.fishercoder.fourththousand;
2+
3+
import com.fishercoder.solutions.fourththousand._3318;
4+
import org.junit.jupiter.api.BeforeEach;
5+
import org.junit.jupiter.api.Test;
6+
7+
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
8+
9+
public class _3318Test {
10+
11+
private _3318.Solution1 solution1;
12+
private static int[] nums;
13+
14+
@BeforeEach
15+
public void setup() {
16+
solution1 = new _3318.Solution1();
17+
}
18+
19+
@Test
20+
public void test1() {
21+
nums = new int[]{1, 1, 2, 2, 3, 4, 2, 3};
22+
assertArrayEquals(new int[]{6, 10, 12}, solution1.findXSum(nums, 6, 2));
23+
}
24+
25+
@Test
26+
public void test2() {
27+
nums = new int[]{3, 8, 7, 8, 7, 5};
28+
assertArrayEquals(new int[]{11, 15, 15, 15, 12}, solution1.findXSum(nums, 2, 2));
29+
}
30+
}

0 commit comments

Comments
 (0)