题目来源:https://leetcode.cn/problems/rearrange-array-to-maximize-prefix-score/
C++题解:先将数组从大到小排列,由最大到最小一个一个添加计算总和,当总和小于等于0时,计算停止,算出总和中的元素个数,即为最大前缀分数
class Solution {
public:
int maxScore(vector<int>& nums) {
sort(nums.begin(),nums.end());
int len = nums.size();
long long sum = 0, num = 0;
for(int ii = len-1; ii >= 0; ii--){
sum = sum + nums[ii];
if(sum <= 0) break;
else num += 1;
}
return(num);
}
};