leetcode659. Split Array into Consecutive Subsequences
leetcode659. Split Array into Consecutive Subsequences
题意:
您将获得按升序排列的整数数组(可能包含重复项),您需要将它们拆分成多个子序列,其中每个子序列至少包含3个连续的整数。返回是否可以进行这样的拆分。
思路:
虽然是medium,但是我感觉有点难想= =。 O(n)复杂度。
dp。
先分开各种不同的序列。按如果间隔大于1就坑定是不同的序列。按此分开不同的序列以此简化问题。
然后处理连续的序列。连续的序列false的情况就是没有足够的数字构成连续的序列。
然后转化成一个mp,值为这个数字的个数。
遍历时用一个one记录以此处为end的序列长度为1的序列。用two记录到此处长度为2的序列。因为贪心的缘故。每次的移动后。因为要尽可能完成一个序列,如果此时这个数字的个数 < one + two的个数。说明这个数字是不够完成one,two要构成连续序列的个数的。所以返回False。two就是上一个的one的值。然后one的值,如果比上个数字的个数大,这个数字的个数和上个数字个数的差。但是如果比上个数字小,那就取零。为了方便,用tot来储存上一个数字的个数。tot表示的含义其实就是要构成序列。下一个数字所必须的长度。
ac代码:
C++
class Solution {
public:
bool isPossible(vector<int>& nums) {
int len = nums.size();
int k = 0;
for(int i = 0; i < len; i++)
{
if(i > 0 && nums[i] > nums[i - 1] + 1)
{
if(!check(nums,k,i - 1)) return false;
else k = i;
}
}
return check(nums,k,len - 1);
}
private:
bool check(vector<int>& nums,int s, int e)
{
int n = nums[e] - nums[s] + 1;
vector<int> mp(n,0);
for(int i = s; i <= e; i++)
{
mp[nums[i] - nums[s]]++;
}
int one, two, tot;
one = two = tot = 0;
for(int i = 0; i < n; i++)
{
if(mp[i] < one + two) return false;
two = one;
one = max(0, mp[i] - tot);
tot = mp[i];
}
return one == 0 && two == 0;
}
};
python
class Solution:
def isPossible(self, nums):
"""
:type nums: List[int]
:rtype: bool
"""
last = 0
for i , num in enumerate(nums):
if i > 0 and nums[i] > nums[i - 1] + 1:
if self.check(nums, last, i - 1) == False:
return False
else:
last = i
return self.check(nums, last, len(nums) - 1)
def check(self, nums, s, e):
mp = []
for i in range(nums[e] - nums[s] + 1):
mp.append(0)
for i in range(s,e + 1):
mp[nums[i] - nums[s]] += 1
one = two = tot = 0
for m in mp:
if m < one + two:
return False
two = one
one = max(0, m - tot)
tot = m
return one == 0 and two == 0
leetcode659. Split Array into Consecutive Subsequences的更多相关文章
- Split Array into Consecutive Subsequences
659. Split Array into Consecutive Subsequences You are given an integer array sorted in ascending or ...
- 【LeetCode】659. Split Array into Consecutive Subsequences 解题报告(Python)
[LeetCode]659. Split Array into Consecutive Subsequences 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id ...
- [Swift]LeetCode659. 分割数组为连续子序列 | Split Array into Consecutive Subsequences
You are given an integer array sorted in ascending order (may contain duplicates), you need to split ...
- [LeetCode] Split Array into Consecutive Subsequences 将数组分割成连续子序列
You are given an integer array sorted in ascending order (may contain duplicates), you need to split ...
- LeetCode Split Array into Consecutive Subsequences
原题链接在这里:https://leetcode.com/problems/split-array-into-consecutive-subsequences/description/ 题目: You ...
- 659. Split Array into Consecutive Subsequences
You are given an integer array sorted in ascending order (may contain duplicates), you need to split ...
- leetcode 659. Split Array into Consecutive Subsequences
You are given an integer array sorted in ascending order (may contain duplicates), you need to split ...
- [LeetCode] 659. Split Array into Consecutive Subsequences 将数组分割成连续子序列
You are given an integer array sorted in ascending order (may contain duplicates), you need to split ...
- [LC] 659. 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 ...
随机推荐
- Linux USB驱动框架分析 【转】
转自:http://blog.chinaunix.net/uid-11848011-id-96188.html 初次接触与OS相关的设备驱动编写,感觉还挺有意思的,为了不至于忘掉看过的东西,笔记跟总结 ...
- Mysql_Learning_Notes_mysql系统结构_2
Mysql_Learning_Notes_mysql系统结构_2 三层体系结构,启动方式,日志类型及解析方法,mysql 升级 连接层 通信协议处理\线程处理\账号认证(用户名和密码认证)\安全检查等 ...
- Asp.Net使用百度编辑器(ueditor)
1. 1.4.3以上版本将不再承诺支持ie6/ie7. 2.如果是aspx 需要加上 ValidateRequest="false" 3.Web.config <syst ...
- iframe 同域下父子页面的通信
//共同引用的JS文件 common.js ; (function (window, $) { $(function ($) { window.trip = window.trip || {}; wi ...
- Genymotion上不能安装APK软件的问题
Genymotion模拟器不能安装APK的原因 官网给出的解释:Genymotion模拟器使用的是x86架构,在第三方市场上的应用有部分不采用x86这么一种架构,所以在编译的时候不通过,报“APP n ...
- 利用CSS函数calc(...)实现Web页面左右布局
前言 因为自己的网站需要,想要做一个左右布局的页面: 左边是导航菜单之类的东西.右边是文档内容(因为最近看的一些软件的文档页面都是这么布局的): 左边固定宽度——300像素.右边使用剩余的宽度: 左边 ...
- CCF CSP 201703-2 学生排队
博客中的文章均为meelo原创,请务必以链接形式注明本文地址 CCF CSP 201703-2 学生排队 问题描述 体育老师小明要将自己班上的学生按顺序排队.他首先让学生按学号从小到大的顺序排成一排, ...
- USACO 6.5 The Clocks
The ClocksIOI'94 - Day 2 Consider nine clocks arranged in a 3x3 array thusly: |-------| |-------| |- ...
- Maven 仓库之阿里云镜像配置
每当项目开发中 update Maven Project 的时候,我们会发现那个进度是非常的慢,这也严重阻碍了平日的开发进度. 然而,殊不知阿里云搭建了一个国内镜像 http://mav ...
- htm5本地存储方案——indexdb的封装
不BB直接上代码 /*封装IndexdDB*/ var localDatabase = { }; localDatabase.dbName = "yiliDB"; localDat ...