当前位置:网站首页>Force deduction exercise - 26 split array into continuous subsequences
Force deduction exercise - 26 split array into continuous subsequences
2022-07-24 12:12:00 【qq_ forty-three million four hundred and three thousand six hun】
26 Split the array into successive subsequences
1. Problem 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 .
Example 1:
Input : [1,2,3,3,4,5]
Output : True
explain :
You can separate these two consecutive subsequences :
1, 2, 3
3, 4, 5
Example 2:
Input : [1,2,3,3,4,4,5,5]
Output : True
explain :
You can separate these two consecutive subsequences :
1, 2, 3, 4, 5
3, 4, 5
Example 3:
Input : [1,2,3,4,4,5]
Output : False
explain :
The input array length range is [1, 10000]
2. Enter description
First type num The length of n, Then input n It's an integer
3. The output shows that
Output “true” or “false”, Exclude Quotes .
4. Example
Input
8
1 2 3 3 4 4 5 5
Output
true
5. Code
#include<iostream>
#include<vector>
#include<string>
#include<algorithm>
#include<unordered_map>
using namespace std;
bool isPossible(vector<int>& nums) {
//1. Use two hash tables nc and tail
//cnt[i] Store the numbers in the original array i Number of occurrences
//tail[i] Store in i The number of consecutive subsequences at the end and in line with the meaning of the question
unordered_map<int, int> cnt, tail;//unordered_map: Its implementation uses a hash table ; and map The implementation uses a red black tree
//2. Count the number of times
for (auto num : nums)
cnt[num]++;
//3. Traverse Only when a certain number is checked , This number has not been consumed , And it cannot form a continuous subsequence with the front , Nor can it form a continuous subsequence with the following , Cannot divide
for (auto num : nums)
{
if (cnt[num] == 0) continue;
else if (cnt[num] > 0 && tail[num - 1] > 0) //tail[num-1]>0 Represents the tail of the preceding continuous subsequence to num-1 ; If at this time num At least one , Then the subsequence can be extended
{
cnt[num]--;//num Number of occurrences minus one
tail[num - 1]--;// With num-1 Subtract one from the number of consecutive subsequences at the end
tail[num]++;// With num Add one to the number of consecutive subsequences at the end
}
else if (cnt[num] > 0 && cnt[num + 1] > 0 && cnt[num + 2] > 0) // There are continuous subsequences
{
cnt[num]--;
cnt[num + 1]--;
cnt[num + 2]--;
tail[num + 2]++;
}
else
return false;
}
return true;
}
int main()
{
int n,tmp;
cin >> n;
vector<int>nums;
for (int i = 0; i < n; i++)
{
cin >> tmp;
nums.push_back(tmp);
}
bool res = isPossible(nums);
//true The return value is 1 false The return value is 0
res == 1 ? (cout << "true") : (cout << "false");
return 0;
}
边栏推荐
- Repeated calls, messages, idempotent schemes, full collation
- TypeNameExtractor could not be found
- TypeNameExtractor could not be found
- CCF 201803_ 1 jump jump
- 三层交换机配置MSTP协议详解【华为eNSP实验】
- 6k+ star,面向小白的深度学习代码库!一行代码实现所有Attention机制!
- Oracle 11.2.0.4 ASM single instance does not start automatically with system startup
- MES系统设备管理概述(中)
- Open source DNS software powerdns BIND9 mydns
- OpenCV:08图像金字塔
猜你喜欢

Microsoft SQL Server database language and function usage (XII)

TypeNameExtractor could not be found

Chapter 1 Introduction

The art of management - driving software R & D efficiency through leadership

Basic usage of GCC

【C和指针第14章】预处理器
![[C and pointer Chapter 14] preprocessor](/img/da/a9a15299157389f8738f7c642a9ff7.png)
[C and pointer Chapter 14] preprocessor

Three small knowledge points about data product managers

一周精彩内容分享(第13期)
![Detailed OSPF configuration of layer 3 switch / router [Huawei ENSP experiment]](/img/a9/f080940ec7bf94ab83c922990efa62.png)
Detailed OSPF configuration of layer 3 switch / router [Huawei ENSP experiment]
随机推荐
1184. Distance between bus stops: simple simulation problem
Blue team resource collection
Time processing of basic library in go
Difference between GCC -l parameter and -l parameter
Oceanbase Database Setup Test
【网络空间安全数学基础第3章】同余
QT | summary of the use of edit box
Share the typora tool
Basic usage of GCC
Miss waiting for a year! Baidu super chain digital publishing service is limited to 50% discount
Detailed explanation of MSTP protocol for layer 3 switch configuration [Huawei ENSP experiment]
Optimization method of "great mathematics for use" -- optimal design of Cascade Reservoir Irrigation
2 万字详解,吃透 ES!
STM32——C语言基础
一文看懂MES系统能实现企业哪些目标
Svn server and client installation (Chinese package) and simple use
Agile? DevOps ?
基于ARM和FPGA的数字示波器设计——QMJ
Why is there discontinuity in MySQL auto increment primary key?
Jackson parsing JSON detailed tutorial