feat(redo): 添加LeetCode问题134、135、860、1005的解决方案新增四个Java类,分别对应LeetCode问题134、135、860、1005的解决方案。

这些问题涉及循环数组、糖果分配、零钱兑换和数组操作等不同主题。
每个类包含一个或多个解决特定问题的算法实现,以及相关的测试方法。

- LeetCode134:实现计算加油站问题的解决方案。
- LeetCode135:实现计算糖果分配问题的解决方案。
- LeetCode860:实现柠檬水找零问题的解决方案。
- LeetCode1005:实现数组操作以最大化和的解决方案。

接下来的步骤包括将这些解决方案集成到主项目中,并通过单元测试确保其正确性。
This commit is contained in:
whaifree 2024-08-06 23:13:55 +08:00
parent 536890ab62
commit 967dc09bec
4 changed files with 235 additions and 0 deletions

View File

@ -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<Integer> queue = new PriorityQueue<>(new Comparator<Integer>() {
@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<Integer> 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;
}
}
}
}

View File

@ -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;
}
}
}

View File

@ -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();
}
}
}

View File

@ -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;
}
}
}