Skip to content

Commit b1fe6a2

Browse files
authored
feat: add solutions to lc problem: No.3229 (#3300)
No.3229.Minimum Operations to Make Array Equal to Target
1 parent 5e25e93 commit b1fe6a2

File tree

9 files changed

+319
-10
lines changed

9 files changed

+319
-10
lines changed

solution/1500-1599/1526.Minimum Number of Increments on Subarrays to Form a Target Array/README.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,11 @@ tags:
9191

9292
我们注意到 $f[i]$ 只与 $f[i-1]$ 有关,因此可以只用一个变量来维护操作次数。
9393

94-
时间复杂度 $O(n)$,空间复杂度 $O(1)$。其中 $n$ 为数组 $target$ 的长度。
94+
时间复杂度 $O(n)$,其中 $n$ 为数组 $target$ 的长度。空间复杂度 $O(1)$。
95+
96+
相似题目:
97+
98+
- [3229. 使数组等于目标数组所需的最少操作次数](https://github.com/doocs/leetcode/blob/main/solution/3200-3299/3229.Minimum%20Operations%20to%20Make%20Array%20Equal%20to%20Target/README.md)
9599

96100
<!-- tabs:start -->
97101

solution/1500-1599/1526.Minimum Number of Increments on Subarrays to Form a Target Array/README_EN.md

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,21 @@ tags:
7373

7474
<!-- solution:start -->
7575

76-
### Solution 1
76+
### Solution 1: Dynamic Programming
77+
78+
We define $f[i]$ as the minimum number of operations required to obtain $target[0,..i]$, initially setting $f[0] = target[0]$.
79+
80+
For $target[i]$, if $target[i] \leq target[i-1]$, then $f[i] = f[i-1]$; otherwise, $f[i] = f[i-1] + target[i] - target[i-1]$.
81+
82+
The final answer is $f[n-1]$.
83+
84+
We notice that $f[i]$ only depends on $f[i-1]$, so we can maintain the operation count using just one variable.
85+
86+
The time complexity is $O(n)$, where $n$ is the length of the array $target$. The space complexity is $O(1)$.
87+
88+
Similar problems:
89+
90+
- [3229. Minimum Operations to Make Array Equal to Target](https://github.com/doocs/leetcode/blob/main/solution/3200-3299/3229.Minimum%20Operations%20to%20Make%20Array%20Equal%20to%20Target/README_EN.md)
7791

7892
<!-- tabs:start -->
7993

solution/3200-3299/3229.Minimum Operations to Make Array Equal to Target/README.md

Lines changed: 104 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -68,32 +68,132 @@ edit_url: https://github.com/doocs/leetcode/edit/main/solution/3200-3299/3229.Mi
6868

6969
<!-- solution:start -->
7070

71-
### 方法一
71+
### 方法一:动态规划
72+
73+
我们可以先计算出 $\textit{nums}$ 和 $\textit{target}$ 两个数组的差值,然后对于一个差值数组,我们找出连续的差值符号相同的区间,然后对于每个区间,我们将第一个元素的绝对值加到结果中,然后对于后面的元素,如果差值的绝对值比前一个差值的绝对值大,那么我们将绝对值的差值加到结果中。
74+
75+
时间复杂度 $O(n)$,其中 $n$ 为数组 $\textit{nums}$ 的长度。空间复杂度 $O(1)$。
76+
77+
相似题目:
78+
79+
- [1526. 形成目标数组的子数组最少增加次数](https://github.com/doocs/leetcode/tree/main/solution/1500-1599/1526.Minimum%20Number%20of%20Increments%20on%20Subarrays%20to%20Form%20a%20Target%20Array/README.md)
7280

7381
<!-- tabs:start -->
7482

7583
#### Python3
7684

7785
```python
78-
86+
class Solution:
87+
def minimumOperations(self, nums: List[int], target: List[int]) -> int:
88+
n = len(nums)
89+
f = abs(target[0] - nums[0])
90+
for i in range(1, n):
91+
x = target[i] - nums[i]
92+
y = target[i - 1] - nums[i - 1]
93+
if x * y > 0:
94+
d = abs(x) - abs(y)
95+
if d > 0:
96+
f += d
97+
else:
98+
f += abs(x)
99+
return f
79100
```
80101

81102
#### Java
82103

83104
```java
84-
105+
class Solution {
106+
public long minimumOperations(int[] nums, int[] target) {
107+
long f = Math.abs(target[0] - nums[0]);
108+
for (int i = 1; i < nums.length; ++i) {
109+
long x = target[i] - nums[i];
110+
long y = target[i - 1] - nums[i - 1];
111+
if (x * y > 0) {
112+
long d = Math.abs(x) - Math.abs(y);
113+
if (d > 0) {
114+
f += d;
115+
}
116+
} else {
117+
f += Math.abs(x);
118+
}
119+
}
120+
return f;
121+
}
122+
}
85123
```
86124

87125
#### C++
88126

89127
```cpp
90-
128+
class Solution {
129+
public:
130+
long long minimumOperations(vector<int>& nums, vector<int>& target) {
131+
using ll = long long;
132+
ll f = abs(target[0] - nums[0]);
133+
for (int i = 1; i < nums.size(); ++i) {
134+
long x = target[i] - nums[i];
135+
long y = target[i - 1] - nums[i - 1];
136+
if (x * y > 0) {
137+
ll d = abs(x) - abs(y);
138+
if (d > 0) {
139+
f += d;
140+
}
141+
} else {
142+
f += abs(x);
143+
}
144+
}
145+
return f;
146+
}
147+
};
91148
```
92149
93150
#### Go
94151
95152
```go
153+
func minimumOperations(nums []int, target []int) int64 {
154+
f := abs(target[0] - nums[0])
155+
for i := 1; i < len(target); i++ {
156+
x := target[i] - nums[i]
157+
y := target[i-1] - nums[i-1]
158+
if x*y > 0 {
159+
if d := abs(x) - abs(y); d > 0 {
160+
f += d
161+
}
162+
} else {
163+
f += abs(x)
164+
}
165+
}
166+
return int64(f)
167+
}
168+
169+
func abs(x int) int {
170+
if x < 0 {
171+
return -x
172+
}
173+
return x
174+
}
175+
```
96176

177+
#### TypeScript
178+
179+
```ts
180+
function minimumOperations(nums: number[], target: number[]): number {
181+
const n = nums.length;
182+
let f = Math.abs(target[0] - nums[0]);
183+
for (let i = 1; i < n; ++i) {
184+
const x = target[i] - nums[i];
185+
const y = target[i - 1] - nums[i - 1];
186+
if (x * y > 0) {
187+
const d = Math.abs(x) - Math.abs(y);
188+
if (d > 0) {
189+
f += d;
190+
}
191+
} else {
192+
f += Math.abs(x);
193+
}
194+
}
195+
return f;
196+
}
97197
```
98198

99199
<!-- tabs:end -->

solution/3200-3299/3229.Minimum Operations to Make Array Equal to Target/README_EN.md

Lines changed: 104 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -66,32 +66,132 @@ edit_url: https://github.com/doocs/leetcode/edit/main/solution/3200-3299/3229.Mi
6666

6767
<!-- solution:start -->
6868

69-
### Solution 1
69+
### Solution 1: Dynamic Programming
70+
71+
We can first calculate the difference between the arrays $\textit{nums}$ and $\textit{target}$. For a difference array, we find continuous intervals where the signs of the differences are the same. For each interval, we add the absolute value of the first element to the result. For the subsequent elements, if the absolute value of the difference is greater than the absolute value of the previous difference, we add the difference of the absolute values to the result.
72+
73+
The time complexity is $O(n)$, where $n$ is the length of the array $\textit{nums}$. The space complexity is $O(1)$.
74+
75+
Similar problems:
76+
77+
- [1526. Minimum Number of Increments on Subarrays to Form a Target Array](https://github.com/doocs/leetcode/tree/main/solution/1500-1599/1526.Minimum%20Number%20of%20Increments%20on%20Subarrays%20to%20Form%20a%20Target%20Array/README_EN.md)
7078

7179
<!-- tabs:start -->
7280

7381
#### Python3
7482

7583
```python
76-
84+
class Solution:
85+
def minimumOperations(self, nums: List[int], target: List[int]) -> int:
86+
n = len(nums)
87+
f = abs(target[0] - nums[0])
88+
for i in range(1, n):
89+
x = target[i] - nums[i]
90+
y = target[i - 1] - nums[i - 1]
91+
if x * y > 0:
92+
d = abs(x) - abs(y)
93+
if d > 0:
94+
f += d
95+
else:
96+
f += abs(x)
97+
return f
7798
```
7899

79100
#### Java
80101

81102
```java
82-
103+
class Solution {
104+
public long minimumOperations(int[] nums, int[] target) {
105+
long f = Math.abs(target[0] - nums[0]);
106+
for (int i = 1; i < nums.length; ++i) {
107+
long x = target[i] - nums[i];
108+
long y = target[i - 1] - nums[i - 1];
109+
if (x * y > 0) {
110+
long d = Math.abs(x) - Math.abs(y);
111+
if (d > 0) {
112+
f += d;
113+
}
114+
} else {
115+
f += Math.abs(x);
116+
}
117+
}
118+
return f;
119+
}
120+
}
83121
```
84122

85123
#### C++
86124

87125
```cpp
88-
126+
class Solution {
127+
public:
128+
long long minimumOperations(vector<int>& nums, vector<int>& target) {
129+
using ll = long long;
130+
ll f = abs(target[0] - nums[0]);
131+
for (int i = 1; i < nums.size(); ++i) {
132+
long x = target[i] - nums[i];
133+
long y = target[i - 1] - nums[i - 1];
134+
if (x * y > 0) {
135+
ll d = abs(x) - abs(y);
136+
if (d > 0) {
137+
f += d;
138+
}
139+
} else {
140+
f += abs(x);
141+
}
142+
}
143+
return f;
144+
}
145+
};
89146
```
90147
91148
#### Go
92149
93150
```go
151+
func minimumOperations(nums []int, target []int) int64 {
152+
f := abs(target[0] - nums[0])
153+
for i := 1; i < len(target); i++ {
154+
x := target[i] - nums[i]
155+
y := target[i-1] - nums[i-1]
156+
if x*y > 0 {
157+
if d := abs(x) - abs(y); d > 0 {
158+
f += d
159+
}
160+
} else {
161+
f += abs(x)
162+
}
163+
}
164+
return int64(f)
165+
}
166+
167+
func abs(x int) int {
168+
if x < 0 {
169+
return -x
170+
}
171+
return x
172+
}
173+
```
94174

175+
#### TypeScript
176+
177+
```ts
178+
function minimumOperations(nums: number[], target: number[]): number {
179+
const n = nums.length;
180+
let f = Math.abs(target[0] - nums[0]);
181+
for (let i = 1; i < n; ++i) {
182+
const x = target[i] - nums[i];
183+
const y = target[i - 1] - nums[i - 1];
184+
if (x * y > 0) {
185+
const d = Math.abs(x) - Math.abs(y);
186+
if (d > 0) {
187+
f += d;
188+
}
189+
} else {
190+
f += Math.abs(x);
191+
}
192+
}
193+
return f;
194+
}
95195
```
96196

97197
<!-- tabs:end -->
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
class Solution {
2+
public:
3+
long long minimumOperations(vector<int>& nums, vector<int>& target) {
4+
using ll = long long;
5+
ll f = abs(target[0] - nums[0]);
6+
for (int i = 1; i < nums.size(); ++i) {
7+
long x = target[i] - nums[i];
8+
long y = target[i - 1] - nums[i - 1];
9+
if (x * y > 0) {
10+
ll d = abs(x) - abs(y);
11+
if (d > 0) {
12+
f += d;
13+
}
14+
} else {
15+
f += abs(x);
16+
}
17+
}
18+
return f;
19+
}
20+
};
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
func minimumOperations(nums []int, target []int) int64 {
2+
f := abs(target[0] - nums[0])
3+
for i := 1; i < len(target); i++ {
4+
x := target[i] - nums[i]
5+
y := target[i-1] - nums[i-1]
6+
if x*y > 0 {
7+
if d := abs(x) - abs(y); d > 0 {
8+
f += d
9+
}
10+
} else {
11+
f += abs(x)
12+
}
13+
}
14+
return int64(f)
15+
}
16+
17+
func abs(x int) int {
18+
if x < 0 {
19+
return -x
20+
}
21+
return x
22+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
class Solution {
2+
public long minimumOperations(int[] nums, int[] target) {
3+
long f = Math.abs(target[0] - nums[0]);
4+
for (int i = 1; i < nums.length; ++i) {
5+
long x = target[i] - nums[i];
6+
long y = target[i - 1] - nums[i - 1];
7+
if (x * y > 0) {
8+
long d = Math.abs(x) - Math.abs(y);
9+
if (d > 0) {
10+
f += d;
11+
}
12+
} else {
13+
f += Math.abs(x);
14+
}
15+
}
16+
return f;
17+
}
18+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
class Solution:
2+
def minimumOperations(self, nums: List[int], target: List[int]) -> int:
3+
n = len(nums)
4+
f = abs(target[0] - nums[0])
5+
for i in range(1, n):
6+
x = target[i] - nums[i]
7+
y = target[i - 1] - nums[i - 1]
8+
if x * y > 0:
9+
d = abs(x) - abs(y)
10+
if d > 0:
11+
f += d
12+
else:
13+
f += abs(x)
14+
return f

0 commit comments

Comments
 (0)