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

边栏推荐
- Mount NFS in kubesphere
- [set theory] binary relationship (special relationship type | empty relationship | identity relationship | global relationship | divisive relationship | size relationship)
- Why should programmers learn microservice architecture if they want to enter a large factory?
- Two drawing interfaces - 1 Matlab style interface
- [set theory] inclusion exclusion principle (including examples of exclusion principle)
- 使用BENCHMARKSQL工具对KingbaseES执行测试时报错funcs sh file not found
- Number of 1 in binary (simple difficulty)
- Smart contract security audit company selection analysis and audit report resources download - domestic article
- Ffmpeg mix
- 2022 P cylinder filling test content and P cylinder filling simulation test questions
猜你喜欢

Php+mysql registration landing page development complete code

Contents of welder (primary) examination and welder (primary) examination in 2022

Use the benchmarksql tool to perform a data prompt on kingbases. The jdbc driver cannot be found

Leetcode simple question: check whether the array is sorted and rotated

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

Leetcode simple question: check whether two string arrays are equal

消息队列(MQ)介绍

Learning practice: comprehensive application of cycle and branch structure (I)

有道云笔记
![[dynamic programming] subsequence problem](/img/d8/020ae959ef53ce097d3a81a0d2d63a.jpg)
[dynamic programming] subsequence problem
随机推荐
General undergraduate college life pit avoidance Guide
Bugku CTF daily question baby_ flag. txt
金仓数据库KingbaseES 插件kdb_date_function
Internationalization and localization, dark mode and dark mode in compose
[dynamic programming] subsequence problem
What's wrong with SD card data damage? How to recover SD card data damage
RSRS index timing and large and small disc rotation
[set theory] inclusion exclusion principle (including examples of exclusion principle)
[fairseq] error: typeerror:_ broadcast_ coalesced(): incompatible function arguments
When using the benchmarksql tool to preheat data for kingbasees, execute: select sys_ Prewarm ('ndx_oorder_2 ') error
JVM原理简介
MC Layer Target
redis 持久化原理
Mongodb slow query optimization analysis strategy
7. Integrated learning
RSRS指标择时及大小盘轮动
【PHP漏洞-弱类型】基础知识、php弱相等、报错绕过
Kingbasees plug-in KDB of Jincang database_ date_ function
Which Bluetooth headset is cost-effective? Four Bluetooth headsets with high cost performance are recommended
The simple problem of leetcode: dismantling bombs