当前位置:网站首页>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

边栏推荐
- Redis persistence principle
- The simple problem of leetcode: dismantling bombs
- How to retrieve the password for opening word files
- GFS分布式文件系统(光是遇见已经很美好了)
- [set theory] binary relation (example of binary relation on a | binary relation on a)
- stm32逆向入门
- [free completion] development of course guidance platform (source code +lunwen)
- 【PHP漏洞-弱类型】基础知识、php弱相等、报错绕过
- Joint search set: the number of points in connected blocks (the number of points in a set)
- 怎么用Kotlin去提高生产力:Kotlin Tips
猜你喜欢

使用BENCHMARKSQL工具对KingbaseES执行测试时报错funcs sh file not found

解决bp中文乱码

Some information about the developer environment in Chengdu

Truncated sentences of leetcode simple questions

Leetcode simple question: check whether the string is an array prefix
![[文献阅读] Sparsity in Deep Learning: Pruning and growth for efficient inference and training in NN](/img/7e/50fa6f65b5a4f0bb60909f57daff56.png)
[文献阅读] Sparsity in Deep Learning: Pruning and growth for efficient inference and training in NN

X-ray normal based contour rendering

Introduction of pointer variables in function parameters

BMZCTF simple_ pop

跨境电商多商户系统怎么选
随机推荐
Smart contract security audit company selection analysis and audit report resources download - domestic article
Integration of Android high-frequency interview questions (including reference answers)
FuncS sh file not found when using the benchmarksql tool to test kingbases
[Thesis Writing] how to write the overall design of JSP tourism network
P35-P41 fourth_ context
金仓数据库KingbaseES 插件kdb_exists_expand
Leetcode simple problem delete an element to strictly increment the array
Ffmpeg tanscoding transcoding
智能合约安全审计公司选型分析和审计报告资源下载---国内篇
[set theory] ordered pair (ordered pair | ordered triple | ordered n ancestor)
Kubernetes source code analysis (I)
[文献阅读] Sparsity in Deep Learning: Pruning and growth for efficient inference and training in NN
Youdao cloud notes
Contents of welder (primary) examination and welder (primary) examination in 2022
[BMZCTF-pwn] 20-secret_ file
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
GFS分布式文件系统(光是遇见已经很美好了)
C Primer Plus Chapter 10, question 14 3 × 5 array
Redraw and reflow
商城系统搭建完成后需要设置哪些功能