659. Split Array into Consecutive Subsequences
阿新 • • 發佈:2018-11-08
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 1. We iterate through the array once to get the frequency of all the elements in the array2. We iterate through the array once more and for each element we either see if it can be appended to a previously constructed consecutive sequence or if it can be the start of a new consecutive sequence. If neither are true, then we return false. public boolean isPossible(int[] nums) { Map<Integer, Integer> freq = newHashMap<>(), appendfreq = new HashMap<>(); for (int i : nums) freq.put(i, freq.getOrDefault(i,0) + 1); for (int i : nums) { if (freq.get(i) == 0) continue; else if (appendfreq.getOrDefault(i,0) > 0) { appendfreq.put(i, appendfreq.get(i) - 1); appendfreq.put(i+1, appendfreq.getOrDefault(i+1,0) + 1); } else if (freq.getOrDefault(i+1,0) > 0 && freq.getOrDefault(i+2,0) > 0) { freq.put(i+1, freq.get(i+1) - 1); freq.put(i+2, freq.get(i+2) - 1); appendfreq.put(i+3, appendfreq.getOrDefault(i+3,0) + 1); } else return false; freq.put(i, freq.get(i) - 1); } return true; }