当前位置:网站首页>LeetCode 1827. Increment array with minimal operation
LeetCode 1827. Increment array with minimal operation
2022-07-01 03:52:00 【Daylight629】
1827. The least operation increments the array
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 itnums[1]obtainnums = [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 :
1) increase nums[2] , The array becomes [1,1,2] .
2) increase nums[1] , The array becomes [1,2,2] .
3) 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 <= 50001 <= nums[i] <= 104
Two 、 Method 1
simulation
class Solution {
public int minOperations(int[] nums) {
if (nums.length == 1) {
return 0;
}
int res = 0;
for (int i = 0; i < nums.length; i++) {
if (i + 1 < nums.length && nums[i + 1] <= nums[i]) {
res += (nums[i] + 1 - nums[i + 1]);
nums[i + 1] = nums[i] + 1;
}
}
return res;
}
}
Complexity analysis
Time complexity :O(n).
Spatial complexity :O(1).
边栏推荐
- JMeter学习笔记2-图形界面简单介绍
- 【TA-霜狼_may-《百人计划》】2.4 传统经验光照模型
- What does ft mean in the data book table
- JMeter login failure, extracting login token, and obtaining token problem solving
- 187. 重复的DNA序列
- Edge浏览器的小技巧:Enter+Ctrl可以自动将地址栏转换为网址
- [TA frost wolf \u may- hundred people plan] 2.3 introduction to common functions
- 【EI会议】2022年国际土木与海洋工程联合会议(JCCME 2022)
- 复习专栏之---消息队列
- 线程常用方法与守护线程
猜你喜欢

C语言的sem_t变量类型

整合阿里云短信的问题:无法从静态上下文中引用非静态方法

Libevent Library Learning
![[ta- frost wolf \u may- hundred people plan] 1.1 rendering pipeline](/img/af/4498382bc47d8c9ae41c407b9d1265.png)
[ta- frost wolf \u may- hundred people plan] 1.1 rendering pipeline

Error: plug ins declaring extensions or extension points must set the singleton directive to true

How keil displays Chinese annotations (simple with pictures)

Deep learning | rnn/lstm of naturallanguageprocessing

The difference between MFC for static libraries and MFC for shared libraries

Explain spark operation mode in detail (local+standalone+yarn)

程序员女友给我做了一个疲劳驾驶检测
随机推荐
Quickly filter data such as clock in time and date: Excel filter to find whether a certain time point is within a certain time period
Appium automation test foundation -- supplement: c/s architecture and b/s architecture description
【TA-霜狼_may-《百人计划》】1.2.2 矩阵计算
[TA frost wolf \u may - "hundred people plan"] 2.1 color space
8. 字符串转换整数 (atoi)
[ta- frost wolf \u may- hundred people plan] 2.2 model and material space
214. minimum palindrome string
Pytorch training deep learning network settings CUDA specified GPU visible
【快捷键】
AfxMessageBox和MessageBox的用法
10. 正则表达式匹配
All in one 1086: Jiaogu conjecture
205. isomorphic string
Jeecgboot output log, how to use @slf4j
168. excel table column name
浏览器顶部loading(来自知乎)
The problem of integrating Alibaba cloud SMS: non static methods cannot be referenced from the static context
409. 最长回文串
【TA-霜狼_may-《百人计划》】2.4 传统经验光照模型
Leetcode: offer 59 - I. maximum value of sliding window