贪心算法之区间调度问题 :: labuladong的算法小抄 #1045
Replies: 23 comments 2 replies
-
提一个小点: Arrays.sort(intvs, new Comparator<int[]>() {
public int compare(int[] a, int[] b) {
return a[1] - b[1];
}
}); 最好改成后面的这个:
Arrays.sort(points, (o1,o2)->{
if(o1[1]>o2[1]) return 1;
if(o1[1]<o2[1]) return -1;
return 0;
}); 直接返回return a[1] - b[1];容易发生数据溢出; |
Beta Was this translation helpful? Give feedback.
-
笔试题,卧槽 |
Beta Was this translation helpful? Give feedback.
-
@xiaozhi007 你说的对,这个细节值得注意👍 |
Beta Was this translation helpful? Give feedback.
-
Arrays.sort(points, Comparator.comparingInt(t -> t[1])); 这样可以避免排序时return a[1] - b[1];溢出。 |
Beta Was this translation helpful? Give feedback.
-
可以按start排序吗? |
Beta Was this translation helpful? Give feedback.
-
|
Beta Was this translation helpful? Give feedback.
-
@Yggdrasill-7C9 动态规划可以认为是含有重叠子问题的暴力算法,本质还是要穷举所有解空间,而贪心的精髓在于不用穷举所有解空间,就能找到答案。所以准确说应该是,能够用贪心算法解决的问题,一定能用暴力穷举的方式求解。 当然,几乎所有计算机算法问题,都能用暴力穷举的方式求解。。。 |
Beta Was this translation helpful? Give feedback.
-
关于溢出问题,可使用以下解法 class Solution {
public int findMinArrowShots(int[][] points) {
Arrays.sort(points, (a, b) -> {
return Integer.compare(a[1], b[1]);
});
int count = 1;
int end = points[0][1];
for (int[] point : points) {
int start = point[0];
if (start > end) { // 找到不重叠区间(边界重叠也算重叠)
count++;
end = point[1];
}
}
return count;
}
} |
Beta Was this translation helpful? Give feedback.
-
三种排序方法的对比 // Arrays.sort(points,(a,b)->(a[1]-b[1]));// 可能出现溢出情况
// Arrays.sort(points,Comparator.comparingInt(a->a[1]));//升序排序 解决溢出方法2 效率较低 调用内置静态方法, 效率较低
Arrays.sort(points,(a,b)->{
if(a[1]>b[1]) return 1;
if(a[1]<b[1]) return -1;
return 0;//上述方法可能溢出 解决方法1
}); |
Beta Was this translation helpful? Give feedback.
-
sort的排序方法中,用a[1] - b[1]会出现溢出的问题 |
Beta Was this translation helpful? Give feedback.
-
6666 |
Beta Was this translation helpful? Give feedback.
-
Arrays.sort(points, (a, b)->(Integer.compare(a[1], b[1]))); |
Beta Was this translation helpful? Give feedback.
-
东哥,你好,请教一个关于区间调度问题的进阶题的解法,今天大疆的笔试题,没有想到好的方法。 |
Beta Was this translation helpful? Give feedback.
-
有没有相关把权重也当作数组的一个维度 排序呢 , 然后在之前的二维数组的基础上再扩展出一个维度呢 (只是普通猜测) |
Beta Was this translation helpful? Give feedback.
-
那按照你的思路的话,我们到底该怎么排序呢,首先按照权重排序吗?然后再根据开始时间或者结束时间?如果先按照权重排序的话,后面的时间就是无规律的了呀,就不能按照东哥上面讲解的方式解答了呀。 |
Beta Was this translation helpful? Give feedback.
-
@aristo-panhu 我估计要用 TreeMap + 动态规划来做,穷举所有可能的安排方法求最值。我记得 LeetCode 上好像有类似的题目,有空找出来发在这。 |
Beta Was this translation helpful? Give feedback.
-
@aristo-panhu 我思考了一下能不能在做贪心排序的同时用dp 穷举所有可能的组合取最大值? |
Beta Was this translation helpful? Give feedback.
-
打卡打卡。滴滴滴 贪心:局部最优》〉》〉》全局最优 区间尾部最小的应该先完成! |
Beta Was this translation helpful? Give feedback.
-
按照右边界排序,就要从左向右遍历,因为右边界越小越好,只要右边界越小,留给下一个区间的空间就越大,所以从左向右遍历,优先选右边界小的。 |
Beta Was this translation helpful? Give feedback.
-
不知道大家有没有疑问这和贪心有什么关系,因为是求最多能参加几个活动,找到活动最早结束的,这样就能赶紧参加下一个活动,就能参加更多活动,这还不够贪心吗? |
Beta Was this translation helpful? Give feedback.
-
435 class Solution {
public int eraseOverlapIntervals(int[][] intervals) {
int n = intervals.length;
// lambda expresion comparator
Arrays.sort(intervals, (a, b) -> Integer.compare(a[1], b[1]));
int end = intervals[0][1];
int count = 0;
// start from the 2nd interval
for (int i = 1; i < n; ++i) {
int start = intervals[i][0];
// if overlaps, count
if (start < end) {
count++;
} else {
// if no overlap, move forward
end = intervals[i][1];
}
}
return count;
}
} |
Beta Was this translation helpful? Give feedback.
-
1 |
Beta Was this translation helpful? Give feedback.
-
打卡,现在这个重叠区间问题改了,附上注释详细的Java代码 public int eraseOverlapIntervals(int[][] intervals) {
//根据每个区间的end进行一个升序排列,我们要保证后面的序列的start是大于等于这个区间的结尾才能说明这两个区间是没有交集的
Arrays.sort(intervals, (o1, o2) -> {
if (o1[1] > o2[1]) {
return 1;
}
if (o1[1] < o2[1]) {
return -1;
}
return 0;
});
//count来记录没有交集的区间,最后的结果就是intervals元素的个数减去这个没有交集的区间个数就好了
int count = 1;
//开始的end肯定是end最小的那个
int start_end = intervals[0][1];
for (int[] interval : intervals) {
int start = interval[0];
if (start >= start_end) {
//如果后面的区间的start是比此时的start_end大或者等于的时候,说明两个区间也是没有交集的
count++;
//更新start_end的大小
start_end = interval[1];
} else {
//如果两个区间有交集的话,什么都不做
}
}
return intervals.length - count;
} |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
文章链接点这里:贪心算法之区间调度问题
评论礼仪 见这里,违者直接拉黑。
Beta Was this translation helpful? Give feedback.
All reactions