Skip to content

Commit ade0f78

Browse files
authored
feat: add solutions to lc problem: No.1827
No.1827.Minimum Operations to Make the Array Increasing
1 parent a567038 commit ade0f78

File tree

5 files changed

+132
-58
lines changed

5 files changed

+132
-58
lines changed

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

+50-24
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@
5353

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

56-
遍历数组,维护一个 preMax 变量
56+
遍历数组,维护最大值 mx
5757

5858
<!-- tabs:start -->
5959

@@ -64,17 +64,11 @@
6464
```python
6565
class Solution:
6666
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
67+
mx = ans = 0
68+
for v in nums:
69+
ans += max(0, mx + 1 - v)
70+
mx = max(mx + 1, v)
71+
return ans
7872
```
7973

8074
### **Java**
@@ -84,23 +78,55 @@ class Solution:
8478
```java
8579
class Solution {
8680
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-
}
81+
int ans = 0;
82+
int mx = 0;
83+
for (int v : nums) {
84+
ans += Math.max(0, mx + 1 - v);
85+
mx = Math.max(mx + 1, v);
9886
}
99-
return times;
87+
return ans;
10088
}
10189
}
10290
```
10391

92+
### **C++**
93+
94+
```cpp
95+
class Solution {
96+
public:
97+
int minOperations(vector<int>& nums) {
98+
int ans = 0;
99+
int mx = 0;
100+
for (int& v : nums)
101+
{
102+
ans += max(0, mx + 1 - v);
103+
mx = max(mx + 1, v);
104+
}
105+
return ans;
106+
}
107+
};
108+
```
109+
110+
### **Go**
111+
112+
```go
113+
func minOperations(nums []int) int {
114+
ans, mx := 0, 0
115+
for _, v := range nums {
116+
ans += max(0, mx+1-v)
117+
mx = max(mx+1, v)
118+
}
119+
return ans
120+
}
121+
122+
func max(a, b int) int {
123+
if a > b {
124+
return a
125+
}
126+
return b
127+
}
128+
```
129+
104130
### **...**
105131

106132
```

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

+49-23
Original file line numberDiff line numberDiff line change
@@ -72,41 +72,67 @@
7272
```python
7373
class Solution:
7474
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
75+
mx = ans = 0
76+
for v in nums:
77+
ans += max(0, mx + 1 - v)
78+
mx = max(mx + 1, v)
79+
return ans
8680
```
8781

8882
### **Java**
8983

9084
```java
9185
class Solution {
9286
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-
}
87+
int ans = 0;
88+
int mx = 0;
89+
for (int v : nums) {
90+
ans += Math.max(0, mx + 1 - v);
91+
mx = Math.max(mx + 1, v);
10492
}
105-
return times;
93+
return ans;
10694
}
10795
}
10896
```
10997

98+
### **C++**
99+
100+
```cpp
101+
class Solution {
102+
public:
103+
int minOperations(vector<int>& nums) {
104+
int ans = 0;
105+
int mx = 0;
106+
for (int& v : nums)
107+
{
108+
ans += max(0, mx + 1 - v);
109+
mx = max(mx + 1, v);
110+
}
111+
return ans;
112+
}
113+
};
114+
```
115+
116+
### **Go**
117+
118+
```go
119+
func minOperations(nums []int) int {
120+
ans, mx := 0, 0
121+
for _, v := range nums {
122+
ans += max(0, mx+1-v)
123+
mx = max(mx+1, v)
124+
}
125+
return ans
126+
}
127+
128+
func max(a, b int) int {
129+
if a > b {
130+
return a
131+
}
132+
return b
133+
}
134+
```
135+
110136
### **...**
111137

112138
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
class Solution {
2+
public:
3+
int minOperations(vector<int>& nums) {
4+
int ans = 0;
5+
int mx = 0;
6+
for (int& v : nums)
7+
{
8+
ans += max(0, mx + 1 - v);
9+
mx = max(mx + 1, v);
10+
}
11+
return ans;
12+
}
13+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
func minOperations(nums []int) int {
2+
ans, mx := 0, 0
3+
for _, v := range nums {
4+
ans += max(0, mx+1-v)
5+
mx = max(mx+1, v)
6+
}
7+
return ans
8+
}
9+
10+
func max(a, b int) int {
11+
if a > b {
12+
return a
13+
}
14+
return b
15+
}
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,7 @@
11
class Solution:
22
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
3+
mx = ans = 0
4+
for v in nums:
5+
ans += max(0, mx + 1 - v)
6+
mx = max(mx + 1, v)
7+
return ans

0 commit comments

Comments
 (0)