标签:turn 顺序 auto temp sequence cto class min sort
贪心算法
class Solution {
public:
vector<int> minSubsequence(vector<int>& nums) {
sort(nums.begin(), nums.end(), greater<int>()); // 内置从大到小排序,less<int>()从小到大排序
int sum = 0;
for (auto mem : nums) {
sum += mem;
}
int temp = 0;
for (int i = 0; i < nums.size(); i++) {
temp += nums[i];
if (temp > sum - temp) return vector<int>(nums.begin(), nums.begin() + i + 1);
}
return nums;
}
};
标签:turn 顺序 auto temp sequence cto class min sort
原文地址:https://www.cnblogs.com/lihanwen/p/12833414.html