1005.K次取反后最大化的数组和
题目链接
https://leetcode.cn/problems/maximize-sum-of-array-after-k-negations/
题目描述

思路
1、自己的想法
class Solution {
    public int largestSumAfterKNegations(int[] nums, int k) {
        sorted(nums);
        //遇到 <0 的 ,就将其 变为相反数,并且 k 自减
        for (int i = 0; i < nums.length; i++) {
            if(nums[i] < 0 && k>0){
                nums[i] *= -1;
                k--;
            }
        }
        //消耗 k 之后可能有剩余,如果 k 是奇数,就就将最小的数乘以 -1 ,是偶数,相当于不变
        if(k%2==1)  nums[nums.length-1] *= -1;
        //最后将数组相加起来
        int result = 0;
        for (int i = 0; i < nums.length; i++) {
            result += nums[i];
        }
        return result;
    }
    //按照绝对值大小进行排序
    public void sorted(int[] nums) {
        for (int k = 0; k < nums.length - 1; k++) {
            int maxIndex = k;
            for (int i = k + 1; i < nums.length; i++) {
                if (Math.abs(nums[i]) > Math.abs(nums[maxIndex])) {
                    maxIndex = i;
                }
            }
            if (maxIndex != k) {
                int temp = nums[maxIndex];
                nums[maxIndex] = nums[k];
                nums[k] = temp;
            }
        }
    }
}
2、答案的绝对值排序更简单
class Solution {
    public int largestSumAfterKNegations(int[] nums, int K) {
    	// 将数组按照绝对值大小从大到小排序,注意要按照绝对值的大小
	nums = IntStream.of(nums)
		     .boxed()
		     .sorted((o1, o2) -> Math.abs(o2) - Math.abs(o1))
		     .mapToInt(Integer::intValue).toArray();
	int len = nums.length;	    
	for (int i = 0; i < len; i++) {
	    //从前向后遍历,遇到负数将其变为正数,同时K--
	    if (nums[i] < 0 && K > 0) {
	    	nums[i] = -nums[i];
	    	K--;
	    }
	}
	// 如果K还大于0,那么反复转变数值最小的元素,将K用完
	if (K % 2 == 1) nums[len - 1] = -nums[len - 1];
	return Arrays.stream(nums).sum();
    }
}
134. 加油站
题目链接
https://leetcode.cn/problems/gas-station/description/
题目描述

思路
好难!!没看懂
class Solution {
    public int canCompleteCircuit(int[] gas, int[] cost) {
        int sum = 0;
        int min = 0;
        for (int i = 0; i < gas.length; i++) {
            sum += (gas[i] - cost[i]);
            min = Math.min(sum, min);
        }
        if (sum < 0) return -1;
        if (min >= 0) return 0;
        for (int i = gas.length - 1; i > 0; i--) {
            min += (gas[i] - cost[i]);
            if (min >= 0) return i;
        }
        return -1;
    }
}
// 解法2
class Solution {
    public int canCompleteCircuit(int[] gas, int[] cost) {
        int curSum = 0;
        int totalSum = 0;
        int index = 0;
        for (int i = 0; i < gas.length; i++) {
            curSum += gas[i] - cost[i];
            totalSum += gas[i] - cost[i];
            if (curSum < 0) {
                index = (i + 1) % gas.length ; 
                curSum = 0;
            }
        }
        if (totalSum < 0) return -1;
        return index;
    }
}
135. 分发糖果
题目链接
https://leetcode.cn/problems/candy/description/
题目描述

思路
class Solution {
    /**
         分两个阶段
         1、起点下标1 从左往右,只要 右边 比 左边 大,右边的糖果=左边 + 1
         2、起点下标 ratings.length - 2 从右往左, 只要左边 比 右边 大,此时 左边的糖果应该 取本身的糖果数(符合比它左边大) 和 右边糖果数 + 1 二者的最大值,这样才符合 它比它左边的大,也比它右边大
    */
    public int candy(int[] ratings) {
        int len = ratings.length;
        int[] candyVec = new int[len];
        candyVec[0] = 1;
        for (int i = 1; i < len; i++) {
            candyVec[i] = (ratings[i] > ratings[i - 1]) ? candyVec[i - 1] + 1 : 1;
        }
        for (int i = len - 2; i >= 0; i--) {
            if (ratings[i] > ratings[i + 1]) {
                candyVec[i] = Math.max(candyVec[i], candyVec[i + 1] + 1);
            }
        }
        int ans = 0;
        for (int num : candyVec) {
            ans += num;
        }
        return ans;
    }
}



















