Skip to content

Commit

Permalink
feat: add solutions to lc problem: No.1775
Browse files Browse the repository at this point in the history
No.1775.Equal Sum Arrays With Minimum Number of Operations
  • Loading branch information
yanglbme committed Feb 5, 2022
1 parent 314e4b7 commit f8f87bf
Show file tree
Hide file tree
Showing 6 changed files with 388 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -54,22 +54,162 @@

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

贪心 + 计数排序。

设 s1, s2 分别表示数组 nums1, nums2 的和。不失一般性,可以设 `s1 < s2`

直观上看,要想使得操作次数最小,我们应当尽可能增加 nums1 中元素的值,同时尽可能减少 nums2 中元素的值,因此:

- nums1 中每个元素 x 可以增加的量为 `6-x ∈ [0,5]`
- nums2 中每个元素 x 可以减少的量为 `x-1 ∈ [0,5]`

`diff = s2 - s1`,我们要选出最少的元素,使得**在 nums1 中元素的增加量****在 nums2 中元素的减少量**之和大于等于 diff。因此我们可以贪心地从 5 这个增加/减少量开始递减地选取即可。

在实际的代码编写中,我们只需要使用一个长度为 6 的数组,其下标为增加 / 减少量,值为对应的元素数量。

<!-- tabs:start -->

### **Python3**

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

```python

class Solution:
def minOperations(self, nums1: List[int], nums2: List[int]) -> int:
s1, s2 = sum(nums1), sum(nums2)
if s1 == s2:
return 0
if s1 > s2:
return self.minOperations(nums2, nums1)
freq = [0] * 6
for x in nums1:
freq[6 - x] += 1
for x in nums2:
freq[x - 1] += 1
diff = s2 - s1
ans, i = 0, 5
while i > 0 and diff > 0:
while freq[i] and diff > 0:
diff -= i
freq[i] -= 1
ans += 1
i -= 1
return -1 if diff > 0 else ans
```

### **Java**

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

```java
class Solution {
public int minOperations(int[] nums1, int[] nums2) {
int s1 = sum(nums1);
int s2 = sum(nums2);
if (s1 == s2) {
return 0;
}
if (s1 > s2) {
return minOperations(nums2, nums1);
}
int[] freq = new int[6];
for (int x : nums1) {
++freq[6 - x];
}
for (int x : nums2) {
++freq[x - 1];
}
int diff = s2 - s1;
int ans = 0;
for (int i = 5; i > 0 && diff > 0; --i) {
while (freq[i] > 0 && diff > 0) {
diff -= i;
--freq[i];
++ans;
}
}
return diff > 0 ? - 1 : ans;
}

private int sum(int[] nums) {
int s = 0;
for (int x : nums) {
s += x;
}
return s;
}
}
```

### **C++**

```cpp
class Solution {
public:
int minOperations(vector<int>& nums1, vector<int>& nums2) {
int s1 = accumulate(nums1.begin(), nums1.end(), 0);
int s2 = accumulate(nums2.begin(), nums2.end(), 0);
if (s1 == s2) return 0;
if (s1 > s2) return minOperations(nums2, nums1);
vector<int> freq(6);
for (int x : nums1) ++freq[6 - x];
for (int x : nums2) ++freq[x - 1];
int diff = s2 - s1;
int ans = 0;
for (int i = 5; i > 0 && diff > 0; --i)
{
while (freq[i] && diff > 0)
{
diff -= i;
--freq[i];
++ans;
}
}
return diff > 0 ? -1 : ans;
}
};
```
### **Go**
```go
func minOperations(nums1 []int, nums2 []int) int {
s1, s2 := sum(nums1), sum(nums2)
if s1 == s2 {
return 0
}
if s1 > s2 {
return minOperations(nums2, nums1)
}
freq := make([]int, 6)
for _, x := range nums1 {
freq[6-x]++
}
for _, x := range nums2 {
freq[x-1]++
}
diff := s2 - s1
ans := 0
for i := 5; i > 0 && diff > 0; i-- {
for freq[i] > 0 && diff > 0 {
diff -= i
freq[i]--
ans++
}
}
if diff > 0 {
return -1
}
return ans
}
func sum(nums []int) int {
s := 0
for _, x := range nums {
s += x
}
return s
}
```

### **...**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,13 +56,140 @@
### **Python3**

```python

class Solution:
def minOperations(self, nums1: List[int], nums2: List[int]) -> int:
s1, s2 = sum(nums1), sum(nums2)
if s1 == s2:
return 0
if s1 > s2:
return self.minOperations(nums2, nums1)
freq = [0] * 6
for x in nums1:
freq[6 - x] += 1
for x in nums2:
freq[x - 1] += 1
diff = s2 - s1
ans, i = 0, 5
while i > 0 and diff > 0:
while freq[i] and diff > 0:
diff -= i
freq[i] -= 1
ans += 1
i -= 1
return -1 if diff > 0 else ans
```

### **Java**

```java
class Solution {
public int minOperations(int[] nums1, int[] nums2) {
int s1 = sum(nums1);
int s2 = sum(nums2);
if (s1 == s2) {
return 0;
}
if (s1 > s2) {
return minOperations(nums2, nums1);
}
int[] freq = new int[6];
for (int x : nums1) {
++freq[6 - x];
}
for (int x : nums2) {
++freq[x - 1];
}
int diff = s2 - s1;
int ans = 0;
for (int i = 5; i > 0 && diff > 0; --i) {
while (freq[i] > 0 && diff > 0) {
diff -= i;
--freq[i];
++ans;
}
}
return diff > 0 ? - 1 : ans;
}

private int sum(int[] nums) {
int s = 0;
for (int x : nums) {
s += x;
}
return s;
}
}
```

### **C++**

```cpp
class Solution {
public:
int minOperations(vector<int>& nums1, vector<int>& nums2) {
int s1 = accumulate(nums1.begin(), nums1.end(), 0);
int s2 = accumulate(nums2.begin(), nums2.end(), 0);
if (s1 == s2) return 0;
if (s1 > s2) return minOperations(nums2, nums1);
vector<int> freq(6);
for (int x : nums1) ++freq[6 - x];
for (int x : nums2) ++freq[x - 1];
int diff = s2 - s1;
int ans = 0;
for (int i = 5; i > 0 && diff > 0; --i)
{
while (freq[i] && diff > 0)
{
diff -= i;
--freq[i];
++ans;
}
}
return diff > 0 ? -1 : ans;
}
};
```
### **Go**
```go
func minOperations(nums1 []int, nums2 []int) int {
s1, s2 := sum(nums1), sum(nums2)
if s1 == s2 {
return 0
}
if s1 > s2 {
return minOperations(nums2, nums1)
}
freq := make([]int, 6)
for _, x := range nums1 {
freq[6-x]++
}
for _, x := range nums2 {
freq[x-1]++
}
diff := s2 - s1
ans := 0
for i := 5; i > 0 && diff > 0; i-- {
for freq[i] > 0 && diff > 0 {
diff -= i
freq[i]--
ans++
}
}
if diff > 0 {
return -1
}
return ans
}
func sum(nums []int) int {
s := 0
for _, x := range nums {
s += x
}
return s
}
```

### **...**
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
class Solution {
public:
int minOperations(vector<int>& nums1, vector<int>& nums2) {
int s1 = accumulate(nums1.begin(), nums1.end(), 0);
int s2 = accumulate(nums2.begin(), nums2.end(), 0);
if (s1 == s2) return 0;
if (s1 > s2) return minOperations(nums2, nums1);
vector<int> freq(6);
for (int x : nums1) ++freq[6 - x];
for (int x : nums2) ++freq[x - 1];
int diff = s2 - s1;
int ans = 0;
for (int i = 5; i > 0 && diff > 0; --i)
{
while (freq[i] && diff > 0)
{
diff -= i;
--freq[i];
++ans;
}
}
return diff > 0 ? -1 : ans;
}
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
func minOperations(nums1 []int, nums2 []int) int {
s1, s2 := sum(nums1), sum(nums2)
if s1 == s2 {
return 0
}
if s1 > s2 {
return minOperations(nums2, nums1)
}
freq := make([]int, 6)
for _, x := range nums1 {
freq[6-x]++
}
for _, x := range nums2 {
freq[x-1]++
}
diff := s2 - s1
ans := 0
for i := 5; i > 0 && diff > 0; i-- {
for freq[i] > 0 && diff > 0 {
diff -= i
freq[i]--
ans++
}
}
if diff > 0 {
return -1
}
return ans
}

func sum(nums []int) int {
s := 0
for _, x := range nums {
s += x
}
return s
}
Loading

0 comments on commit f8f87bf

Please sign in to comment.