File tree 2 files changed +37
-1
lines changed
2 files changed +37
-1
lines changed Original file line number Diff line number Diff line change 1
- # 1,282 LeetCode solutions in JavaScript
1
+ # 1,283 LeetCode solutions in JavaScript
2
2
3
3
[ https://leetcodejavascript.com ] ( https://leetcodejavascript.com )
4
4
1071
1071
1400|[ Construct K Palindrome Strings] ( ./solutions/1400-construct-k-palindrome-strings.js ) |Medium|
1072
1072
1401|[ Circle and Rectangle Overlapping] ( ./solutions/1401-circle-and-rectangle-overlapping.js ) |Medium|
1073
1073
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|
1074
1075
1408|[ String Matching in an Array] ( ./solutions/1408-string-matching-in-an-array.js ) |Easy|
1075
1076
1410|[ HTML Entity Parser] ( ./solutions/1410-html-entity-parser.js ) |Medium|
1076
1077
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|
Original file line number Diff line number Diff line change
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
+ } ;
You can’t perform that action at this time.
0 commit comments