当前位置:网站首页>The least operation of leetcode simple problem makes the array increment
The least operation of leetcode simple problem makes the array increment
2022-07-03 04:34:00 【·Starry Sea】
subject
Give you an array of integers nums ( Subscript from 0 Start ). In every operation , You can select an element in the array , And add it 1 .
For example , If nums = [1,2,3] , You can choose to increase it nums[1] obtain nums = [1,3,3] .
Please return to make nums Strictly increasing Of least Operating frequency .
We call it an array nums yes Strictly increasing , When it satisfies for all 0 <= i < nums.length - 1 There are nums[i] < nums[i+1] . A length of 1 Is a special case of strictly incrementing .
Example 1:
Input :nums = [1,1,1]
Output :3
explain : You can do the following :
- increase nums[2] , The array becomes [1,1,2] .
- increase nums[1] , The array becomes [1,2,2] .
- increase nums[2] , The array becomes [1,2,3] .
Example 2:
Input :nums = [1,5,2,4,1]
Output :14
Example 3:
Input :nums = [8]
Output :0
Tips :
1 <= nums.length <= 5000
1 <= nums[i] <= 10^4
source : Power button (LeetCode)
Their thinking
It can be like an example 1 That way, iterate over the array several times to change the array , You can also correct the currently unqualified value once during forward traversal , In this way, multiple backtracking is avoided .
class Solution:
def minOperations(self, nums: List[int]) -> int:
count=0
for i in range(1,len(nums)):
if nums[i]<=nums[i-1]:
count+=nums[i-1]+1-nums[i]
nums[i]=nums[i-1]+1
return count

边栏推荐
- Some information about the developer environment in Chengdu
- What's wrong with SD card data damage? How to recover SD card data damage
- Square root of X
- 2.14 summary
- Preliminary cognition of C language pointer
- Basic use of continuous integration server Jenkins
- data2vec! New milestone of unified mode
- Asp access teaching management system design finished product
- Wine travel Jianghu War: Ctrip is strong, meituan is strong, and Tiktok is fighting
- Learning practice: comprehensive application of cycle and branch structure (I)
猜你喜欢

Redis persistence principle

2022 registration of G2 utility boiler stoker examination and G2 utility boiler stoker reexamination examination

2022 chemical automation control instrument examination summary and chemical automation control instrument certificate examination

使用BENCHMARKSQL工具对KingbaseES预热数据时执行:select sys_prewarm(‘NDX_OORDER_2 ‘)报错

使用BENCHMARKSQL工具对KingbaseES执行测试时报错funcs sh file not found
![[dynamic programming] subsequence problem](/img/d8/020ae959ef53ce097d3a81a0d2d63a.jpg)
[dynamic programming] subsequence problem

2022 Shandong Province safety officer C certificate examination content and Shandong Province safety officer C certificate examination questions and analysis

使用BENCHMARKSQL工具对kingbaseES执行灌数据提示无法找到JDBC driver

Auman Galaxy new year of the tiger appreciation meeting was held in Beijing - won the double certification of "intelligent safety" and "efficient performance" of China Automotive Research Institute

Joint search set: the number of points in connected blocks (the number of points in a set)
随机推荐
[Chongqing Guangdong education] reference materials for design and a better life of Zhongyuan Institute of science and technology
Data Lake three swordsmen -- comparative analysis of delta, Hudi and iceberg
FFMpeg filter
带有注意力RPN和多关系检测器的小样本目标检测网络(提供源码和数据及下载)...
Number of uniform strings of leetcode simple problem
UiPath实战(08) - 选取器(Selector)
RSRS index timing and large and small disc rotation
sd卡数据损坏怎么回事,sd卡数据损坏怎么恢复
Dismantle a 100000 yuan BYD "Yuan". Come and see what components are in it.
[literature reading] sparse in deep learning: practicing and growth for effective information and training in NN
Reptile exercise 02
How to use kotlin to improve productivity: kotlin tips
Matplotlib -- save graph
The programmer went to bed at 12 o'clock in the middle of the night, and the leader angrily scolded: go to bed so early, you are very good at keeping fit
[Thesis Writing] how to write the overall design of JSP tourism network
data2vec! New milestone of unified mode
消息队列(MQ)介绍
IPhone x forgot the boot password
[set theory] inclusion exclusion principle (including examples of exclusion principle)
金仓数据库KingbaseES 插件kdb_exists_expand