253. Meeting Rooms II

照原本 252, 56 的思路來做的話....只針對 start 排序, 的確是符合這個case

但不符合 ex2, 這樣跑, count 會是 0, 應該預想, 先佔一個 room , 但這樣 ex1 會錯

單單針對 start 排序是不夠的, 以下是錯的

use line sweep

1.separately sort start, sort end

應該是要改成這樣想

start

| | | |

end

| | | |

start 走到 index 0, 1 時, 都小於 min end

before min end, there are two starts, means 2 rooms are required, count = 2

when start 到 index 2 時, 已經比 min end 大, 代表有一個房間結束了, 所以不需要更多 room,

維持 count = 2, so min end index++

O(logn), O(n)

2. use treeMap

這應該是最好寫的 line sweep, 又實用

time: O(nlogn)

space: O(n)

use minHeap

time : O(nlogn)

space: O(h), heap size, <= n

Last updated

Was this helpful?