feat(redo): 添加LeetCode问题134、135、860、1005的解决方案新增四个Java类,分别对应LeetCode问题134、135、860、1005的解决方案。
这些问题涉及循环数组、糖果分配、零钱兑换和数组操作等不同主题。 每个类包含一个或多个解决特定问题的算法实现,以及相关的测试方法。 - LeetCode134:实现计算加油站问题的解决方案。 - LeetCode135:实现计算糖果分配问题的解决方案。 - LeetCode860:实现柠檬水找零问题的解决方案。 - LeetCode1005:实现数组操作以最大化和的解决方案。 接下来的步骤包括将这些解决方案集成到主项目中,并通过单元测试确保其正确性。
This commit is contained in:
parent
a5ac260b63
commit
8b775ad439
@ -0,0 +1,54 @@
|
||||
package cn.whaifree.redo.redo_all_240721;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* @version 1.0
|
||||
* @Author whai文海
|
||||
* @Date 2024/8/7 21:23
|
||||
* @注释
|
||||
*/
|
||||
public class LeetCode406 {
|
||||
|
||||
@Test
|
||||
public void test()
|
||||
{
|
||||
int[][] people = {{7,0},{4,4},{7,1},{5,0},{6,1},{5,2}};
|
||||
for (int[] ints : new Solution().reconstructQueue(people)) {
|
||||
System.out.println(Arrays.toString(ints));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
class Solution {
|
||||
/**
|
||||
* [[7,0],[4,4],[7,1],[5,0],[6,1],[5,2]]
|
||||
* @param people
|
||||
* @return
|
||||
*/
|
||||
public int[][] reconstructQueue(int[][] people) {
|
||||
List<int[]> list = new ArrayList<>();
|
||||
Arrays.sort(people, (o1, o2) -> {
|
||||
if (o1[0] == o2[0]) {
|
||||
return o1[1] - o2[1];
|
||||
} else {
|
||||
return o2[0] - o1[0];
|
||||
}
|
||||
});
|
||||
|
||||
/**
|
||||
* 遍历每一个,如果身高
|
||||
*/
|
||||
for (int i = 0; i < people.length; i++) {
|
||||
list.add(people[i][1], people[i]);
|
||||
}
|
||||
|
||||
return list.toArray(new int[people.length][2]);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
122
src/main/java/cn/whaifree/redo/redo_all_240721/LeetCode435.java
Normal file
122
src/main/java/cn/whaifree/redo/redo_all_240721/LeetCode435.java
Normal file
@ -0,0 +1,122 @@
|
||||
package cn.whaifree.redo.redo_all_240721;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.Comparator;
|
||||
|
||||
/**
|
||||
* @version 1.0
|
||||
* @Author whai文海
|
||||
* @Date 2024/8/7 21:41
|
||||
* @注释
|
||||
*/
|
||||
public class LeetCode435 {
|
||||
|
||||
@Test
|
||||
public void test()
|
||||
{
|
||||
So3 solution = new So3();
|
||||
// [0,2],[1,3],[2,4],[3,5],[4,6]
|
||||
System.out.println(solution.eraseOverlapIntervals(new int[][]{{0, 2}, {1, 3}, {2, 4}, {3, 5}, {4, 6}}));
|
||||
System.out.println(solution.eraseOverlapIntervals(new int[][]{{1, 2}, {1, 2}, {1, 2}}));
|
||||
System.out.println(solution.eraseOverlapIntervals(new int[][]{{1, 2}, {2, 3}}));
|
||||
}
|
||||
|
||||
class Solution {
|
||||
public int eraseOverlapIntervals(int[][] intervals) {
|
||||
Arrays.sort(intervals, new Comparator<int[]>() {
|
||||
@Override
|
||||
public int compare(int[] o1, int[] o2) {
|
||||
if (o1[0] == o2[0]) {
|
||||
return o1[1] - o2[1];
|
||||
}
|
||||
return o1[0] - o2[0];
|
||||
}
|
||||
});
|
||||
|
||||
// int start = intervals[0][0];
|
||||
int end = intervals[0][1];
|
||||
int res = 0;
|
||||
for (int i = 1; i < intervals.length; i++) {
|
||||
int[] next = intervals[i];
|
||||
/**
|
||||
* | |
|
||||
* | |
|
||||
*
|
||||
* | |
|
||||
* | |
|
||||
*
|
||||
* | |
|
||||
* | |
|
||||
*
|
||||
*/
|
||||
int nextStart = next[0];
|
||||
int nextEnd = next[1];
|
||||
if (end <= nextStart) {
|
||||
// start = nextStart;
|
||||
end = nextEnd;
|
||||
}else if (nextEnd <= end) {
|
||||
res++;
|
||||
end = nextEnd;
|
||||
} else {
|
||||
res++;
|
||||
}
|
||||
}
|
||||
return res;
|
||||
}
|
||||
}
|
||||
|
||||
class Solution2 {
|
||||
public int eraseOverlapIntervals(int[][] intervals) {
|
||||
Arrays.sort(intervals, new Comparator<int[]>() {
|
||||
@Override
|
||||
public int compare(int[] o1, int[] o2) {
|
||||
return o1[1] - o2[1];
|
||||
}
|
||||
});
|
||||
int end = intervals[0][1];
|
||||
int can = 1;
|
||||
for (int i = 1; i < intervals.length; i++) {
|
||||
int[] next = intervals[i];
|
||||
int nextStart = next[0];
|
||||
int nextEnd = next[1];
|
||||
// 如果上次活动结束比下次开始时间早,能够多参加一次
|
||||
if (nextStart >= end) {
|
||||
can++;
|
||||
end = nextEnd;
|
||||
}
|
||||
}
|
||||
|
||||
return intervals.length - can;
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
class So3{
|
||||
public int eraseOverlapIntervals(int[][] intervals) {
|
||||
// 最多能参加几次活动
|
||||
Arrays.sort(intervals, new Comparator<int[]>() {
|
||||
@Override
|
||||
public int compare(int[] o1, int[] o2) {
|
||||
return o1[0] - o2[0];
|
||||
}
|
||||
});
|
||||
|
||||
int participation = 1;
|
||||
int end = intervals[0][1];
|
||||
for (int i = 1; i < intervals.length; i++) {
|
||||
if (intervals[i][0] < end) {
|
||||
// 后一个活动的开始时间早于头一个活动开始时间,这个活动不能参加
|
||||
end = Math.min(end, intervals[i][1]);
|
||||
} else {
|
||||
participation++;
|
||||
end = intervals[i][1];
|
||||
}
|
||||
}
|
||||
|
||||
return intervals.length - participation;
|
||||
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user