Interval scheduling problem in greedy algorithm

1. Problem overview

Classical problem, given a closed interval of the form [begin, end], design an algorithm to find at most several disjoint intervals among these intervals.

For example, these four intervals, [1,2], [2,3], [3,4], [1,3], have at most three disjoint each other, and the intersection of the interval boundaries does not count as overlapping. Then this problem can be solved with greed,

This problem seems to be solved with several greedy strategies, but they are all problematic. For example, we can sort by start time, but there may be some intervals that start very early but are very long, making us mistakenly missed some short intervals.

If you choose the interval with the smallest interval, there may be a lot of intersections between the intervals, leading to wrong answers.

So let's change our thinking. Assuming that a person has many activities a day, then go to those that end earlier, and the person will have more free time in a day, and the more activities he can participate in.

贪心策略就是: 对区间的右边界排序,选取结束时间最早的,然后删除与该区间重叠的区间,重复上述步骤。

It is not difficult to find that after sorting, if it overlaps with the interval, the begin must be less than the end of the selected interval.

class Solution {
    
    
public:
	int get_solution(vector<vector<int>> & intervals)
	{
    
    
		if (intervals.size() == 0)return 0;
		sort(intervals.begin(), intervals.end(), [](const auto& u, const auto& v)
			{
    
    
				return u[1] < v[1];
			}); 
		int right = intervals[0][1];//初始右边界
		int res = 1;
		int size = intervals.size();
		for (int i = 1; i < size; i++)
		{
    
    
			if (intervals[i][0] >= right)
			{
    
    //左边界大于右边界
				right = intervals[i][1];
				res++;
			}
		}
		return res;
	}
};

Two, sample questions

leetcode435. No overlapping interval

Subject address

Given a set of intervals, find the minimum number of intervals that need to be removed so that the remaining intervals do not overlap each other.

It's very simple. We can find the most disjoint intervals and subtract them.

class Solution {
    
    
public:
	int eraseOverlapIntervals(vector<vector<int>>& intervals) {
    
    
		if (!intervals.size())return 0;
		sort(intervals.begin(), intervals.end(), [](const auto& u, const auto& v)
			{
    
    
				return u[1] < v[1];
			});
		int right = intervals[0][1];
		int res = 1;
		for (int i=1;i<intervals.size();i++)
		{
    
    
			if (intervals[i][0] >= right)
			{
    
    
				res++;
				right = intervals[i][1];
			}
		}
		return intervals.size()-res;
	}
};

leetcode452. Use the least number of arrows to detonate the balloon

Topic link

A bow and arrow can be shot completely vertically from different points along the x-axis. Shoot an arrow at coordinate x. If the start and end coordinates of the diameter of a balloon are start and end, and start ≤ x ≤ end, the balloon will be detonated. There is no limit to the number of bows and arrows that can be shot. Once the bow and arrow are fired, they can advance indefinitely. We want to find the minimum number of bows and arrows required to detonate all the balloons.

Considering the leftmost position of the right border of all balloons, then there must be an arrow where the shooting position is its right border (otherwise there will be no arrow to detonate it). After we have determined an arrow, we can remove all the balloons detonated by this arrow, and from the remaining undetonated balloons, choose the one that is the farthest to the left of the right boundary to determine the next one. Arrow until all the balloons are detonated.

class Solution {
    
    
public:
	int findMinArrowShots(vector<vector<int>>& points) 
	{
    
    
		if (points.size() == 0)return 0;
		sort(points.begin(), points.end(), [](const vector<int>& a, const vector<int >& b)
			{
    
    return a[1] < b[1];});
		int pos = points[0][1];
		int res = 1;
		for (auto &t : points)
		{
    
    
			if (pos < t[0])
			{
    
    
				pos = t[1];
				res++;
			}
		}
		return res;
	}
};

Guess you like

Origin blog.csdn.net/weixin_45605341/article/details/112967991