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

leetcode659 Split Array into Consecutive Subsequences

时间:2017-08-21 19:41:02      阅读:153      评论:0      收藏:0      [点我收藏+]

标签:end   turn   amp   ==   order   div   tor   bsp   tin   

思路:

对于每个数,尽量放在已有子序列的后面;如果不能,就创建一个新的子序列。

实现:

 1 class Solution 
 2 {
 3 public:
 4     bool isPossible(vector<int>& nums) 
 5     {
 6         unordered_map<int, int> cnt, end;
 7         for (auto i : nums) cnt[i]++;
 8         for (auto i : nums)
 9         {
10             if (cnt[i] == 0) continue;
11             else if (end[i - 1] > 0)
12             {
13                 end[i - 1]--;
14                 end[i]++;
15             }
16             else if (cnt[i + 1] > 0 && cnt[i + 2] > 0)
17             {
18                 cnt[i + 1]--;
19                 cnt[i + 2]--;
20                 end[i + 2]++;
21             }
22             else return false;
23             cnt[i]--;
24         }
25         return true;
26     }
27 };

 

leetcode659 Split Array into Consecutive Subsequences

标签:end   turn   amp   ==   order   div   tor   bsp   tin   

原文地址:http://www.cnblogs.com/wangyiming/p/7406148.html

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