当前位置:网站首页>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;
}
边栏推荐
- 【C和指针第14章】预处理器
- [mathematical basis of Cyberspace Security Chapter 9] finite field
- One week's wonderful content sharing (issue 13)
- Common shortcuts to VIM editor
- Mysql database
- makefile快速使用
- C # entry series (29) -- preprocessing commands
- 字符串匹配的KMP
- Convergence rules for 4 * 4 image weights
- Svn server and client installation (Chinese package) and simple use
猜你喜欢

Remember to optimize my personal blog once

Microsoft SQL Server database language and function usage (XII)

一周精彩内容分享(第13期)

Share the typora tool

Record a garbage collection and analysis of gceasy

6k+ star, a deep learning code base for Xiaobai! One line of code implements all attention mechanisms!

JVM visualvm: multi hop fault handling tool

Online XML to CSV tool

Leetcode:51. queen n

The biggest crisis for testers in the workplace is not at the age of 30, but being laid off in middle age
随机推荐
Common formulas and application scenarios of discrete distribution
Microsoft SQL Server database language and function usage (XII)
L1-059 ring stupid bell
scrapy-redis写项目备忘
Oracle 11.2.0.4 ASM single instance does not start automatically with system startup
Please ask whether Oracle CDC does not support checkpointing. When the task is suspended and restarted during the real-time collection process, is the data changed
[rust] rust language foundation | you should quickly get an impression when learning a language
How to upload pictures from typora to CSDN
Day5: construct program logic
6k+ star, a deep learning code base for Xiaobai! One line of code implements all attention mechanisms!
oracle 11.2.0.4 asm单实例不随系统启动而自动开启
1184. Distance between bus stops: simple simulation problem
Easy to use example
Delphi gexperts expert instructions for improving development efficiency
利用huggingface模型翻译英文
Skillfully using command line parameters in Delphi to realize the trigger function of dragging files onto program icons
js图像转base64
Pushgateway installation and Prometheus configuration
Learn some programming: anti unemployment "vaccine"
Common shortcuts to VIM editor