Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions .github/workflows/gradle.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,10 @@ jobs:

steps:
- uses: actions/checkout@v4
- name: Set up JDK 17 for x64
- name: Set up JDK 21 for x64
uses: actions/setup-java@v3
with:
java-version: '17'
java-version: '21'
distribution: 'temurin'
architecture: x64
- name: Validate Gradle wrapper
Expand Down
Binary file modified gradle/wrapper/gradle-wrapper.jar
Binary file not shown.
5 changes: 3 additions & 2 deletions gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#Wed Sep 27 18:38:36 EDT 2023
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-8.0-bin.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-8.14-bin.zip
networkTimeout=10000
validateDistributionUrl=true
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
47 changes: 32 additions & 15 deletions gradlew

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

41 changes: 23 additions & 18 deletions gradlew.bat

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

54 changes: 54 additions & 0 deletions src/main/java/problemset/a3318/FindXSumOfAllKLongSubarrays.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package problemset.a3318;

import java.util.*;

public class FindXSumOfAllKLongSubarrays {

record Pair(int num, int count) implements Comparable<Pair> {
@Override
public int compareTo(Pair other) {
if (this.count != other.count)
return other.count - this.count;
return other.num - this.num;
}
}

public int[] findXSumOfAllKLongSubarrays(int[] nums, int k, int x) {
int n = nums.length;
int[] res = new int[nums.length - k + 1];

for (int i = 0; i < n - k + 1; i++) {
Map<Integer, Integer> count = new HashMap<>();

for (int j = i; j < i + k; j++)
count.put(nums[j], count.getOrDefault(nums[j], 0) + 1);

if (count.size() <= x) {
int sum = 0;
for (int j = i; j < i + k; j++)
sum += nums[j];
res[i] = sum;
} else {
res[i] = count.entrySet().stream()
.map(e -> new Pair(e.getKey(), e.getValue()))
.sorted()
.limit(x)
.mapToInt(p -> p.num * p.count)
.sum();
// List<Pair> pairs = new ArrayList<>();
// for (var entry : count.entrySet())
// pairs.add(new Pair(entry.getKey(), entry.getValue()));

// Collections.sort(pairs);

// int curSum = 0;
// for (int j = 0; j < x; j++) {
// Pair pair = pairs.get(j);
// curSum += pair.num * pair.count;
// }
// res[i] = curSum;
}
}
return res;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package problemset.a3318;

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

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

class FindXSumOfAllKLongSubarraysTest {

private FindXSumOfAllKLongSubarrays find;

private int[] nums1;
private int k1;
private int x1;
private int[] nums2;
private int k2;
private int x2;

@BeforeEach
void setUp() {
find = new FindXSumOfAllKLongSubarrays();
nums1 = new int[]{1,1,2,2,3,4,2,3};
k1 = 6;
x1 = 2;
nums2 = new int[]{3,8,7,8,7,5};
k2 = 2;
x2 = 2;
}

@Test
void testCaseOne_findXSumOfAllKLongSubarrays() {
int[] expected = new int[]{6,10,12};
int[] actual = find.findXSumOfAllKLongSubarrays(nums1, k1, x1);
assertArrayEquals(expected, actual);
}

@Test
void testCaseTwo_findXSumOfAllKLongSubarrays() {
int[] expected = new int[]{11,15,15,15,12};
int[] actual = find.findXSumOfAllKLongSubarrays(nums2, k2, x2);
assertArrayEquals(expected, actual);
}
}
Loading