Given n segments of line (into the X axis) with coordinates [li; ri]. You are to choose the minimum number of segments that cover the segment [0;M]. Design a greedy algorithm to solve this problem.
Here what I did: sorting them by starting points in increasing order, then I choose the longest one, second longest.... But here is a problem: Suppose we want to cove [0,12] segment, and there are 3 segments: [0,5],[5,12], [0,10]. Follow the algorithm, it will take [0,10], then it does not cover all the segment, but if we take the other two, it will cover up.
Do you guys have any other idea? without sorting and taking longest lines does not work. we want to cover segment [0,12] and there are 5 segments: [0,2][2,10].[10,12], [0,6][6,12] Follow ur algo the first three are chosen but the last 2 r the optimal one