码迷,mamicode.com
首页 > 其他好文 > 详细

LeetCode 659: Split Array into Consecutive Subsequence

时间:2017-09-10 15:48:54      阅读:144      评论:0      收藏:0      [点我收藏+]

标签:leetcode   als   etc   div   col   ash   for   isp   turn   

Note:

1. If it does not belong any sequences : append.getOrDefault(num, 0) == 0, create a new sequence. It requires num + 1 and num + 2 count > 0.

2. Once it has sequence, move sequece to num + 1.

class Solution {
    public boolean isPossible(int[] nums) {
        if (nums.length < 3) {
            return false;
        }
        
        Map<Integer, Integer> count = new HashMap<>(), append = new HashMap<>();
        for (int num : nums) count.put(num, count.getOrDefault(num, 0) + 1);
        for (int num : nums) {
            if (count.get(num) == 0) continue;
            else if (append.getOrDefault(num, 0) > 0) {
                append.put(num, append.get(num) - 1);
                append.put(num + 1, append.getOrDefault(num + 1, 0) + 1);
            } else if (count.getOrDefault(num + 1, 0) > 0 && count.getOrDefault(num + 2, 0) > 0) {
                count.put(num + 1, count.get(num + 1) - 1);
                count.put(num + 2, count.get(num + 2) - 1);
                append.put(num + 3, append.getOrDefault(num + 3, 0) + 1);
            } else return false;
            count.put(num, count.get(num) - 1);
        }
        return true;
    }
}

 

LeetCode 659: Split Array into Consecutive Subsequence

标签:leetcode   als   etc   div   col   ash   for   isp   turn   

原文地址:http://www.cnblogs.com/shuashuashua/p/7501050.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!