class Solution {
static bool cmp(int a,int b)
{
return abs(a)>abs(b);
}
public:
int largestSumAfterKNegations(vector<int>& nums, int k) {
sort(nums.begin(),nums.end(),cmp);
for(int i=0;i<nums.size();i++)
{
if(nums[i]<0&&k>0)
{
nums[i]*=-1;
k--;
}
}
if(k%2==1) nums[nums.size()-1]*=-1;
int result=0;
for(int a:nums) result+=a;
return result;
}
};
class Solution {
public:
int canCompleteCircuit(vector<int>& gas, vector<int>& cost) {
int curSum=0;
int totalSum=0;
int start=0;
for(int i=0;i<gas.size();i++)
{
curSum+=gas[i]-cost[i];
totalSum+=gas[i]-cost[i];
if(curSum<0)
{
start=i+1;
curSum=0;
}
}
if(totalSum<0) return -1;
return start;
}
};
class Solution {
public:
int candy(vector<int>& ratings) {
vector<int> candy(ratings.size(),1);
for(int i=1;i<ratings.size();i++)
{
if(ratings[i]>ratings[i-1]) candy[i]=candy[i-1]+1;
}
for(int i=ratings.size()-2;i>=0;i--)
{
if(ratings[i]>ratings[i+1]) candy[i]=max(candy[i],candy[i+1]+1);
}
int result=0;
for(int i=0;i<candy.size();i++)
{
result+=candy[i];
}
return result;
}
};