Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit c12f418

Browse files
feat: add solutions to lc problem: No.2919 (#1906)
No.2919.Minimum Increment Operations to Make Array Beautiful
1 parent f15e271 commit c12f418

File tree

7 files changed

+179
-6
lines changed

7 files changed

+179
-6
lines changed

‎solution/2900-2999/2919.Minimum Increment Operations to Make Array Beautiful/README.md‎

Lines changed: 67 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -75,34 +75,98 @@
7575

7676
<!-- 这里可写通用的实现逻辑 -->
7777

78+
**方法一:动态规划**
79+
80+
我们定义 $f,ドル $g,ドル $h$ 表示前 $i$ 项中,分别以最后三项作为子数组的最大值所需要的最小增量运算数,初始时 $f = 0,ドル $g = 0,ドル $h = 0$。
81+
82+
接下来,我们遍历数组 $nums,ドル对于每个 $x,ドル我们需要更新 $f,ドル $g,ドル $h$ 的值,使其满足题目要求,即:
83+
84+
$$
85+
\begin{aligned}
86+
f' &= g \\
87+
g' &= h \\
88+
h' &= \min(f, g, h) + \max(k - x, 0)
89+
\end{aligned}
90+
$$
91+
92+
最后,我们只需要返回 $f,ドル $g,ドル $h$ 中的最小值即可。
93+
94+
时间复杂度 $O(n),ドル其中 $n$ 为数组长度。空间复杂度 $O(1)$。
95+
7896
<!-- tabs:start -->
7997

8098
### **Python3**
8199

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

84102
```python
85-
103+
class Solution:
104+
def minIncrementOperations(self, nums: List[int], k: int) -> int:
105+
f = g = h = 0
106+
for x in nums:
107+
f, g, h = g, h, min(f, g, h) + max(k - x, 0)
108+
return min(f, g, h)
86109
```
87110

88111
### **Java**
89112

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

92115
```java
93-
116+
class Solution {
117+
public long minIncrementOperations(int[] nums, int k) {
118+
long f = 0, g = 0, h = 0;
119+
for (int x : nums) {
120+
long hh = Math.min(Math.min(f, g), h) + Math.max(k - x, 0);
121+
f = g;
122+
g = h;
123+
h = hh;
124+
}
125+
return Math.min(Math.min(f, g), h);
126+
}
127+
}
94128
```
95129

96130
### **C++**
97131

98132
```cpp
99-
133+
class Solution {
134+
public:
135+
long long minIncrementOperations(vector<int>& nums, int k) {
136+
long long f = 0, g = 0, h = 0;
137+
for (int x : nums) {
138+
long long hh = min({f, g, h}) + max(k - x, 0);
139+
f = g;
140+
g = h;
141+
h = hh;
142+
}
143+
return min({f, g, h});
144+
}
145+
};
100146
```
101147
102148
### **Go**
103149
104150
```go
151+
func minIncrementOperations(nums []int, k int) int64 {
152+
var f, g, h int
153+
for _, x := range nums {
154+
f, g, h = g, h, min(min(f, g), h)+max(k-x, 0)
155+
}
156+
return int64(min(min(f, g), h))
157+
}
158+
```
159+
160+
### **TypeScript**
105161

162+
```ts
163+
function minIncrementOperations(nums: number[], k: number): number {
164+
let [f, g, h] = [0, 0, 0];
165+
for (const x of nums) {
166+
[f, g, h] = [g, h, Math.min(f, g, h) + Math.max(k - x, 0)];
167+
}
168+
return Math.min(f, g, h);
169+
}
106170
```
107171

108172
### **...**

‎solution/2900-2999/2919.Minimum Increment Operations to Make Array Beautiful/README_EN.md‎

Lines changed: 67 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -69,30 +69,94 @@ Hence, the answer is 0.
6969

7070
## Solutions
7171

72+
**Solution 1: Dynamic Programming**
73+
74+
We define $f,ドル $g,ドル and $h$ as the minimum number of increment operations needed to get the maximum value from the last three items in the first $i$ items, initially $f = 0,ドル $g = 0,ドル $h = 0$.
75+
76+
Next, we traverse the array $nums$. For each $x,ドル we need to update the values of $f,ドル $g,ドル and $h$ to meet the requirements of the problem, that is:
77+
78+
$$
79+
\begin{aligned}
80+
f' &= g \\
81+
g' &= h \\
82+
h' &= \min(f, g, h) + \max(k - x, 0)
83+
\end{aligned}
84+
$$
85+
86+
Finally, we only need to return the minimum value among $f,ドル $g,ドル and $h$.
87+
88+
The time complexity is $O(n),ドル where $n$ is the length of the array. The space complexity is $O(1)$.
89+
7290
<!-- tabs:start -->
7391

7492
### **Python3**
7593

7694
```python
77-
95+
class Solution:
96+
def minIncrementOperations(self, nums: List[int], k: int) -> int:
97+
f = g = h = 0
98+
for x in nums:
99+
f, g, h = g, h, min(f, g, h) + max(k - x, 0)
100+
return min(f, g, h)
78101
```
79102

80103
### **Java**
81104

82105
```java
83-
106+
class Solution {
107+
public long minIncrementOperations(int[] nums, int k) {
108+
long f = 0, g = 0, h = 0;
109+
for (int x : nums) {
110+
long hh = Math.min(Math.min(f, g), h) + Math.max(k - x, 0);
111+
f = g;
112+
g = h;
113+
h = hh;
114+
}
115+
return Math.min(Math.min(f, g), h);
116+
}
117+
}
84118
```
85119

86120
### **C++**
87121

88122
```cpp
89-
123+
class Solution {
124+
public:
125+
long long minIncrementOperations(vector<int>& nums, int k) {
126+
long long f = 0, g = 0, h = 0;
127+
for (int x : nums) {
128+
long long hh = min({f, g, h}) + max(k - x, 0);
129+
f = g;
130+
g = h;
131+
h = hh;
132+
}
133+
return min({f, g, h});
134+
}
135+
};
90136
```
91137
92138
### **Go**
93139
94140
```go
141+
func minIncrementOperations(nums []int, k int) int64 {
142+
var f, g, h int
143+
for _, x := range nums {
144+
f, g, h = g, h, min(min(f, g), h)+max(k-x, 0)
145+
}
146+
return int64(min(min(f, g), h))
147+
}
148+
```
149+
150+
### **TypeScript**
95151

152+
```ts
153+
function minIncrementOperations(nums: number[], k: number): number {
154+
let [f, g, h] = [0, 0, 0];
155+
for (const x of nums) {
156+
[f, g, h] = [g, h, Math.min(f, g, h) + Math.max(k - x, 0)];
157+
}
158+
return Math.min(f, g, h);
159+
}
96160
```
97161

98162
### **...**
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
class Solution {
2+
public:
3+
long long minIncrementOperations(vector<int>& nums, int k) {
4+
long long f = 0, g = 0, h = 0;
5+
for (int x : nums) {
6+
long long hh = min({f, g, h}) + max(k - x, 0);
7+
f = g;
8+
g = h;
9+
h = hh;
10+
}
11+
return min({f, g, h});
12+
}
13+
};
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
func minIncrementOperations(nums []int, k int) int64 {
2+
var f, g, h int
3+
for _, x := range nums {
4+
f, g, h = g, h, min(min(f, g), h)+max(k-x, 0)
5+
}
6+
return int64(min(min(f, g), h))
7+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
class Solution {
2+
public long minIncrementOperations(int[] nums, int k) {
3+
long f = 0, g = 0, h = 0;
4+
for (int x : nums) {
5+
long hh = Math.min(Math.min(f, g), h) + Math.max(k - x, 0);
6+
f = g;
7+
g = h;
8+
h = hh;
9+
}
10+
return Math.min(Math.min(f, g), h);
11+
}
12+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
class Solution:
2+
def minIncrementOperations(self, nums: List[int], k: int) -> int:
3+
f = g = h = 0
4+
for x in nums:
5+
f, g, h = g, h, min(f, g, h) + max(k - x, 0)
6+
return min(f, g, h)
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
function minIncrementOperations(nums: number[], k: number): number {
2+
let [f, g, h] = [0, 0, 0];
3+
for (const x of nums) {
4+
[f, g, h] = [g, h, Math.min(f, g, h) + Math.max(k - x, 0)];
5+
}
6+
return Math.min(f, g, h);
7+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /