#K79222. Maximum Number of Non-Overlapping Challenges
Maximum Number of Non-Overlapping Challenges
Maximum Number of Non-Overlapping Challenges
You are given challenges, each with a start time and an end time . Your task is to select the maximum number of challenges such that no two selected challenges overlap. This problem can be modeled with intervals where each interval is represented by its start and end times. A challenge is represented as an interval . Two challenges are non-overlapping if when challenge finishes. Use a greedy algorithm that sorts the intervals by their ending times to achieve an optimal solution.
For example, given the intervals: , , , and , the maximum number of non-overlapping challenges is .
inputFormat
The input is read from standard input (stdin). The first line contains an integer , the number of challenges. Each of the next lines contains two space-separated integers representing the start time and the end time of a challenge.
outputFormat
Output a single integer to standard output (stdout), the maximum number of non-overlapping challenges.## sample
4
1 3
2 5
4 6
5 7
2