Leetcode 1827. Minimum Operations to Make the Array Increasing #biweekly contest

preview_player
Показать описание
50th LeetCode Biweekly Contest Problem 1
You are given an integer array nums (0-indexed). In one operation, you can choose an element of the array and increment it by 1.

For example, if nums = [1,2,3], you can choose to increment nums[1] to make nums = [1,3,3].
Return the minimum number of operations needed to make nums strictly increasing.

An array nums is strictly increasing if nums[i] less than nums[i+1] for. An array of length 1 is trivially strictly increasing.



Example 1:

Input: nums = [1,1,1]
Output: 3
Explanation: You can do the following operations:
1) Increment nums[2], so nums becomes [1,1,2].
2) Increment nums[1], so nums becomes [1,2,2].
3) Increment nums[2], so nums becomes [1,2,3].

Рекомендации по теме
Комментарии
Автор

Whenever I have any doubt, I wish that I find your video solution. Your way of explanation is easy to understand.

AnkitKumar-ysvs
Автор

Why didn't you sort the array? And why did it work without sorting?

div
Автор

nice, but if u explain it in ython also, would be great

BoburKamolov-qqxv
Автор

I have been given an array where arr[i]<=a[i+K]. here K is given number. Mini operation required to make any array satisfy this condition.

or

Mini operation required to make non-decresing.

Above is not working as it is for strictly increasing.

raj-nzbj
Автор

we need to sort the array too first because if the array is too large then the no. of operations to make elements unique will also increase but if we sort it we always be iincreasing the array by 1 for every condition

harshfeb
Автор

could you explain how you ensure that is the minimum?

mynameismynameis