659. Split Array into Consecutive Subsequences

want to find CIS,

but make sure it's cis and length >= 3, make sure num, num+1, num+2 exists in the following nums

count: map[number, freq count => how many keys not checked]

canAppend: map[append From Which number Ending, how many subsequence has created]

ex: 1 1 2 2 3 3 4

1. 創立新的 subsequnce

起始 by 某數 x, 那要確保 x+1, x+2 都成立

ex: 起始 by 1, 那 2 3 都確保有用, count—

1 x x 2 x x 3

2. append from 某 subseq

1 x x 2 x x 3 xxxx [4], 假設已經有既有的 subsequence, 後面直接接上

why append, 為什麼不重啟一個新的 subsequence ⇒ 因為 直接 append 可以確保一定成功, 重啟一個新的 , 還要確定後面有 5, 6

如果 current num 前面的數字, 有既有的 subsequence, 那可以拿來繼續拼接,

所以變成

canAppend[num-1]—;

canAppend[num]++,

如果都沒有既有的 subsequnce 存在, 那就要做 1. ⇒ 創立新的 subsequnce

time: O(n)

space:O(n)

Last updated

Was this helpful?