-
-
Notifications
You must be signed in to change notification settings - Fork 7.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add solutions to lc problem: No.3229 (#3300)
No.3229.Minimum Operations to Make Array Equal to Target
- Loading branch information
Showing
9 changed files
with
319 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
20 changes: 20 additions & 0 deletions
20
solution/3200-3299/3229.Minimum Operations to Make Array Equal to Target/Solution.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
}; |
22 changes: 22 additions & 0 deletions
22
solution/3200-3299/3229.Minimum Operations to Make Array Equal to Target/Solution.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
18 changes: 18 additions & 0 deletions
18
solution/3200-3299/3229.Minimum Operations to Make Array Equal to Target/Solution.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
14 changes: 14 additions & 0 deletions
14
solution/3200-3299/3229.Minimum Operations to Make Array Equal to Target/Solution.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
Oops, something went wrong.