当前位置:网站首页>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
边栏推荐
- Joint set search: merge intervals and ask whether two numbers are in the same set
- Ffmpeg mix
- Preliminary cognition of C language pointer
- C language series - Section 3 - functions
- Why should programmers learn microservice architecture if they want to enter a large factory?
- 因子选股-打分模型
- vulnhub HA: Natraj
- 2022 new examination questions for the main principals of hazardous chemical business units and examination skills for the main principals of hazardous chemical business units
- 金仓数据库KingbaseES 插件kdb_date_function
- Pyqt control part (II)
猜你喜欢
[free completion] development of course guidance platform (source code +lunwen)
How to retrieve the password for opening word files
BMZCTF simple_ pop
FuncS sh file not found when using the benchmarksql tool to test kingbases
跨境电商多商户系统怎么选
Introduction of pointer variables in function parameters
[Thesis Writing] how to write the overall design of JSP tourism network
UiPath实战(08) - 选取器(Selector)
Contents of welder (primary) examination and welder (primary) examination in 2022
Truncated sentences of leetcode simple questions
随机推荐
【PHP漏洞-弱类型】基础知识、php弱相等、报错绕过
Dive into deep learning - 2.1 data operation & Exercise
[set theory] ordered pair (ordered pair | ordered triple | ordered n ancestor)
sd卡数据损坏怎么回事,sd卡数据损坏怎么恢复
多板块轮动策略编写技巧----策略编写学习教材
会员积分商城系统的功能介绍
Priv-app permission异常
消息队列(MQ)介绍
2022-02-14 (394. String decoding)
Ffmpeg tanscoding transcoding
怎么用Kotlin去提高生产力:Kotlin Tips
[set theory] binary relationship (definition field | value field | inverse operation | inverse synthesis operation | restriction | image | single root | single value | nature of synthesis operation)
[dynamic programming] subsequence problem
I've been in software testing for 8 years and worked as a test leader for 3 years. I can also be a programmer if I'm not a professional
Function introduction of member points mall system
使用BENCHMARKSQL工具对kingbaseES执行灌数据提示无法找到JDBC driver
FFMpeg example
Internationalization and localization, dark mode and dark mode in compose
2022-02-13 (347. Top k high frequency elements)
Web - Information Collection