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

if (canAppend.getOrDefault(num-1, 0) > 0) {
    canAppend.put(num-1, canAppend.get(num-1)-1);
    canAppend.put(num, canAppend.getOrDefault(num, 0) + 1);

ๅฆ‚ๆžœ current num ๅ‰้ข็š„ๆ•ธๅญ—, ๆœ‰ๆ—ขๆœ‰็š„ subsequence, ้‚ฃๅฏไปฅๆ‹ฟไพ†็นผ็บŒๆ‹ผๆŽฅ,

ๆ‰€ไปฅ่ฎŠๆˆ

canAppend[num-1]โ€”;

canAppend[num]++,

ๅฆ‚ๆžœ้ƒฝๆฒ’ๆœ‰ๆ—ขๆœ‰็š„ subsequnce ๅญ˜ๅœจ, ้‚ฃๅฐฑ่ฆๅš 1. โ‡’ ๅ‰ต็ซ‹ๆ–ฐ็š„ subsequnce

} else { // start by num
    if (count.getOrDefault(num+1, 0) == 0 || count.getOrDefault(num+2, 0) == 0) {
        return false;
    }
    count.put(num+1, count.get(num+1)-1); // num+1 ็”จ้Ž--
    count.put(num+2, count.get(num+2)-1); // num+2 ็”จ้Ž--
    canAppend.put(num+2, canAppend.getOrDefault(num+2, 0) + 1);
}
// ๅฆ‚ๆญค, ๅœจ num+2, ๆœ‰ๆ—ขๆœ‰็š„ subsequnce ++
canAppend.put(num+2, canAppend.getOrDefault(num+2, 0) + 1);

time: O(n)

space:O(n)

class Solution {
    public boolean isPossible(int[] nums) {
        /*
            0. count[x] == 0 continue; 
            1. start by x, x+1, x+2, used, count--, canAppend[x+2]++
            2. canAppend[x-1] > 0, means we can append by before subsequence, canAppend[x-1]--. canAppend[x]++
        */
        Map<Integer, Integer> count = new HashMap<>(); // map[number, freq count => how many keys not checked]
        Map<Integer, Integer> canAppend = new HashMap<>(); // map[append From Which number Ending, how many subsequence has created]
        
        for (int num : nums) {
            count.put(num, count.getOrDefault(num, 0) + 1);
        }
        for (int num : nums) {
            if (count.get(num) == 0) continue;
            
            count.put(num, count.get(num)-1);
            
            if (canAppend.getOrDefault(num-1, 0) > 0) {
                canAppend.put(num-1, canAppend.get(num-1)-1);
                canAppend.put(num, canAppend.getOrDefault(num, 0) + 1);
                
            } else { // start by num
                if (count.getOrDefault(num+1, 0) == 0 || count.getOrDefault(num+2, 0) == 0) {
                    return false;
                }
                count.put(num+1, count.get(num+1)-1);
                count.put(num+2, count.get(num+2)-1);
                canAppend.put(num+2, canAppend.getOrDefault(num+2, 0) + 1);
            }

        }
        return true;
        
    }
}

Last updated