forked from doocs/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
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.1775
No.1775.Equal Sum Arrays With Minimum Number of Operations
- Loading branch information
Showing
6 changed files
with
388 additions
and
2 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
24 changes: 24 additions & 0 deletions
24
solution/1700-1799/1775.Equal Sum Arrays With Minimum Number of Operations/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,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; | ||
} | ||
}; |
37 changes: 37 additions & 0 deletions
37
solution/1700-1799/1775.Equal Sum Arrays With Minimum Number of Operations/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,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 | ||
} |
Oops, something went wrong.