当前位置:网站首页>Leetcode659. split the array into continuous subsequences (hash table)
Leetcode659. split the array into continuous subsequences (hash table)
2022-07-26 14:49:00 【Jay_ fearless】
Title Description
Here's an array of integers sorted in ascending order num( May contain duplicate numbers ), Please divide them into one or more subsequences , Each subsequence is composed of continuous integers with a length of at least 3 .
A subsequence is a selection of parts from the original array ( Or all of them ) Element without changing its relative position
If the above segmentation can be completed , Then return to true ; otherwise , return false .
explain :
The input array length range is [1, 10000]
sample input 1
8
1 2 3 3 4 4 5 5
sample output 1
true
explain :
You can separate these two consecutive subsequences :
1, 2, 3
3, 4, 5
sample input 2
6
1 2 3 4 4 5
sample output 2
false
analysis
This topic adopts greedy thinking , We first use a hash table mp1 Record the number of occurrences of all numbers , Then use another hash table mp2 To indicate that the length is at least 3 Whether the sequence of exists .
If the current number is x, If mp2[x-1] There is , Just connect this number to x-1 Back , Simultaneous updating mp2 China and Israel x-1 End and end with x The number of sequences at the end .
if x-1 The sequence of does not exist , Just reopen a sequence , But it needs judgment x+1,x+2 Whether there is .
C++ Code
#include<bits/stdc++.h>
using namespace std;
int n;
class Solution {
public:
bool isPossible(vector<int>& nums) {
map<int,int> mp1,mp2;
for(auto x:nums) mp1[x]++; // Record the number of occurrences of all numbers
for(auto x:nums)
{
if(!mp1[x]) continue; // If this number runs out , Just skip this cycle
if(mp2[x-1]){ // If the x-1 The sequence of exists , Just put x Add to the end
mp2[x]++;
mp2[x-1]--;
mp1[x]--;
}
else if(mp1[x+1] && mp1[x+2]) // if x-1 The sequence of does not exist , Just reopen a sequence , But it needs judgment x+1,x+2 Whether there is
{
mp2[x+2]++; // Create a to x start ,x+2 The sequence at the end
mp1[x]--,mp1[x+1]--,mp1[x+2]--;
}
else return false;
}
return true;
}
};
int main()
{
cin>>n;
vector<int> v;
int a;
for(int i=0;i<n;i++)
{
cin>>a;
v.push_back(a);
}
if(Solution().isPossible(v)) puts("true");
else puts("false");
return 0;
}
边栏推荐
- 【文件上传漏洞-06】分布式配置文件攻击实验—以upload-labs-4为例
- [ostep] 03 virtualized CPU - restricted direct execution mechanism
- CAS single sign on
- Error reported by Nacos enabled client
- Would you please refer to the document of Database specification?
- RPN: region proposal networks
- SA Siam: Twin network for real-time target tracking
- Low power multi-channel wfas1431 wireless data acquisition and transmission instrument operation process description
- [draw with toolbar]
- Create root permission virtual environment
猜你喜欢

Unity learning notes – infinite map

SiamFC:用于目标跟踪的全卷积孪生网络

BSN IPFS(星际文件系统)专网简介、功能、架构及特性、接入说明

Fill in the questionnaire and receive the prize | we sincerely invite you to fill in the Google play academy activity survey questionnaire

VP video structured framework

智能家居行业发展,密切关注边缘计算和小程序容器技术

GOM登录器配置免费版生成图文教程

SA-Siam:用于实时目标跟踪的孪生网络

中部“第一城”,长沙“人才引力”从争先到领先

PyTorch中 torch.nn与torch.nn.functional的区别
随机推荐
Brief description of llcc68 broadcast wake-up
CAS单点登录
AMB | towards sustainable agriculture: rhizosphere microbial engineering
中部“第一城”,长沙“人才引力”从争先到领先
When AI encounters life and health, Huawei cloud builds three bridges for them
Instructions for various interfaces of hand-held vibrating wire collector vh03
Siamrpn: recommended regional network and twin network
Kubernetes----Pod配置资源配额
unity透明通道的小技巧
[ostep] 03 virtualized CPU - restricted direct execution mechanism
My creation Anniversary - from the heart
Mysql-04 storage engine and data type
网络图片转本地导致内核退出
[solution of ordinary differential equation and drawing solution of small boat walking track]
One stop monitoring of the software and hardware infrastructure of the whole university, and Suzhou University replaces PostgreSQL with time series database
TransC知识表示模型
Use of delve for go development and debugging
How to evaluate the test quality?
Stacked noise reducing auto encoder (sdae)
Figure introduction to neural network core dataset