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

边栏推荐
- After job hopping at the end of the year, I interviewed more than 30 companies in two weeks and finally landed
- Joint set search: merge intervals and ask whether two numbers are in the same set
- [set theory] binary relationship (special relationship type | empty relationship | identity relationship | global relationship | divisive relationship | size relationship)
- MC Layer Target
- 重绘和回流
- 2022-02-14 (394. String decoding)
- 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
- Small program animation realizes the running lantern and animation object
- GFS distributed file system (it's nice to meet it alone)
- Why should programmers learn microservice architecture if they want to enter a large factory?
猜你喜欢

JVM原理简介

Leetcode simple question: check whether the string is an array prefix

使用BENCHMARKSQL工具对kingbasees并发测试时kill掉主进程成功后存在子线程未及时关闭

Data Lake three swordsmen -- comparative analysis of delta, Hudi and iceberg

Dismantle a 100000 yuan BYD "Yuan". Come and see what components are in it.

Prefix and (continuously updated)

Triangular rasterization

带有注意力RPN和多关系检测器的小样本目标检测网络(提供源码和数据及下载)...

C language series - Section 3 - functions

Leetcode simple question: check whether the array is sorted and rotated
随机推荐
[literature reading] sparse in deep learning: practicing and growth for effective information and training in NN
Kingbasees plug-in KDB of Jincang database_ date_ function
How do you use lodash linking function- How do you chain functions using lodash?
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
逆袭大学生的职业规划
Priv-app permission异常
[fxcg] market analysis today
7. Integrated learning
[set theory] ordered pair (ordered pair | ordered triple | ordered n ancestor)
Some information about the developer environment in Chengdu
Introduction of pointer variables in function parameters
[Thesis Writing] how to write the overall design of JSP tourism network
JS multidimensional array to one-dimensional array
Ffmpeg mix
Two drawing interfaces - 1 Matlab style interface
MongoDB 慢查询语句优化分析策略
[dynamic programming] subsequence problem
Know that Chuangyu cloud monitoring - scanv Max update: Ecology OA unauthorized server request forgery and other two vulnerabilities can be detected
vulnhub HA: Natraj
Joint set search: merge intervals and ask whether two numbers are in the same set