Skip to content

Commit a837d6f

Browse files
committed
feat: add solutions to leetcode problem: No.1827. Minimum Operations to Make the Array Increasing
1 parent 2887bab commit a837d6f

File tree

4 files changed

+92
-27
lines changed

4 files changed

+92
-27
lines changed

solution/1800-1899/1827.Minimum Operations to Make the Array Increasing/README.md

+32-3
Original file line numberDiff line numberDiff line change
@@ -49,27 +49,56 @@
4949
<li><code>1 &lt;= nums[i] &lt;= 10<sup>4</sup></code></li>
5050
</ul>
5151

52-
5352
## 解法
5453

5554
<!-- 这里可写通用的实现逻辑 -->
5655

56+
遍历数组,维护一个 preMax 变量。
57+
5758
<!-- tabs:start -->
5859

5960
### **Python3**
6061

6162
<!-- 这里可写当前语言的特殊实现逻辑 -->
6263

6364
```python
64-
65+
class Solution:
66+
def minOperations(self, nums: List[int]) -> int:
67+
n = len(nums)
68+
pre_max = nums[0]
69+
times = 0
70+
for i in range(1, n):
71+
if nums[i] <= pre_max:
72+
steps = pre_max - nums[i] + 1
73+
times += steps
74+
pre_max = nums[i] + steps
75+
else:
76+
pre_max = nums[i]
77+
return times
6578
```
6679

6780
### **Java**
6881

6982
<!-- 这里可写当前语言的特殊实现逻辑 -->
7083

7184
```java
72-
85+
class Solution {
86+
public int minOperations(int[] nums) {
87+
int n = nums.length;
88+
int preMax = nums[0];
89+
int times = 0;
90+
for (int i = 1; i < n; ++i) {
91+
if (nums[i] <= preMax) {
92+
int steps = preMax - nums[i] + 1;
93+
times += steps;
94+
preMax = nums[i] + steps;
95+
} else {
96+
preMax = nums[i];
97+
}
98+
}
99+
return times;
100+
}
101+
}
73102
```
74103

75104
### **...**

solution/1800-1899/1827.Minimum Operations to Make the Array Increasing/README_EN.md

+30-24
Original file line numberDiff line numberDiff line change
@@ -6,28 +6,18 @@
66

77
<p>You are given an integer array <code>nums</code> (<strong>0-indexed</strong>). In one operation, you can choose an element of the array and increment it by <code>1</code>.</p>
88

9-
10-
119
<ul>
1210
<li>For example, if <code>nums = [1,2,3]</code>, you can choose to increment <code>nums[1]</code> to make <code>nums = [1,<u><b>3</b></u>,3]</code>.</li>
1311
</ul>
1412

15-
16-
1713
<p>Return <em>the <strong>minimum</strong> number of operations needed to make</em> <code>nums</code> <em><strong>strictly</strong> <strong>increasing</strong>.</em></p>
1814

19-
20-
2115
<p>An array <code>nums</code> is <strong>strictly increasing</strong> if <code>nums[i] &lt; nums[i+1]</code> for all <code>0 &lt;= i &lt; nums.length - 1</code>. An array of length <code>1</code> is trivially strictly increasing.</p>
2216

23-
24-
2517
<p>&nbsp;</p>
2618

2719
<p><strong>Example 1:</strong></p>
2820

29-
30-
3121
<pre>
3222

3323
<strong>Input:</strong> nums = [1,1,1]
@@ -44,12 +34,8 @@
4434

4535
</pre>
4636

47-
48-
4937
<p><strong>Example 2:</strong></p>
5038

51-
52-
5339
<pre>
5440

5541
<strong>Input:</strong> nums = [1,5,2,4,1]
@@ -58,12 +44,8 @@
5844

5945
</pre>
6046

61-
62-
6347
<p><strong>Example 3:</strong></p>
6448

65-
66-
6749
<pre>
6850

6951
<strong>Input:</strong> nums = [8]
@@ -72,14 +54,10 @@
7254

7355
</pre>
7456

75-
76-
7757
<p>&nbsp;</p>
7858

7959
<p><strong>Constraints:</strong></p>
8060

81-
82-
8361
<ul>
8462
<li><code>1 &lt;= nums.length &lt;= 5000</code></li>
8563
<li><code>1 &lt;= nums[i] &lt;= 10<sup>4</sup></code></li>
@@ -92,13 +70,41 @@
9270
### **Python3**
9371

9472
```python
95-
73+
class Solution:
74+
def minOperations(self, nums: List[int]) -> int:
75+
n = len(nums)
76+
pre_max = nums[0]
77+
times = 0
78+
for i in range(1, n):
79+
if nums[i] <= pre_max:
80+
steps = pre_max - nums[i] + 1
81+
times += steps
82+
pre_max = nums[i] + steps
83+
else:
84+
pre_max = nums[i]
85+
return times
9686
```
9787

9888
### **Java**
9989

10090
```java
101-
91+
class Solution {
92+
public int minOperations(int[] nums) {
93+
int n = nums.length;
94+
int preMax = nums[0];
95+
int times = 0;
96+
for (int i = 1; i < n; ++i) {
97+
if (nums[i] <= preMax) {
98+
int steps = preMax - nums[i] + 1;
99+
times += steps;
100+
preMax = nums[i] + steps;
101+
} else {
102+
preMax = nums[i];
103+
}
104+
}
105+
return times;
106+
}
107+
}
102108
```
103109

104110
### **...**
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
class Solution {
2+
public int minOperations(int[] nums) {
3+
int n = nums.length;
4+
int preMax = nums[0];
5+
int times = 0;
6+
for (int i = 1; i < n; ++i) {
7+
if (nums[i] <= preMax) {
8+
int steps = preMax - nums[i] + 1;
9+
times += steps;
10+
preMax = nums[i] + steps;
11+
} else {
12+
preMax = nums[i];
13+
}
14+
}
15+
return times;
16+
}
17+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
class Solution:
2+
def minOperations(self, nums: List[int]) -> int:
3+
n = len(nums)
4+
pre_max = nums[0]
5+
times = 0
6+
for i in range(1, n):
7+
if nums[i] <= pre_max:
8+
steps = pre_max - nums[i] + 1
9+
times += steps
10+
pre_max = nums[i] + steps
11+
else:
12+
pre_max = nums[i]
13+
return times

0 commit comments

Comments
 (0)