Skip to content

feat: add solutions to lc problem: No.3229 #3300

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,11 @@ tags:

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

时间复杂度 $O(n)$,空间复杂度 $O(1)$。其中 $n$ 为数组 $target$ 的长度。
时间复杂度 $O(n)$,其中 $n$ 为数组 $target$ 的长度。空间复杂度 $O(1)$。

相似题目:

- [3229. 使数组等于目标数组所需的最少操作次数](https://github.com/doocs/leetcode/blob/main/solution/3200-3299/3229.Minimum%20Operations%20to%20Make%20Array%20Equal%20to%20Target/README.md)

<!-- tabs:start -->

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,21 @@ tags:

<!-- solution:start -->

### Solution 1
### Solution 1: Dynamic Programming

We define $f[i]$ as the minimum number of operations required to obtain $target[0,..i]$, initially setting $f[0] = target[0]$.

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]$.

The final answer is $f[n-1]$.

We notice that $f[i]$ only depends on $f[i-1]$, so we can maintain the operation count using just one variable.

The time complexity is $O(n)$, where $n$ is the length of the array $target$. The space complexity is $O(1)$.

Similar problems:

- [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)

<!-- tabs:start -->

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,32 +68,132 @@ edit_url: https://github.com/doocs/leetcode/edit/main/solution/3200-3299/3229.Mi

<!-- solution:start -->

### 方法一
### 方法一:动态规划

我们可以先计算出 $\textit{nums}$ 和 $\textit{target}$ 两个数组的差值,然后对于一个差值数组,我们找出连续的差值符号相同的区间,然后对于每个区间,我们将第一个元素的绝对值加到结果中,然后对于后面的元素,如果差值的绝对值比前一个差值的绝对值大,那么我们将绝对值的差值加到结果中。

时间复杂度 $O(n)$,其中 $n$ 为数组 $\textit{nums}$ 的长度。空间复杂度 $O(1)$。

相似题目:

- [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)

<!-- tabs:start -->

#### Python3

```python

class Solution:
def minimumOperations(self, nums: List[int], target: List[int]) -> int:
n = len(nums)
f = abs(target[0] - nums[0])
for i in range(1, n):
x = target[i] - nums[i]
y = target[i - 1] - nums[i - 1]
if x * y > 0:
d = abs(x) - abs(y)
if d > 0:
f += d
else:
f += abs(x)
return f
```

#### Java

```java

class Solution {
public long minimumOperations(int[] nums, int[] target) {
long f = Math.abs(target[0] - nums[0]);
for (int i = 1; i < nums.length; ++i) {
long x = target[i] - nums[i];
long y = target[i - 1] - nums[i - 1];
if (x * y > 0) {
long d = Math.abs(x) - Math.abs(y);
if (d > 0) {
f += d;
}
} else {
f += Math.abs(x);
}
}
return f;
}
}
```

#### C++

```cpp

class Solution {
public:
long long minimumOperations(vector<int>& nums, vector<int>& target) {
using ll = long long;
ll f = abs(target[0] - nums[0]);
for (int i = 1; i < nums.size(); ++i) {
long x = target[i] - nums[i];
long y = target[i - 1] - nums[i - 1];
if (x * y > 0) {
ll d = abs(x) - abs(y);
if (d > 0) {
f += d;
}
} else {
f += abs(x);
}
}
return f;
}
};
```

#### Go

```go
func minimumOperations(nums []int, target []int) int64 {
f := abs(target[0] - nums[0])
for i := 1; i < len(target); i++ {
x := target[i] - nums[i]
y := target[i-1] - nums[i-1]
if x*y > 0 {
if d := abs(x) - abs(y); d > 0 {
f += d
}
} else {
f += abs(x)
}
}
return int64(f)
}

func abs(x int) int {
if x < 0 {
return -x
}
return x
}
```

#### TypeScript

```ts
function minimumOperations(nums: number[], target: number[]): number {
const n = nums.length;
let f = Math.abs(target[0] - nums[0]);
for (let i = 1; i < n; ++i) {
const x = target[i] - nums[i];
const y = target[i - 1] - nums[i - 1];
if (x * y > 0) {
const d = Math.abs(x) - Math.abs(y);
if (d > 0) {
f += d;
}
} else {
f += Math.abs(x);
}
}
return f;
}
```

<!-- tabs:end -->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,32 +66,132 @@ edit_url: https://github.com/doocs/leetcode/edit/main/solution/3200-3299/3229.Mi

<!-- solution:start -->

### Solution 1
### Solution 1: Dynamic Programming

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.

The time complexity is $O(n)$, where $n$ is the length of the array $\textit{nums}$. The space complexity is $O(1)$.

Similar problems:

- [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)

<!-- tabs:start -->

#### Python3

```python

class Solution:
def minimumOperations(self, nums: List[int], target: List[int]) -> int:
n = len(nums)
f = abs(target[0] - nums[0])
for i in range(1, n):
x = target[i] - nums[i]
y = target[i - 1] - nums[i - 1]
if x * y > 0:
d = abs(x) - abs(y)
if d > 0:
f += d
else:
f += abs(x)
return f
```

#### Java

```java

class Solution {
public long minimumOperations(int[] nums, int[] target) {
long f = Math.abs(target[0] - nums[0]);
for (int i = 1; i < nums.length; ++i) {
long x = target[i] - nums[i];
long y = target[i - 1] - nums[i - 1];
if (x * y > 0) {
long d = Math.abs(x) - Math.abs(y);
if (d > 0) {
f += d;
}
} else {
f += Math.abs(x);
}
}
return f;
}
}
```

#### C++

```cpp

class Solution {
public:
long long minimumOperations(vector<int>& nums, vector<int>& target) {
using ll = long long;
ll f = abs(target[0] - nums[0]);
for (int i = 1; i < nums.size(); ++i) {
long x = target[i] - nums[i];
long y = target[i - 1] - nums[i - 1];
if (x * y > 0) {
ll d = abs(x) - abs(y);
if (d > 0) {
f += d;
}
} else {
f += abs(x);
}
}
return f;
}
};
```

#### Go

```go
func minimumOperations(nums []int, target []int) int64 {
f := abs(target[0] - nums[0])
for i := 1; i < len(target); i++ {
x := target[i] - nums[i]
y := target[i-1] - nums[i-1]
if x*y > 0 {
if d := abs(x) - abs(y); d > 0 {
f += d
}
} else {
f += abs(x)
}
}
return int64(f)
}

func abs(x int) int {
if x < 0 {
return -x
}
return x
}
```

#### TypeScript

```ts
function minimumOperations(nums: number[], target: number[]): number {
const n = nums.length;
let f = Math.abs(target[0] - nums[0]);
for (let i = 1; i < n; ++i) {
const x = target[i] - nums[i];
const y = target[i - 1] - nums[i - 1];
if (x * y > 0) {
const d = Math.abs(x) - Math.abs(y);
if (d > 0) {
f += d;
}
} else {
f += Math.abs(x);
}
}
return f;
}
```

<!-- tabs:end -->
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
class Solution {
public:
long long minimumOperations(vector<int>& nums, vector<int>& target) {
using ll = long long;
ll f = abs(target[0] - nums[0]);
for (int i = 1; i < nums.size(); ++i) {
long x = target[i] - nums[i];
long y = target[i - 1] - nums[i - 1];
if (x * y > 0) {
ll d = abs(x) - abs(y);
if (d > 0) {
f += d;
}
} else {
f += abs(x);
}
}
return f;
}
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
func minimumOperations(nums []int, target []int) int64 {
f := abs(target[0] - nums[0])
for i := 1; i < len(target); i++ {
x := target[i] - nums[i]
y := target[i-1] - nums[i-1]
if x*y > 0 {
if d := abs(x) - abs(y); d > 0 {
f += d
}
} else {
f += abs(x)
}
}
return int64(f)
}

func abs(x int) int {
if x < 0 {
return -x
}
return x
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
class Solution {
public long minimumOperations(int[] nums, int[] target) {
long f = Math.abs(target[0] - nums[0]);
for (int i = 1; i < nums.length; ++i) {
long x = target[i] - nums[i];
long y = target[i - 1] - nums[i - 1];
if (x * y > 0) {
long d = Math.abs(x) - Math.abs(y);
if (d > 0) {
f += d;
}
} else {
f += Math.abs(x);
}
}
return f;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
class Solution:
def minimumOperations(self, nums: List[int], target: List[int]) -> int:
n = len(nums)
f = abs(target[0] - nums[0])
for i in range(1, n):
x = target[i] - nums[i]
y = target[i - 1] - nums[i - 1]
if x * y > 0:
d = abs(x) - abs(y)
if d > 0:
f += d
else:
f += abs(x)
return f
Loading
Loading