简体   繁体   中英

Mutually Overlapping Subset of Activites

I am prepping for a final and this was a practice problem. It is not a homework problem.

How do I go about attacking this? Also, more generally, how do I know when to use Greedy vs. Dynamic programming? Intuitively, I think this is a good place to use greedy. I'm also thinking that if I could somehow create an orthogonal line and "sweep" it, checking the #of intersections at each point and updating a global max, then I could just return the max at the end of the sweep. I'm not sure how to plane sweep algorithmically though.

a. We are given a set of activities I1 ... In: each activity Ii is represented by its left-point Li and its right-point Ri. Design a very efficient algorithm that finds the maximum number of mutually overlapping subset of activities (write your solution in English, bullet by bullet).

b. Analyze the time complexity of your algorithm.

Proposed solution:

Ex set: {(0,2) (3,7) (4,6) (7,8) (1,5)} Max is 3 from interval 4-5

1) Split start and end points into two separate arrays and sort them in non-decreasing order

Start points: [0,1,3,4,7] (SP) End points: [2,5,6,7,8] (EP)

I know that I can use two pointers to sort of simulate the plane sweep, but I'm not exactly sure how. I'm stuck here.

I'd say your idea of a sweep is good.

You don't need to worry about planar sweeping, just use the start/end points. Put the elements in a queue. In every step take the smaller element from the queue front. If it's a start point, increment current tasks count, otherwise decrement it.

Since you don't need to point which tasks are overlapping - just the count of them - you don't need to worry about specific tasks duration.

Regarding your greedy vs DP question, in my non-professional opinion greedy may not always provide valid answer, whereas DP only works for problem that can be divided into smaller subproblems well. In this case, I wouldn't call your sweep-solution either.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM