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

边栏推荐
- 有道云笔记
- Games101 Lesson 9 shading 3 Notes
- A outsourcing boy's mid-2022 summary
- Kubernetes源码分析(一)
- P35-P41 fourth_ context
- When using the benchmarksql tool to test the concurrency of kingbasees, there are sub threads that are not closed in time after the main process is killed successfully
- Function introduction of member points mall system
- [set theory] binary relationship (binary relationship notation | binary relationship from a to B | number of binary relationships | example of binary relationship)
- 关于开学的准备与专业认知
- Kingbasees plug-in KDB of Jincang database_ exists_ expand
猜你喜欢

Human resource management system based on JSP

Solve BP Chinese garbled code
![[nlp] - brief introduction to the latest work of spark neural network](/img/65/35ae0137f4030bdb2b0ab9acd85e16.png)
[nlp] - brief introduction to the latest work of spark neural network

Leetcode simple question: check whether two string arrays are equal

2022 a special equipment related management (elevator) analysis and a special equipment related management (elevator) simulation test

Preliminary cognition of C language pointer

FuncS sh file not found when using the benchmarksql tool to test kingbases

X-ray normal based contour rendering
![[free completion] development of course guidance platform (source code +lunwen)](/img/14/7c1c822bda050a805fa7fc25b802a4.jpg)
[free completion] development of course guidance platform (source code +lunwen)

GFS distributed file system (it's nice to meet it alone)
随机推荐
Data Lake three swordsmen -- comparative analysis of delta, Hudi and iceberg
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
RSRS指标择时及大小盘轮动
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
Leetcode simple question: check whether the string is an array prefix
sd卡数据损坏怎么回事,sd卡数据损坏怎么恢复
[fxcg] inflation differences will still lead to the differentiation of monetary policies in various countries
The simple problem of leetcode: dismantling bombs
JS multidimensional array to one-dimensional array
Kubernetes source code analysis (I)
redis 持久化原理
[free completion] development of course guidance platform (source code +lunwen)
一名外包仔的2022年中总结
2022-02-14 (394. String decoding)
金仓数据库KingbaseES 插件kdb_date_function
Leetcode simple question: check whether two string arrays are equal
After reviewing MySQL for a month, I was stunned when the interviewer of Alibaba asked me
Jincang KFS data bidirectional synchronization scenario deployment
Php+mysql registration landing page development complete code
Mount NFS in kubesphere