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

边栏推荐
- Php+mysql registration landing page development complete code
- [set theory] set identities (idempotent law | exchange law | combination law | distribution rate | De Morgan law | absorption rate | zero law | identity | exclusion law | contradiction law | complemen
- vulnhub HA: Natraj
- Why should programmers learn microservice architecture if they want to enter a large factory?
- 2022 registration of G2 utility boiler stoker examination and G2 utility boiler stoker reexamination examination
- A outsourcing boy's mid-2022 summary
- What are the Bluetooth headsets with good sound quality in 2022? Inventory of four high-quality Bluetooth headsets
- 普通本科大学生活避坑指南
- [fxcg] market analysis today
- Dive into deep learning - 2.1 data operation & Exercise
猜你喜欢

Solve BP Chinese garbled code

2022 Shandong Province safety officer C certificate examination content and Shandong Province safety officer C certificate examination questions and analysis

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

What are the Bluetooth headsets with good sound quality in 2022? Inventory of four high-quality Bluetooth headsets

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

Games101 Lesson 9 shading 3 Notes

Two points -leetcode-540 A single element in an ordered array

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

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

Introduction of pointer variables in function parameters
随机推荐
[literature reading] sparse in deep learning: practicing and growth for effective information and training in NN
4 years of experience to interview test development, 10 minutes to end, ask too
GFS分布式文件系统(光是遇见已经很美好了)
使用BENCHMARKSQL工具对KingbaseES执行测试时报错funcs sh file not found
2022-02-12 (338. Bit count)
关于开学的准备与专业认知
Leetcode simple question: check whether two string arrays are equal
GFS distributed file system (it's nice to meet it alone)
General undergraduate college life pit avoidance Guide
Redraw and reflow
Mongodb slow query optimization analysis strategy
使用BENCHMARKSQL工具对KingbaseES预热数据时执行:select sys_prewarm(‘NDX_OORDER_2 ‘)报错
解决bp中文乱码
使用BENCHMARKSQL工具对kingbasees并发测试时kill掉主进程成功后存在子线程未及时关闭
Ffmpeg mix
怎么用Kotlin去提高生产力:Kotlin Tips
Basic use of continuous integration server Jenkins
[文献阅读] Sparsity in Deep Learning: Pruning and growth for efficient inference and training in NN
重绘和回流
Learning practice: comprehensive application of cycle and branch structure (I)