264. Ugly Number II

https://www.jiuzhang.com/problem/ugly-number-ii/

Solution 1

use minHeap

從小到大從 minheap 取數字出來

注意, heap 如果不用 long, 數字太大時是會錯的, 會超出int 範圍, 可能結果還會失去 sign 的符號變負的

time: O(3nlog3n)

space: O(3n)

or like this, 10th = 0~(10-1-1) 0~8, at lasyt return index 9

min heap

time: O(3nlog3n)

space: O(3n)

類似 DP

T: O(3N) = O(N)

S: O(N)

better explain:

Last updated

Was this helpful?