2587. Rearrange Array to Maximize Prefix Score

problem

solution

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
class Solution {
public:
int maxScore(vector<int>& nums) {
sort(nums.begin(), nums.end(), greater<int>());
int count = (nums.front()>0)?1:0;
long long cur = nums[0];
for(int i=0;i<nums.size() ; ++i) {
if(i==0) continue;
cur += nums[i];
if(cur > 0) count++;
else break;
}
return count ;
}
};

analysis

  • time complexity O(nlogn)
  • space complexity O(1)