Skip to content

Commit 4e58e14

Browse files
authored
Create minimum-subsequence-in-non-increasing-order.cpp
1 parent ad875ab commit 4e58e14

File tree

1 file changed

+19
-0
lines changed

1 file changed

+19
-0
lines changed
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// Time: O(nlogn)
2+
// Space: O(1)
3+
4+
class Solution {
5+
public:
6+
vector<int> minSubsequence(vector<int>& nums) {
7+
int total = accumulate(cbegin(nums), cend(nums), 0), curr = 0;
8+
sort(begin(nums), end(nums), greater<int>());
9+
int result = 0;
10+
for (int i = 0; i < nums.size(); ++i) {
11+
curr += nums[i];
12+
if (curr > total - curr) {
13+
result = i + 1;
14+
break;
15+
}
16+
}
17+
return vector<int>(cbegin(nums), cbegin(nums) + result);
18+
}
19+
};

0 commit comments

Comments
 (0)