All problems

Problem 469

Video Stitching

Medium · 20 pts · Array, Greedy, Dynamic Programming

Problem

You have video clips from a sporting event that lasted T seconds. Clip i covers the time interval [start, end], and clips may overlap. You can cut clips into smaller pieces freely. Find the minimum number of clips needed to cover the whole event [0, T], or -1 if it is impossible. Greedily, from the current covered end, choose among the clips that start at or before it the one that reaches furthest.

Input

First line: N and T. Next N lines: start and end of each clip.

Output

Print the minimum number of clips, or -1.

Constraints

1 <= N <= 100; 0 <= start <= end <= 100; 1 <= T <= 100

Example input

6 10
0 2
4 6
8 10
1 9
1 5
5 9

Example output

3

Explanation

With T = 10, the clips [0,2], [1,9] and [8,10] together cover [0,10], so 3 clips are enough. No two clips cover the whole event.

Every problem here can be used in a RelayTrixCode team match. Create a room.

Previous: Hand of Straights Next: Queue Reconstruction by Height