Skip to content

Commit bf796be

Browse files
add 1403
1 parent c0e32e4 commit bf796be

File tree

3 files changed

+90
-0
lines changed

3 files changed

+90
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ _If you like this project, please leave me a star._ ★
88

99
| # | Title | Solutions | Video | Difficulty | Tag
1010
|-----|----------------|---------------|--------|-------------|-------------
11+
|1403|[Minimum Subsequence in Non-Increasing Order](https://leetcode.com/problems/minimum-subsequence-in-non-increasing-order/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1403.java) | |Easy|Greedy, Sort|
1112
|1401|[Circle and Rectangle Overlapping](https://leetcode.com/problems/circle-and-rectangle-overlapping/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1401.java) | |Medium|Geometry|
1213
|1400|[Construct K Palindrome Strings](https://leetcode.com/problems/construct-k-palindrome-strings/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1400.java) | |Medium|Greedy|
1314
|1399|[Count Largest Group](https://leetcode.com/problems/count-largest-group/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1399.java) | |Easy|Array|
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
package com.fishercoder.solutions;
2+
3+
import java.util.ArrayList;
4+
import java.util.Arrays;
5+
import java.util.Collections;
6+
import java.util.List;
7+
import java.util.stream.Collectors;
8+
9+
/**
10+
* 1403. Minimum Subsequence in Non-Increasing Order
11+
*
12+
* Given the array nums, obtain a subsequence of the array whose sum of elements is strictly greater than the sum of the non included elements in such subsequence.
13+
* If there are multiple solutions, return the subsequence with minimum size and if there still exist multiple solutions,
14+
* return the subsequence with the maximum total sum of all its elements. A subsequence of an array can be obtained by erasing some (possibly zero) elements from the array.
15+
* Note that the solution with the given constraints is guaranteed to be unique. Also return the answer sorted in non-increasing order.
16+
*
17+
* Example 1:
18+
* Input: nums = [4,3,10,9,8]
19+
* Output: [10,9]
20+
* Explanation: The subsequences [10,9] and [10,8] are minimal such that the sum of their elements is strictly greater than the sum of elements not included, however, the subsequence [10,9] has the maximum total sum of its elements.
21+
*
22+
* Example 2:
23+
* Input: nums = [4,4,7,6,7]
24+
* Output: [7,7,6]
25+
* Explanation: The subsequence [7,7] has the sum of its elements equal to 14 which is not strictly greater than the sum of elements not included (14 = 4 + 4 + 6). Therefore, the subsequence [7,6,7] is the minimal satisfying the conditions. Note the subsequence has to returned in non-decreasing order.
26+
*
27+
* Example 3:
28+
* Input: nums = [6]
29+
* Output: [6]
30+
*
31+
* Constraints:
32+
* 1 <= nums.length <= 500
33+
* 1 <= nums[i] <= 100
34+
* */
35+
public class _1403 {
36+
public static class Solution1 {
37+
public List<Integer> minSubsequence(int[] nums) {
38+
List<Integer> list = Arrays.stream(nums)
39+
.boxed()
40+
.sorted(Collections.reverseOrder())
41+
.collect(Collectors.toCollection(() -> new ArrayList<>(nums.length)));
42+
int sum = list.stream().mapToInt(num -> num).sum();
43+
int minSum = 0;
44+
List<Integer> result = new ArrayList<>();
45+
for (int i = 0; i < nums.length; i++) {
46+
if (minSum > (sum - minSum)) {
47+
return result;
48+
}
49+
minSum += list.get(i);
50+
result.add(list.get(i));
51+
}
52+
return result;
53+
}
54+
}
55+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package com.fishercoder;
2+
3+
import com.fishercoder.solutions._1403;
4+
import org.junit.BeforeClass;
5+
import org.junit.Test;
6+
7+
import java.util.Arrays;
8+
9+
import static org.junit.Assert.assertEquals;
10+
11+
public class _1403Test {
12+
private static _1403.Solution1 solution1;
13+
14+
@BeforeClass
15+
public static void setup() {
16+
solution1 = new _1403.Solution1();
17+
}
18+
19+
@Test
20+
public void test1() {
21+
assertEquals(Arrays.asList(10, 9), solution1.minSubsequence(new int[]{4, 3, 10, 9, 8}));
22+
}
23+
24+
@Test
25+
public void test2() {
26+
assertEquals(Arrays.asList(7, 7, 6), solution1.minSubsequence(new int[]{4, 4, 7, 6, 7}));
27+
}
28+
29+
@Test
30+
public void test3() {
31+
assertEquals(Arrays.asList(6), solution1.minSubsequence(new int[]{6}));
32+
}
33+
34+
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy