# Greedy algorithm interval problem

#### Example 1: interval point selection

Given N closed intervals [ai,bi], please select as few points as possible on the number axis so that each interval contains at least one selected point.

Outputs the minimum number of selected points.

Points located at the end of the interval are also counted as within the interval.

#### Input format

The first line contains the integer N, which represents the interval number.

The next N lines contain two integers ai,bi, representing the two endpoints of an interval.

#### Output format

Outputs an integer representing the minimum number of points required.

1≤N≤105,
−109≤ai≤bi≤109

```3
-1 1
2 4
3 5
```

#### Output example:

```2
```

```import java.io.BufferedReader;
import java.util.Arrays;

public class Main {

public static void main(String[] args) throws Exception {
int n = Integer.parseInt(str[0]);
Edge[] edges = new Edge[n + 1];
for (int i = 1; i <= n; i++) {
edges[i] = new Edge(Integer.parseInt(str[0]), Integer.parseInt(str[1]));
}
Arrays.sort(edges);
int res = 0, t = (int) -2e9;
for (int i = 1; i <= n; i++) {
if (edges[i].l > t) {
res++;
t = edges[i].r;
}
}
System.out.println(res);
}
}

class Edge implements Comparable<Edge> {
int l, r;

public Edge(int l, int r) {
this.l = l;
this.r = r;
}

@Override
public int compareTo(Edge o) {
if (o != null)
return this.r - o.r;
return 0;
}
}
```

#### Example 2: maximum number of disjoint intervals

Given N closed intervals [ai,bi], please select several intervals on the number axis so that the selected intervals do not intersect each other (including endpoints).

Output the maximum number of selectable intervals.

#### Input format

The first line contains the integer N, which represents the interval number.

The next N lines contain two integers ai,bi, representing the two endpoints of an interval.

#### Output format

Output an integer indicating the maximum number of selectable intervals.

1≤N≤105,
−109≤ai≤bi≤109

```3
-1 1
2 4
3 5
```

#### Output example:

```2
```

```//The maximum number of disjoint intervals is equivalent to the number of minimum coverage intervals
//Same as the title code above
import java.util.Arrays;

public class Main {

public static void main(String[] args) throws Exception {
int n = Integer.parseInt(str[0]);
Edge[] edges = new Edge[n + 1];
for (int i = 1; i <= n; i++) {
edges[i] = new Edge(Integer.parseInt(str[0]), Integer.parseInt(str[1]));
}
Arrays.sort(edges);
int res = 0, t = (int) -2e9;
for (int i = 1; i <= n; i++) {
if (edges[i].l > t) {
res++;
t = edges[i].r;
}
}
System.out.println(res);
}
}

class Edge implements Comparable<Edge> {
int l, r;

public Edge(int l, int r) {
this.l = l;
this.r = r;
}

@Override
public int compareTo(Edge o) {
if (o != null)
return this.r - o.r;
return 0;
}
}
```

#### Example 3: interval grouping

Given N closed intervals [ai,bi], please divide these intervals into several groups so that there is no intersection between two intervals (including endpoints) in each group, and make the number of groups as small as possible.

Minimum number of output groups.

#### Input format

The first line contains the integer N, which represents the interval number.

The next N lines contain two integers ai,bi, representing the two endpoints of an interval.

#### Output format

Output an integer representing the minimum number of groups.

1≤N≤105,
−109≤ai≤bi≤109

```3
-1 1
2 4
3 5
```

#### Output example:

```2
```

```import java.io.BufferedReader;
import java.util.Arrays;
import java.util.PriorityQueue;

public class Main {

public static void main(String[] args) throws Exception {
int n = Integer.parseInt(str[0]);
Edge[] edges = new Edge[n + 1];
for (int i = 1; i <= n; i++) {
edges[i] = new Edge(Integer.parseInt(str[0]), Integer.parseInt(str[1]));
}
Arrays.sort(edges);
//Priority Queue PriorityQueue is the implementation of the Queue interface, in which the elements can be sorted
PriorityQueue<Integer> queue = new PriorityQueue<>();
for (int i = 1; i <= n; i++) {
if (queue.isEmpty() || queue.peek() >= edges[i].l) {
} else {
queue.poll();
}
}
System.out.println(queue.size());
}
}

class Edge implements Comparable<Edge> {
int l, r;

public Edge(int l, int r) {
this.l = l;
this.r = r;
}

@Override
public int compareTo(Edge o) {
if (o != null)
return this.l - o.l;
return 0;
}
}
```

#### Example 4: interval coverage

Given N closed intervals [ai,bi] and a segment interval [s,t], please select as few intervals as possible to completely cover the specified segment interval.

Output the minimum number of intervals. If it cannot be completely covered, output − 1.

#### Input format

The first row contains two integers s and t, representing the two endpoints of a given segment interval.

The second line contains the integer N, which represents the number of given intervals.

The next N lines contain two integers ai,bi, representing the two endpoints of an interval.

#### Output format

Output an integer representing the minimum number of intervals required.

If there is no solution, output − 1.

1≤N≤105,
−109≤ai≤bi≤109,
−109≤s≤t≤109

```1 5
3
-1 3
2 4
3 5
```

#### Output example:

```2
```

```import java.io.BufferedReader;
import java.util.Arrays;

public class Main {

public static void main(String[] args) throws Exception {
int st = Integer.parseInt(str[0]);
int ed = Integer.parseInt(str[1]);
int n = Integer.parseInt(str[0]);
Edge[] edges = new Edge[n + 1];
for (int i = 1; i <= n; i++) {
edges[i] = new Edge(Integer.parseInt(str[0]), Integer.parseInt(str[1]));
}
Arrays.sort(edges);
int res = 0;
boolean flag = false;
for (int i = 1; i <= n; i++) {
int j = i;
int t = (int) -2e9;
while (j <= n && edges[j].l <= st) {
t = Math.max(t, edges[j].r);
j++;
}
if (t < st)
break;
res++;
if (t >= ed) {
flag = true;
break;
}
st = t;
i = j - 1;
}
if (!flag)
res = -1;
System.out.println(res);
}
}

class Edge implements Comparable<Edge> {
int l, r;

public Edge(int l, int r) {
this.l = l;
this.r = r;
}

@Override
public int compareTo(Edge o) {
if (o != null)
return this.l - o.l;
return 0;
}
}
```

Tags: Algorithm

Posted on Sun, 21 Nov 2021 17:40:15 -0500 by Pixelsize