Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 9d40d44

Browse files
committedApr 13, 2025
Add solution #1403
1 parent 7a97f9e commit 9d40d44

File tree

2 files changed

+37
-1
lines changed

2 files changed

+37
-1
lines changed
 

‎README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# 1,282 LeetCode solutions in JavaScript
1+
# 1,283 LeetCode solutions in JavaScript
22

33
[https://leetcodejavascript.com](https://leetcodejavascript.com)
44

@@ -1071,6 +1071,7 @@
10711071
1400|[Construct K Palindrome Strings](./solutions/1400-construct-k-palindrome-strings.js)|Medium|
10721072
1401|[Circle and Rectangle Overlapping](./solutions/1401-circle-and-rectangle-overlapping.js)|Medium|
10731073
1402|[Reducing Dishes](./solutions/1402-reducing-dishes.js)|Hard|
1074+
1403|[Minimum Subsequence in Non-Increasing Order](./solutions/1403-minimum-subsequence-in-non-increasing-order.js)|Easy|
10741075
1408|[String Matching in an Array](./solutions/1408-string-matching-in-an-array.js)|Easy|
10751076
1410|[HTML Entity Parser](./solutions/1410-html-entity-parser.js)|Medium|
10761077
1415|[The k-th Lexicographical String of All Happy Strings of Length n](./solutions/1415-the-k-th-lexicographical-string-of-all-happy-strings-of-length-n.js)|Medium|
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/**
2+
* 1403. Minimum Subsequence in Non-Increasing Order
3+
* https://leetcode.com/problems/minimum-subsequence-in-non-increasing-order/
4+
* Difficulty: Easy
5+
*
6+
* Given the array nums, obtain a subsequence of the array whose sum of elements is strictly
7+
* greater than the sum of the non included elements in such subsequence.
8+
*
9+
* If there are multiple solutions, return the subsequence with minimum size and if there still
10+
* exist multiple solutions, return the subsequence with the maximum total sum of all its elements.
11+
* A subsequence of an array can be obtained by erasing some (possibly zero) elements from the
12+
* array.
13+
*
14+
* Note that the solution with the given constraints is guaranteed to be unique. Also return the
15+
* answer sorted in non-increasing order.
16+
*/
17+
18+
/**
19+
* @param {number[]} nums
20+
* @return {number[]}
21+
*/
22+
var minSubsequence = function(nums) {
23+
const sorted = nums.sort((a, b) => b - a);
24+
const totalSum = sorted.reduce((sum, num) => sum + num, 0);
25+
const result = [];
26+
let currentSum = 0;
27+
28+
for (const num of sorted) {
29+
currentSum += num;
30+
result.push(num);
31+
if (currentSum > totalSum - currentSum) break;
32+
}
33+
34+
return result;
35+
};

0 commit comments

Comments
 (0)
Please sign in to comment.