Detailed explanation of interval problem of greedy algorithm

content

1. Introduction to Greedy Algorithms

basic idea

limitation

2. Classic example

interval problem

Greedy strategy

3. Code


1. Introduction to Greedy Algorithms

basic idea

1) The greedy algorithm (greedy algorithm) refers to taking the best or optimal (that is, the most favorable) choice in each step of the choice when solving the problem, so as to hope to lead to the best or optimal algorithm. .
2) The result obtained by the greedy algorithm is not necessarily the optimal result (sometimes it will be the optimal solution), but it is a result that is relatively approximate (close to) the optimal solution.

limitation

A big reason why the greedy algorithm fails is its obvious limitation: it almost only considers local optima. The so-called local optimization is to only consider the current best interests, neither look one step forward nor one step backward, so that only the optimal solution of the current stage is used every time. Therefore, in most cases, the greedy algorithm cannot obtain the overall optimal solution, but its solution is a good approximation of the optimal solution.

2. Classic example

interval problem

Given multiple intervals, compute the minimum number of intervals to remove to make them non-overlapping. Start and end connections do not count as overlapping.

Input and output example


The input is an array consisting of multiple arrays with a fixed length of 2, representing the start and end of the interval.

Output an integer representing the number of intervals to remove.


Input :[1,2],[2,4],[1,3]]


Output :1

In this example, we can remove the interval [1,3] so that the remaining intervals [1,2],[2,4]] do not overlap each other.

Greedy strategy

When choosing an interval to keep, the end of the interval is important: the smaller the interval end you choose, the more room you have left for other intervals,
and the more intervals you can keep.

Therefore, the greedy strategy we adopt is to preferentially keep the intervals with small endings and disjoint intervals. The specific implementation method is:

1. First sort the interval in increasing order according to the size of the end ,

2. Each time select the interval with the smallest ending and does not overlap with the previously selected interval.

In the example, the sorted array is [1,2],[1,3],[2,4]]. According to our greedy strategy, we first initialize to the interval [1,2]; since [1,3] intersects with [1,2], we skip this interval; since [2,4] does not intersect with [1,2] , we keep it. Therefore, the final reserved interval is [1,2], [2,4].

3. Code

import java.util.Arrays;
import java.util.Comparator;

public class Solution {
	public static int merge(int[][] intervals) {
		//先按照区间结尾的大小进行升序排序
		Arrays.sort(intervals, new Comparator<int[]>() {
			@Override
			public int compare(int[] o1, int[] o2) {
				return o1[1] - o2[1];
			}
		});
		int count =1;//区间不重合的个数,默认第一个满足题意,初始化为1
		int cur = intervals[0][1];//排序完成后贪心选择的最小的区间结尾
		for (int i = 1; i < intervals.length; i++) {
			if(intervals[i][0] >= cur) {
				count++;
				cur=intervals[i][1];
			}
		}
        //所有区间的个数减去不重叠区间的个数,就是重叠区间的个数。
		return intervals.length - count;
    }
}

Guess you like

Origin blog.csdn.net/qq_52360069/article/details/123596797