659. Split Array into Consecutive Subsequences

Description

You are given an integer array sorted in ascending order (may contain duplicates), you need to split them into several subsequences, where each subsequences consist of at least 3 consecutive integers. Return whether you can make such a split.

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

Example 3:

Input: [1,2,3,4,4,5]
Output: False

Note:

  1. The length of the input is in range of [1, 10000]

Solution

Greedy, time O(n), space O(n)

  • 先遍历数组得到每个num出现的frequency。
  • 对于每个num来说,要么将它append到已经成型的sequence中,要么以它为起点开辟一个新的长度为3的sequence。如果两者皆不可行,则返回false。
class Solution {
    public boolean isPossible(int[] nums) {
        Map freq = new HashMap<>();
        Map appendFreq = new HashMap<>();
        for (int n : nums) {
            freq.put(n, freq.getOrDefault(n, 0) + 1);
        }
        
        for (int n : nums) {
            if (freq.get(n) == 0) {     // n is eat up
                continue;
            }
            if (appendFreq.getOrDefault(n, 0) > 0) {
                // append to an exsiting consecutive sequence
                appendFreq.put(n, appendFreq.get(n) - 1);
                appendFreq.put(n + 1, appendFreq.getOrDefault(n + 1, 0) + 1);
            } else if (freq.getOrDefault(n + 1, 0) > 0 
                       && freq.getOrDefault(n + 2, 0) > 0) {
                // create a new consecutive sequence of 3 elements
                freq.put(n + 1, freq.get(n + 1) - 1);
                freq.put(n + 2, freq.get(n + 2) - 1);
                appendFreq.put(n + 3, appendFreq.getOrDefault(n + 3, 0) + 1);
            } else {
                return false;
            }
            
            freq.put(n, freq.get(n) - 1);
        }
        
        return true;
    }
}

你可能感兴趣的:(659. Split Array into Consecutive Subsequences)