From a4225faa536bf4d9af53611af3a1c1bd22aea4fc Mon Sep 17 00:00:00 2001 From: yanglbme Date: Sun, 21 Jul 2024 21:17:32 +0800 Subject: [PATCH] feat: add solutions to lc problem: No.3229 No.3229.Minimum Operations to Make Array Equal to Target --- .../README.md | 6 +- .../README_EN.md | 16 ++- .../README.md | 108 +++++++++++++++++- .../README_EN.md | 108 +++++++++++++++++- .../Solution.cpp | 20 ++++ .../Solution.go | 22 ++++ .../Solution.java | 18 +++ .../Solution.py | 14 +++ .../Solution.ts | 17 +++ 9 files changed, 319 insertions(+), 10 deletions(-) create mode 100644 solution/3200-3299/3229.Minimum Operations to Make Array Equal to Target/Solution.cpp create mode 100644 solution/3200-3299/3229.Minimum Operations to Make Array Equal to Target/Solution.go create mode 100644 solution/3200-3299/3229.Minimum Operations to Make Array Equal to Target/Solution.java create mode 100644 solution/3200-3299/3229.Minimum Operations to Make Array Equal to Target/Solution.py create mode 100644 solution/3200-3299/3229.Minimum Operations to Make Array Equal to Target/Solution.ts diff --git a/solution/1500-1599/1526.Minimum Number of Increments on Subarrays to Form a Target Array/README.md b/solution/1500-1599/1526.Minimum Number of Increments on Subarrays to Form a Target Array/README.md index 946a912a6162f..b0fe402ef0ad0 100644 --- a/solution/1500-1599/1526.Minimum Number of Increments on Subarrays to Form a Target Array/README.md +++ b/solution/1500-1599/1526.Minimum Number of Increments on Subarrays to Form a Target Array/README.md @@ -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) diff --git a/solution/1500-1599/1526.Minimum Number of Increments on Subarrays to Form a Target Array/README_EN.md b/solution/1500-1599/1526.Minimum Number of Increments on Subarrays to Form a Target Array/README_EN.md index fe4370612f9d9..0db551b52544d 100644 --- a/solution/1500-1599/1526.Minimum Number of Increments on Subarrays to Form a Target Array/README_EN.md +++ b/solution/1500-1599/1526.Minimum Number of Increments on Subarrays to Form a Target Array/README_EN.md @@ -73,7 +73,21 @@ tags: -### 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) diff --git a/solution/3200-3299/3229.Minimum Operations to Make Array Equal to Target/README.md b/solution/3200-3299/3229.Minimum Operations to Make Array Equal to Target/README.md index be8c2cc37605b..5d87a2fbdbfb5 100644 --- a/solution/3200-3299/3229.Minimum Operations to Make Array Equal to Target/README.md +++ b/solution/3200-3299/3229.Minimum Operations to Make Array Equal to Target/README.md @@ -68,32 +68,132 @@ edit_url: https://github.com/doocs/leetcode/edit/main/solution/3200-3299/3229.Mi -### 方法一 +### 方法一:动态规划 + +我们可以先计算出 $\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) #### 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& nums, vector& 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; +} ``` diff --git a/solution/3200-3299/3229.Minimum Operations to Make Array Equal to Target/README_EN.md b/solution/3200-3299/3229.Minimum Operations to Make Array Equal to Target/README_EN.md index 2bade6ced56c3..e32a4fd400e3a 100644 --- a/solution/3200-3299/3229.Minimum Operations to Make Array Equal to Target/README_EN.md +++ b/solution/3200-3299/3229.Minimum Operations to Make Array Equal to Target/README_EN.md @@ -66,32 +66,132 @@ edit_url: https://github.com/doocs/leetcode/edit/main/solution/3200-3299/3229.Mi -### 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) #### 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& nums, vector& 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; +} ``` diff --git a/solution/3200-3299/3229.Minimum Operations to Make Array Equal to Target/Solution.cpp b/solution/3200-3299/3229.Minimum Operations to Make Array Equal to Target/Solution.cpp new file mode 100644 index 0000000000000..7709e5c6369e7 --- /dev/null +++ b/solution/3200-3299/3229.Minimum Operations to Make Array Equal to Target/Solution.cpp @@ -0,0 +1,20 @@ +class Solution { +public: + long long minimumOperations(vector& nums, vector& 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; + } +}; \ No newline at end of file diff --git a/solution/3200-3299/3229.Minimum Operations to Make Array Equal to Target/Solution.go b/solution/3200-3299/3229.Minimum Operations to Make Array Equal to Target/Solution.go new file mode 100644 index 0000000000000..c80b89e43282a --- /dev/null +++ b/solution/3200-3299/3229.Minimum Operations to Make Array Equal to Target/Solution.go @@ -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 +} \ No newline at end of file diff --git a/solution/3200-3299/3229.Minimum Operations to Make Array Equal to Target/Solution.java b/solution/3200-3299/3229.Minimum Operations to Make Array Equal to Target/Solution.java new file mode 100644 index 0000000000000..0e5ec9103486c --- /dev/null +++ b/solution/3200-3299/3229.Minimum Operations to Make Array Equal to Target/Solution.java @@ -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; + } +} \ No newline at end of file diff --git a/solution/3200-3299/3229.Minimum Operations to Make Array Equal to Target/Solution.py b/solution/3200-3299/3229.Minimum Operations to Make Array Equal to Target/Solution.py new file mode 100644 index 0000000000000..4428f92a99d5c --- /dev/null +++ b/solution/3200-3299/3229.Minimum Operations to Make Array Equal to Target/Solution.py @@ -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 diff --git a/solution/3200-3299/3229.Minimum Operations to Make Array Equal to Target/Solution.ts b/solution/3200-3299/3229.Minimum Operations to Make Array Equal to Target/Solution.ts new file mode 100644 index 0000000000000..0f704df024826 --- /dev/null +++ b/solution/3200-3299/3229.Minimum Operations to Make Array Equal to Target/Solution.ts @@ -0,0 +1,17 @@ +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; +}