File tree 5 files changed +132
-58
lines changed
solution/1800-1899/1827.Minimum Operations to Make the Array Increasing
5 files changed +132
-58
lines changed Original file line number Diff line number Diff line change 53
53
54
54
<!-- 这里可写通用的实现逻辑 -->
55
55
56
- 遍历数组,维护一个 preMax 变量 。
56
+ 遍历数组,维护最大值 mx 。
57
57
58
58
<!-- tabs:start -->
59
59
64
64
``` python
65
65
class Solution :
66
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
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
78
72
```
79
73
80
74
### ** Java**
@@ -84,23 +78,55 @@ class Solution:
84
78
``` java
85
79
class Solution {
86
80
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);
98
86
}
99
- return times ;
87
+ return ans ;
100
88
}
101
89
}
102
90
```
103
91
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
+
104
130
### ** ...**
105
131
106
132
```
Original file line number Diff line number Diff line change 72
72
``` python
73
73
class Solution :
74
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
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
86
80
```
87
81
88
82
### ** Java**
89
83
90
84
``` java
91
85
class Solution {
92
86
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);
104
92
}
105
- return times ;
93
+ return ans ;
106
94
}
107
95
}
108
96
```
109
97
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
+
110
136
### ** ...**
111
137
112
138
```
Original file line number Diff line number Diff line change
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 number Diff line number Diff line change
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 number Diff line number Diff line change 1
1
class Solution :
2
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
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
You can’t perform that action at this time.
0 commit comments