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