-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
824958c
commit 7b53699
Showing
3 changed files
with
84 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
53 changes: 53 additions & 0 deletions
53
src/main/java/com/fishercoder/solutions/fourththousand/_3318.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
30
src/test/java/com/fishercoder/fourththousand/_3318Test.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | ||
} | ||
} |