Skip to content

Commit 918f4f6

Browse files
authored
Added task 2386.
1 parent 3a2a7d0 commit 918f4f6

File tree

4 files changed

+115
-0
lines changed

4 files changed

+115
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1848,6 +1848,7 @@ implementation 'com.github.javadev:leetcode-in-java:1.12'
18481848

18491849
| # | Title | Difficulty | Tag | Time, ms | Time, %
18501850
|------|----------------|-------------|-------------|----------|---------
1851+
| 2386 |[Find the K-Sum of an Array](src/main/java/g2301_2400/s2386_find_the_k_sum_of_an_array/Solution.java)| Hard | Array, Sorting, Heap_(Priority_Queue) | 75 | 100.00
18511852
| 2385 |[Amount of Time for Binary Tree to Be Infected](src/main/java/g2301_2400/s2385_amount_of_time_for_binary_tree_to_be_infected/Solution.java)| Medium | Tree, Binary_Tree, Depth_First_Search, Breadth_First_Search | 20 | 100.00
18521853
| 2384 |[Largest Palindromic Number](src/main/java/g2301_2400/s2384_largest_palindromic_number/Solution.java)| Medium | String, Hash_Table, Greedy | 26 | 100.00
18531854
| 2383 |[Minimum Hours of Training to Win a Competition](src/main/java/g2301_2400/s2383_minimum_hours_of_training_to_win_a_competition/Solution.java)| Easy | Array, Greedy | 0 | 100.00
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
package g2301_2400.s2386_find_the_k_sum_of_an_array;
2+
3+
// #Hard #Array #Sorting #Heap_(Priority_Queue)
4+
// #2022_08_25_Time_75_ms_(100.00%)_Space_86.8_MB_(33.33%)
5+
6+
import java.util.Arrays;
7+
import java.util.PriorityQueue;
8+
9+
public class Solution {
10+
public long kSum(int[] nums, int k) {
11+
long sum = 0L;
12+
for (int i = 0; i < nums.length; i++) {
13+
if (nums[i] > 0) {
14+
sum += nums[i];
15+
} else {
16+
nums[i] = -nums[i];
17+
}
18+
}
19+
Arrays.sort(nums);
20+
PriorityQueue<Pair<Long, Integer>> pq =
21+
new PriorityQueue<>((a, b) -> Long.compare(b.getKey(), a.getKey()));
22+
pq.offer(new Pair<>(sum, 0));
23+
while (k-- > 1) {
24+
Pair<Long, Integer> top = pq.poll();
25+
if (top != null) {
26+
long s = top.getKey();
27+
int i = top.getValue();
28+
if (i < nums.length) {
29+
pq.offer(new Pair<>(s - nums[i], i + 1));
30+
if (i > 0) {
31+
pq.offer(new Pair<>(s - nums[i] + nums[i - 1], i + 1));
32+
}
33+
}
34+
}
35+
}
36+
return pq.peek().getKey();
37+
}
38+
39+
private static class Pair<K, V> {
40+
K key;
41+
V value;
42+
43+
public Pair(K key, V value) {
44+
this.key = key;
45+
this.value = value;
46+
}
47+
48+
public K getKey() {
49+
return key;
50+
}
51+
52+
public V getValue() {
53+
return value;
54+
}
55+
}
56+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
2386\. Find the K-Sum of an Array
2+
3+
Hard
4+
5+
You are given an integer array `nums` and a **positive** integer `k`. You can choose any **subsequence** of the array and sum all of its elements together.
6+
7+
We define the **K-Sum** of the array as the <code>k<sup>th</sup></code> **largest** subsequence sum that can be obtained (**not** necessarily distinct).
8+
9+
Return _the K-Sum of the array_.
10+
11+
A **subsequence** is an array that can be derived from another array by deleting some or no elements without changing the order of the remaining elements.
12+
13+
**Note** that the empty subsequence is considered to have a sum of `0`.
14+
15+
**Example 1:**
16+
17+
**Input:** nums = [2,4,-2], k = 5
18+
19+
**Output:** 2
20+
21+
**Explanation:** All the possible subsequence sums that we can obtain are the following sorted in decreasing order:
22+
23+
- 6, 4, 4, 2, <ins>2</ins>, 0, 0, -2.
24+
25+
The 5-Sum of the array is 2.
26+
27+
**Example 2:**
28+
29+
**Input:** nums = [1,-2,3,4,-10,12], k = 16
30+
31+
**Output:** 10
32+
33+
**Explanation:** The 16-Sum of the array is 10.
34+
35+
**Constraints:**
36+
37+
* `n == nums.length`
38+
* <code>1 <= n <= 10<sup>5</sup></code>
39+
* <code>-10<sup>9</sup> <= nums[i] <= 10<sup>9</sup></code>
40+
* <code>1 <= k <= min(2000, 2<sup>n</sup>)</code>
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package g2301_2400.s2386_find_the_k_sum_of_an_array;
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 kSum() {
11+
assertThat(new Solution().kSum(new int[] {2, 4, -2}, 5), equalTo(2L));
12+
}
13+
14+
@Test
15+
void kSum2() {
16+
assertThat(new Solution().kSum(new int[] {1, -2, 3, 4, -10, 12}, 16), equalTo(10L));
17+
}
18+
}

0 commit comments

Comments
 (0)