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的更多相关文章

  1. Split Array into Consecutive Subsequences

    659. Split Array into Consecutive Subsequences You are given an integer array sorted in ascending or ...

  2. 【LeetCode】659. Split Array into Consecutive Subsequences 解题报告(Python)

    [LeetCode]659. Split Array into Consecutive Subsequences 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id ...

  3. [Swift]LeetCode659. 分割数组为连续子序列 | Split Array into Consecutive Subsequences

    You are given an integer array sorted in ascending order (may contain duplicates), you need to split ...

  4. [LeetCode] Split Array into Consecutive Subsequences 将数组分割成连续子序列

    You are given an integer array sorted in ascending order (may contain duplicates), you need to split ...

  5. LeetCode Split Array into Consecutive Subsequences

    原题链接在这里:https://leetcode.com/problems/split-array-into-consecutive-subsequences/description/ 题目: You ...

  6. 659. Split Array into Consecutive Subsequences

    You are given an integer array sorted in ascending order (may contain duplicates), you need to split ...

  7. leetcode 659. Split Array into Consecutive Subsequences

    You are given an integer array sorted in ascending order (may contain duplicates), you need to split ...

  8. [LeetCode] 659. Split Array into Consecutive Subsequences 将数组分割成连续子序列

    You are given an integer array sorted in ascending order (may contain duplicates), you need to split ...

  9. [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 ...

随机推荐

  1. 查看linux服务器内存信息

    查看服务器内存信息 dmidecode|grep -P -A5 "Memory\s+Device"|grep Size [root@localhost home]# dmideco ...

  2. Ubuntu_安装Wiz笔记

    前言 安装完成了Linux,有了搜狗输入法,我们还需要笔记软件,本文主要介绍如何安装为知笔记 安装步骤 找到wiz官网:http://www.wiz.cn/ 获取Linux安装教程 安装QT 下载的Q ...

  3. Python模块制作

    在Python中,每个Python文件都可以作为一个模块,模块的名字就是文件的名字. 定义自己的模块 比如有这样一个文件test.py,在test.py中定义了函数add def add(a,b): ...

  4. 简单的TCP接受在转发到客户端的套接口

    //功能:客服端发送tcp包,服务器接受到并打印出来,并将包转换为大写后到客户端//2015.9.10成功 #include <stdio.h>#include <sys/socke ...

  5. android studio 解决avd启动问题 ----waiting for target device come online

    android studio 模拟器打不开,一直停留在第三方.waiting for target  device  come online 问题解决方法 方法1.Android Emulator 未 ...

  6. Spring框架(管理事务)

    Spring底层使用Transaction事物模板来进行操作.具体操作: 1.service 需要获得 TransactionTemplate 2.spring 配置模板,并注入给service 3. ...

  7. Unix IPC之Posix消息队列(2)

    /* Query status and attributes of message queue MQDES. */ extern int mq_getattr (mqd_t __mqdes, stru ...

  8. sql server 2000系统表sysproperties在SQL 2008中无效的问题

    Sqlserver有一个扩展属性系统表sysproperties,因为只接触过MSSQL2005及以后的版本,在生产库2008版本及联机文档上搜了下都找不到这个系统表,后来发现这个系统表在2005版本 ...

  9. MySQL学习笔记:删除存储过程和函数

    删除存储过程.存储函数主要使用drop语句: drop procedure  —— 删除存储过程 drop function  —— 删除存储函数 语法: DROP {PROCEDURE|FUNCTI ...

  10. python类、类继承

    yield: 简单地讲,yield 的作用就是把一个函数变成一个 generator,带有 yield 的函数不再是一个普通函数,Python 解释器会将其视为一个 generator,调用 fab( ...