diff --git a/src/main/java/cn/whaifree/redo/redo_all_240721/LeetCode1005.java b/src/main/java/cn/whaifree/redo/redo_all_240721/LeetCode1005.java new file mode 100644 index 0000000..ad5d5ec --- /dev/null +++ b/src/main/java/cn/whaifree/redo/redo_all_240721/LeetCode1005.java @@ -0,0 +1,108 @@ +package cn.whaifree.redo.redo_all_240721; + +import org.junit.Test; + +import java.util.*; + +/** + * @version 1.0 + * @Author whai文海 + * @Date 2024/8/5 23:37 + * @注释 + */ +public class LeetCode1005 { + @Test + public void test() + { + int[] nums = new int[]{-8,3,-5,-3,-5,-2}; + Solution solution = new Solution(); + System.out.println(solution.largestSumAfterKNegations(nums, 6)); + + } + class Solution { + /** + * -4 3 2 1 + * // 如果绝对值最大的是负数-1 + * // 把所有负数变为正 + * // 如果k还有 + * // 偶数,不改了 + * // 奇数,找到最小的变为负数 + * @param nums + * @param k + * @return + */ + public int largestSumAfterKNegations(int[] nums, int k) { + + PriorityQueue queue = new PriorityQueue<>(new Comparator() { + @Override + public int compare(Integer o1, Integer o2) { + int absCompare = Math.abs(o2) - Math.abs(o1); + if (absCompare != 0) { + return absCompare; + } else { + // 如果绝对值相同,负数排在正数前面 + return o1 - o2; + } + } + }); + + for (int num : nums) { + queue.add(num); + } + + List list = new ArrayList<>(); + for (int i = 0; i < nums.length; i++) { + list.add(queue.poll()); + } + + int sum = 0; + for (int i = 0 ; i < list.size() ; i++) { + Integer poll = list.get(i); + if (poll < 0 && k > 0) { + sum += -poll; + k--; + }else { + sum += poll; + } + } + if (k % 2 == 0) { + return sum; + }else { + Integer poll = Math.abs(list.get(list.size() - 1)); + return sum - poll - poll; + } + + } + } + + class Solution1 { + /** + * -4 -7 3 2 1 + * -7 -4 1 2 3 + * + * @param nums + * @param k + * @return + */ + public int largestSumAfterKNegations(int[] nums, int k) { + Arrays.sort(nums); + int res = 0; + int minAbs = Integer.MAX_VALUE; + for (int i = 0; i < nums.length; i++) { + if (nums[i] < 0 && k > 0) { + nums[i] = -nums[i]; + k--; + } + minAbs = Math.min(minAbs, nums[i]); // 已经全部翻转 + res += nums[i]; + } + + if (k % 2 == 0) { + return res; + }else { + return res - 2 * minAbs; + } + } + } + +} diff --git a/src/main/java/cn/whaifree/redo/redo_all_240721/LeetCode134.java b/src/main/java/cn/whaifree/redo/redo_all_240721/LeetCode134.java new file mode 100644 index 0000000..d24659c --- /dev/null +++ b/src/main/java/cn/whaifree/redo/redo_all_240721/LeetCode134.java @@ -0,0 +1,47 @@ +package cn.whaifree.redo.redo_all_240721; + +import org.junit.Test; + +import java.util.Arrays; + +/** + * @version 1.0 + * @Author whai文海 + * @Date 2024/8/5 23:08 + * @注释 + */ +public class LeetCode134 { + + @Test + public void test() + { + int[] gas = {1,2,3,4,5}; + int[] cost = {3,4,5,1,2}; + System.out.println(new Solution().canCompleteCircuit(gas, cost)); + } + + class Solution { + public int canCompleteCircuit(int[] gas, int[] cost) { + + // 理论上,如果汽油够,一定能绕圈 + int sumGas = Arrays.stream(gas).parallel().sum(); + int sumCost = Arrays.stream(cost).parallel().sum(); + if (sumGas < sumCost) { + return -1; + } + + int nowHave = 0; + int nowStart = 0; + for (int i = 0; i < gas.length; i++) { + nowHave += gas[i]; + if (nowHave < cost[i]) { + nowHave = 0; + nowStart = (i + 1) % gas.length; + } else { + nowHave -= cost[i]; + } + } + return nowStart; + } + } +} diff --git a/src/main/java/cn/whaifree/redo/redo_all_240721/LeetCode135.java b/src/main/java/cn/whaifree/redo/redo_all_240721/LeetCode135.java new file mode 100644 index 0000000..e6c2184 --- /dev/null +++ b/src/main/java/cn/whaifree/redo/redo_all_240721/LeetCode135.java @@ -0,0 +1,41 @@ +package cn.whaifree.redo.redo_all_240721; + +import org.junit.Test; + +import java.util.Arrays; + +/** + * @version 1.0 + * @Author whai文海 + * @Date 2024/8/6 22:56 + * @注释 + */ +public class LeetCode135 { + + @Test + public void test() + { + System.out.println(new Solution().candy(new int[]{1,2,2,3,1})); + } + class Solution { + public int candy(int[] ratings) { + + int[] candy = new int[ratings.length]; + Arrays.fill(candy, 1); + for (int i = 0; i < ratings.length - 1; i++) { + if (ratings[i] < ratings[i + 1]) { + candy[i + 1] = candy[i] + 1; + } + } + + for (int i = ratings.length - 1; i > 0; i--) { + if (ratings[i - 1] > ratings[i]) { + candy[i - 1] = Math.max(candy[i - 1], candy[i] + 1); + // candy[i-1]为原来从左到右遍历的值,需要记录保存 + } + } + return Arrays.stream(candy).sum(); + } + } + +} diff --git a/src/main/java/cn/whaifree/redo/redo_all_240721/LeetCode860.java b/src/main/java/cn/whaifree/redo/redo_all_240721/LeetCode860.java new file mode 100644 index 0000000..d3b82f0 --- /dev/null +++ b/src/main/java/cn/whaifree/redo/redo_all_240721/LeetCode860.java @@ -0,0 +1,39 @@ +package cn.whaifree.redo.redo_all_240721; + +/** + * @version 1.0 + * @Author whai文海 + * @Date 2024/8/6 22:51 + * @注释 + */ +public class LeetCode860 { + + class Solution { + public boolean lemonadeChange(int[] bills) { + int iHave5 = 0; + int iHave10 = 0; + for (int bill : bills) { + if (bill == 5) { + iHave5++; + } else if (bill == 10) { + iHave5--; + iHave10++; + } else if (bill == 20) { + + if (iHave10 > 0) { + iHave10--; + iHave5--; + }else { + iHave5 -= 3; + } + + } + if (iHave5 < 0 || iHave10 < 0) { + return false; + } + + } + return true; + } + } +}