class Solution {
public int largestSumAfterKNegations(int[] nums, int k) {
//绝对值从大到小排序
nums = IntStream.of(nums)
.boxed()
.sorted((o1, o2) -> Math.abs(o2) - Math.abs(o1))//o2-o1
.mapToInt(Integer :: intValue).toArray();
int len = nums.length;
for(int i = 0; i<len ; i++){
if(nums[i] < 0 && k > 0){
nums[i] = -nums[i];
k--;
}
}
if(k % 2 == 1){
nums[len - 1] = -nums[len - 1];
}
return Arrays.stream(nums).sum();
}
}
class Solution {
public int canCompleteCircuit(int[] gas, int[] cost) {
int totalsum = 0;
int cursum = 0;
int start = 0;
for(int i = 0 ;i < gas.length ;i++){
cursum += gas[i] - cost[i];
totalsum += gas[i] - cost[i];
if(cursum < 0 ){
cursum = 0;
start = i + 1 ;
}
}
if(totalsum < 0) return -1;
return start;
}
}
class Solution {
public int candy(int[] ratings) {
int len = ratings.length;
int[] candy = new int[len];
candy[0] = 1;
for(int i = 0; i<len ; i++){
if(i > 0 && ratings[i] > ratings[i-1]){
candy[i] = candy[i-1] + 1;
}else{
candy[i] = 1;
}
}
for(int i = len -1 ; i >= 0 ; i--){
if(i < len - 1 && ratings[i] > ratings[i+1]){
candy[i] = Math.max(candy[i+1] + 1, candy[i]);
}
}
return Arrays.stream(candy).sum();
}
}
class Solution {
public boolean lemonadeChange(int[] bills) {
int five = 0;
int ten = 0;
for(int i = 0 ; i < bills.length ; i++){
if(bills[i] == 5){
five++;
}else if(bills[i] == 10){
ten++;
five--;
}else if(bills[i] == 20){
if(ten > 0){
ten--;
five--;
}else{
five-= 3;
}
}
}
if(five >= 0 && ten >= 0){
return true ;
}else{
return false;
}
}
}