Skip to content

Commit

Permalink
[LEET-3318] add 3318
Browse files Browse the repository at this point in the history
  • Loading branch information
fishercoder1534 committed Oct 22, 2024
1 parent 824958c commit 7b53699
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 0 deletions.
1 change: 1 addition & 0 deletions paginated_contents/algorithms/4th_thousand/README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
| # | Title | Solutions | Video | Difficulty | Tag
|------|---------------------------------------------------------------------------------------------------------------------------------------------------------------------|-----------------------------------------------------------------------------------------------------------------------------------|-------------------------------------------------------------------------------|------------|----------------------------------------------------------------------
| 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 |
| 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 |
| 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 |
| 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 |
Expand Down
53 changes: 53 additions & 0 deletions src/main/java/com/fishercoder/solutions/fourththousand/_3318.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package com.fishercoder.solutions.fourththousand;

import java.util.HashMap;
import java.util.Map;
import java.util.PriorityQueue;

public class _3318 {
public static class Solution1 {
public int[] findXSum(int[] nums, int k, int x) {
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
Map<Integer, int[]> map = new HashMap<>();
int i = 0;
for (; i < k; i++) {
int[] a = map.getOrDefault(nums[i], new int[2]);
a[0] = nums[i];
a[1]++;
map.put(nums[i], a);
}
maxHeap.addAll(map.values());
int[] ans = new int[nums.length - k + 1];
for (int j = i - 1, p = 0; j < nums.length; ) {
ans[p++] = computeTopX(new PriorityQueue<>(maxHeap), x);

j++;
if (j >= nums.length) {
break;
}
int[] a = map.getOrDefault(nums[j], new int[2]);
a[0] = nums[j];
a[1]++;
map.put(nums[j], a);

a = map.getOrDefault(nums[j - k], new int[2]);
a[0] = nums[j - k];
a[1]--;
map.put(nums[j - k], a);

maxHeap.clear();
maxHeap.addAll(map.values());
}
return ans;
}

private int computeTopX(PriorityQueue<int[]> maxHeap, int x) {
int sum = 0;
while (!maxHeap.isEmpty() && x-- > 0) {
int[] a = maxHeap.poll();
sum += a[0] * a[1];
}
return sum;
}
}
}
30 changes: 30 additions & 0 deletions src/test/java/com/fishercoder/fourththousand/_3318Test.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package com.fishercoder.fourththousand;

import com.fishercoder.solutions.fourththousand._3318;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.assertArrayEquals;

public class _3318Test {

private _3318.Solution1 solution1;
private static int[] nums;

@BeforeEach
public void setup() {
solution1 = new _3318.Solution1();
}

@Test
public void test1() {
nums = new int[]{1, 1, 2, 2, 3, 4, 2, 3};
assertArrayEquals(new int[]{6, 10, 12}, solution1.findXSum(nums, 6, 2));
}

@Test
public void test2() {
nums = new int[]{3, 8, 7, 8, 7, 5};
assertArrayEquals(new int[]{11, 15, 15, 15, 12}, solution1.findXSum(nums, 2, 2));
}
}

0 comments on commit 7b53699

Please sign in to comment.