Skip to content

Commit 8ea0cfa

Browse files
committed
feat: add solutions to lc problem: No.1827
No.1827.Minimum Operations to Make the Array Increasing
1 parent 97473dd commit 8ea0cfa

File tree

7 files changed

+70
-38
lines changed

7 files changed

+70
-38
lines changed

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

Lines changed: 28 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,13 @@
5353

5454
<!-- 这里可写通用的实现逻辑 -->
5555

56-
遍历数组,维护最大值 mx。
56+
**方法一:一次遍历**
57+
58+
我们用变量 $mx$ 记录当前严格递增数组的最大值,初始时 $mx = 0$。
59+
60+
从左到右遍历数组 `nums`,对于当前遍历到的元素 $v$,如果 $v \lt mx + 1$,那么我们需要将其增加到 $mx + 1$,这样才能保证数组严格递增。因此,我们此次需要进行的操作次数为 $max(0, mx + 1 - v)$,累加到答案中,然后更新 $mx=max(mx + 1, v)$。继续遍历下一个元素,直到遍历完整个数组。
61+
62+
时间复杂度 $O(n)$,空间复杂度 $O(1)$。其中 $n$ 为数组 `nums` 的长度。
5763

5864
<!-- tabs:start -->
5965

@@ -64,7 +70,7 @@
6470
```python
6571
class Solution:
6672
def minOperations(self, nums: List[int]) -> int:
67-
mx = ans = 0
73+
ans = mx = 0
6874
for v in nums:
6975
ans += max(0, mx + 1 - v)
7076
mx = max(mx + 1, v)
@@ -78,8 +84,7 @@ class Solution:
7884
```java
7985
class Solution {
8086
public int minOperations(int[] nums) {
81-
int ans = 0;
82-
int mx = 0;
87+
int ans = 0, mx = 0;
8388
for (int v : nums) {
8489
ans += Math.max(0, mx + 1 - v);
8590
mx = Math.max(mx + 1, v);
@@ -95,8 +100,7 @@ class Solution {
95100
class Solution {
96101
public:
97102
int minOperations(vector<int>& nums) {
98-
int ans = 0;
99-
int mx = 0;
103+
int ans = 0, mx = 0;
100104
for (int& v : nums) {
101105
ans += max(0, mx + 1 - v);
102106
mx = max(mx + 1, v);
@@ -109,13 +113,13 @@ public:
109113
### **Go**
110114
111115
```go
112-
func minOperations(nums []int) int {
113-
ans, mx := 0, 0
116+
func minOperations(nums []int) (ans int) {
117+
mx := 0
114118
for _, v := range nums {
115119
ans += max(0, mx+1-v)
116120
mx = max(mx+1, v)
117121
}
118-
return ans
122+
return
119123
}
120124
121125
func max(a, b int) int {
@@ -126,6 +130,21 @@ func max(a, b int) int {
126130
}
127131
```
128132

133+
### **C#**
134+
135+
```cs
136+
public class Solution {
137+
public int MinOperations(int[] nums) {
138+
int ans = 0, mx = 0;
139+
foreach (int v in nums) {
140+
ans += Math.Max(0, mx + 1 - v);
141+
mx = Math.Max(mx + 1, v);
142+
}
143+
return ans;
144+
}
145+
}
146+
```
147+
129148
### **...**
130149

131150
```

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

Lines changed: 22 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -61,11 +61,8 @@
6161
<p><strong>Constraints:</strong></p>
6262

6363
<ul>
64-
6564
<li><code>1 &lt;= nums.length &lt;= 5000</code></li>
66-
6765
<li><code>1 &lt;= nums[i] &lt;= 10<sup>4</sup></code></li>
68-
6966
</ul>
7067

7168
## Solutions
@@ -77,7 +74,7 @@
7774
```python
7875
class Solution:
7976
def minOperations(self, nums: List[int]) -> int:
80-
mx = ans = 0
77+
ans = mx = 0
8178
for v in nums:
8279
ans += max(0, mx + 1 - v)
8380
mx = max(mx + 1, v)
@@ -89,8 +86,7 @@ class Solution:
8986
```java
9087
class Solution {
9188
public int minOperations(int[] nums) {
92-
int ans = 0;
93-
int mx = 0;
89+
int ans = 0, mx = 0;
9490
for (int v : nums) {
9591
ans += Math.max(0, mx + 1 - v);
9692
mx = Math.max(mx + 1, v);
@@ -106,8 +102,7 @@ class Solution {
106102
class Solution {
107103
public:
108104
int minOperations(vector<int>& nums) {
109-
int ans = 0;
110-
int mx = 0;
105+
int ans = 0, mx = 0;
111106
for (int& v : nums) {
112107
ans += max(0, mx + 1 - v);
113108
mx = max(mx + 1, v);
@@ -120,13 +115,13 @@ public:
120115
### **Go**
121116
122117
```go
123-
func minOperations(nums []int) int {
124-
ans, mx := 0, 0
118+
func minOperations(nums []int) (ans int) {
119+
mx := 0
125120
for _, v := range nums {
126121
ans += max(0, mx+1-v)
127122
mx = max(mx+1, v)
128123
}
129-
return ans
124+
return
130125
}
131126
132127
func max(a, b int) int {
@@ -137,6 +132,22 @@ func max(a, b int) int {
137132
}
138133
```
139134

135+
136+
### **C#**
137+
138+
```cs
139+
public class Solution {
140+
public int MinOperations(int[] nums) {
141+
int ans = 0, mx = 0;
142+
foreach (int v in nums) {
143+
ans += Math.Max(0, mx + 1 - v);
144+
mx = Math.Max(mx + 1, v);
145+
}
146+
return ans;
147+
}
148+
}
149+
```
150+
140151
### **...**
141152

142153
```

solution/1800-1899/1827.Minimum Operations to Make the Array Increasing/Solution.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
class Solution {
22
public:
33
int minOperations(vector<int>& nums) {
4-
int ans = 0;
5-
int mx = 0;
4+
int ans = 0, mx = 0;
65
for (int& v : nums) {
76
ans += max(0, mx + 1 - v);
87
mx = max(mx + 1, v);
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
public class Solution {
2+
public int MinOperations(int[] nums) {
3+
int ans = 0, mx = 0;
4+
foreach (int v in nums) {
5+
ans += Math.Max(0, mx + 1 - v);
6+
mx = Math.Max(mx + 1, v);
7+
}
8+
return ans;
9+
}
10+
}

solution/1800-1899/1827.Minimum Operations to Make the Array Increasing/Solution.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
func minOperations(nums []int) int {
2-
ans, mx := 0, 0
1+
func minOperations(nums []int) (ans int) {
2+
mx := 0
33
for _, v := range nums {
44
ans += max(0, mx+1-v)
55
mx = max(mx+1, v)
66
}
7-
return ans
7+
return
88
}
99

1010
func max(a, b int) int {
Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,10 @@
11
class Solution {
22
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-
}
3+
int ans = 0, mx = 0;
4+
for (int v : nums) {
5+
ans += Math.max(0, mx + 1 - v);
6+
mx = Math.max(mx + 1, v);
147
}
15-
return times;
8+
return ans;
169
}
1710
}

solution/1800-1899/1827.Minimum Operations to Make the Array Increasing/Solution.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
class Solution:
22
def minOperations(self, nums: List[int]) -> int:
3-
mx = ans = 0
3+
ans = mx = 0
44
for v in nums:
55
ans += max(0, mx + 1 - v)
66
mx = max(mx + 1, v)

0 commit comments

Comments
 (0)