Split Array into Consecutive Subsequences

Given an array nums sorted in ascending order, return true if and only if you can split it into 1 or more subsequences such that each subsequence consists of consecutive integers and has length at least 3.

Example 1:

Input: [1,2,3,3,4,5]
Output: True
Explanation:
You can split them into two consecutive subsequences : 
1, 2, 3
3, 4, 5

Example 2:

Input: [1,2,3,3,4,4,5,5]
Output: True
Explanation:
You can split them into two consecutive subsequences : 
1, 2, 3, 4, 5
3, 4, 5

Idea: Use greedy to count the frequency first, and then use an urgentTail hashmap to count the frequency of the next number I will need. The subtle thinking of this question is whether the number has been used, it is marked with count, and it will be reduced by one. In the next judgment, see if there is still count. If there is no count, it means that it can no longer be used. It has been used before.

In addition, Greedy's idea is: every time a number is encountered, if it can be used, first consider whether someone in the urgentmap needs this number, add it, and add the next required number at the same time, which is greed, and then if no one needs it, Just start a 3 straight, if it can't be turned on, it means no, it can be turned on, the frequency of three counts is -1, and the next number is added to the urgentmap;

class Solution {
    public boolean isPossible(int[] nums) {
        if(nums == null || nums.length == 0) {
            return false;
        }
        HashMap<Integer, Integer> frequencyMap = new HashMap<>();
        HashMap<Integer, Integer> urgentTail = new HashMap<>();
        
        for(Integer i: nums) {
            frequencyMap.put(i, frequencyMap.getOrDefault(i, 0) + 1);
        }
        
        for(Integer i: nums) {
            // 用count -1 代表已经用过了;如果还有没有用的,count number > 0;
            if(frequencyMap.get(i) <= 0) {
                continue;
            }
            // Greedy,首先满足已经有的顺子的尾巴,并且加入新的尾巴;
            if(urgentTail.getOrDefault(i, 0) > 0) {
                frequencyMap.put(i, frequencyMap.get(i) - 1);
                urgentTail.put(i, urgentTail.get(i) - 1);
                // Greedy,  把下一个也加进去;
                urgentTail.put(i + 1, urgentTail.getOrDefault(i + 1, 0) + 1);
            } else { 
                // 开始一个顺子:i, i + 1, i + 2; 
                if(frequencyMap.getOrDefault(i + 1, 0) > 0 
                       && frequencyMap.getOrDefault(i + 2, 0) > 0) {
                    frequencyMap.put(i, frequencyMap.get(i) - 1);
                    frequencyMap.put(i + 1, frequencyMap.get(i + 1) - 1);
                    frequencyMap.put(i + 2, frequencyMap.get(i + 2) - 1);
                    urgentTail.put(i + 3, urgentTail.getOrDefault(i + 3, 0) + 1);
                } else {
                    // 没有超过3个的顺子;
                    return false;
               }
            }  
        }
        return  true;
    }
}

 

Published 710 original articles · Like 13 · Visits 190,000+

Guess you like

Origin blog.csdn.net/u013325815/article/details/105525920