当前位置:网站首页>Force deduction exercise - 29 complete the array as required
Force deduction exercise - 29 complete the array as required
2022-07-24 12:12:00 【qq_ forty-three million four hundred and three thousand six hun】
29 Complete the array as required
1. Problem description
Given a sorted array of positive integers nums, And a positive integer n . from [1, n] Select any number in the interval and add it to nums in , bring [1, n] Any number in the interval can be used nums The sum of some numbers in . Please output the minimum number of numbers that need to be supplemented to meet the above requirements .
Example 1:
Input : nums = [1,3], n = 6
Output : 1
explain :
according to nums The existing combination in [1], [3], [1,3], We can draw 1, 3, 4.
Now if we will 2 Add to nums in , Combination becomes : [1], [2], [3], [1,3], [2,3], [1,2,3].
The sum of them can represent numbers 1, 2, 3, 4, 5, 6, Be able to cover [1, 6] All the numbers in the interval .
So we need to add at least one number .
Example 2:
Input : nums = [1,5,10], n = 20
Output : 2
explain : We need to add [2, 4].
Example 3:
Input : nums = [1,2,2], n = 5
Output : 0
2. Enter description
First, enter the length of the array m, Then input m It's an integer .
Finally, enter a positive integer n.
n No more than 32 position int The representation range of type , namely :n<=2147483647.
3. The output shows that
Output an integer
4. Example
Input
3
1 5 10
20
Output
2
5. Code
#include<iostream>
#include<vector>
#include<string>
#include<algorithm>
#include<unordered_map>
#include<set>
#include<stack>
using namespace std;
int min_Patches(vector<int> &nums, int sum)
{
// requirement :
// Given a sorted array of positive integers nums, from [1, n] Select any number in the interval and add it to nums in , bring [1, n] Any number in the interval can be used nums The sum of some numbers in .
// Please output the minimum number of numbers that need to be supplemented to meet the above requirements .
// Problem solving https://leetcode.cn/problems/patching-array/solution/an-yao-qiu-bu-qi-shu-zu-by-leetcode-solu-klp1/
// about x, if [1,x-1] All numbers are overwritten , And x In the array , be [1,2*x-1] All numbers are also overwritten
//1. Definition
long long total = 1;// The cumulative sum
int index = 0;// Ergodic subscript
int count = 0;// The number of numbers to be added
//2. Traversal and processing
// Every time an array is not found nums The smallest integer covered x, Add x, Then find the next smallest integer that is not covered , Repeat the above steps until the interval [1,n] All numbers in are overwritten .
while (total <= sum)
{
if (index < nums.size() && nums[index] <= total )
{
total += nums[index++];
}
else
{
total = total * 2;
count++;// Add one to the number that needs to be supplemented
}
}
return count;
}
int main()
{
vector<int>nums;
int m,tmp,n;
cin >> m;
for (int i = 0; i < m; i++)
{
cin >> tmp;
nums.push_back(tmp);
}
cin >> n;
int res = min_Patches(nums, n);
cout << res << endl;
return 0;
}
边栏推荐
- 计算两个坐标经纬度之间的距离(5种方式)
- Optimization method of "great mathematics for use" -- optimal design of Cascade Reservoir Irrigation
- Examples of map search
- QT notes - realize form adaptation
- STM32——C语言基础
- Time processing of basic library in go
- L1-043 reading room
- 字符串匹配的KMP
- How to eliminate the character set and sorting rules specially set for fields in MySQL tables?
- [mathematical basis of Cyberspace Security Chapter 9] finite field
猜你喜欢

OpenCV:08图像金字塔

Aruba learning notes 04 Web UI -- Introduction to configuration panel
![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]

Install MariaDB columnstore (version 10.3)

Design of digital oscilloscope based on arm and FPGA -- QMJ

【网络空间安全数学基础第3章】同余

Examples of map search

容错、熔断的使用与扩展

08.01 adjacency matrix

Zhihuihuayun | cluster log dynamic collection scheme
随机推荐
Convergence rules for 4 * 4 image weights
L1-059 敲笨钟
STM32——C语言基础
Differences between JS map and foreach
Three small knowledge points about data product managers
[C and pointer Chapter 14] preprocessor
GCC的基本用法
Oracle 11.2.0.4 ASM single instance does not start automatically with system startup
Anaconda environment migration
TypeNameExtractor could not be found
Quick check list of various XSS payloads
Judge whether a group of cards can become shunzi (the size of the king is 14,15)
Examples of map search
[Commons beanautils topic] 004 beanautils topic
[C and pointer Chapter 11] dynamic memory allocation
Source code analysis sentry user behavior record implementation process
QT | summary of the use of edit box
Using huggingface model to translate English
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
Remember to optimize my personal blog once