title: Interval Partitioning by Finish Time?
body: <p>Given <code>n</code> lectures, each with start time and finish time, the problem is to assign all the lectures to rooms such that no two lectures occur at the same time in the same room. It is easy to design a greedy algorithm that sorts lectures by start time to minimize the number of rooms used. What if we process lectures by their finish time? Does the greedy algorithm still work? I tried a few examples, and did not find a counter example yet. </p>
  
  <p>Greedy algorithm by Start Time</p>
  
  <ol>
  <li><p>Sort all the lectures by start time in ascending order</p></li>
  <li><p>Place all the used rooms in a priority queue. The priority of a room is based on the finish time of last lecture scheduled in the room.</p></li>
  <li><p>Assign each lecture to the first available room in the queue. A room is available if the finish time of last lecture scheduled in the room is less than the start time of the current lecture (no overlap).</p></li>
  <li><p>If none of rooms in the queue is available, add a new room, and insert the lecture into the new room.</p></li>
  </ol>
  
  <p>The above greedy algorithm works. What if we change step 1, sort lectures by finish time, and keep other steps the same? Does it still work?</p>
  
  <p>If we sort lectures by finish time in descending order, that is, we would process the last lecture first, that is a symmetric of the above greedy algorithm, I believe it definitely works.</p>
  
  <p>But my question is, we sort lectures by finish time in ascending order, apply the greedy algorithm, does it work?</p>