classSolution: defhelp(self, h1: List[int], h2: List[int], diff: int) -> int: h = [0] * 7 for i inrange(1, 7): h[6 - i] += h1[i] h[i - 1] += h2[i] res = 0 for i inrange(5, 0, -1): if diff <= 0: break t = min((diff + i - 1) // i, h[i]) res += t diff -= t * i return res
defminOperations(self, nums1: List[int], nums2: List[int]) -> int: n, m = len(nums1), len(nums2) if6 * n < m or6 * m < n: return -1 cnt1 = [0] * 7 cnt2 = [0] * 7 diff = 0 for i in nums1: cnt1[i] += 1 diff += i for i in nums2: cnt2[i] += 1 diff -= i if diff == 0: return0 if diff > 0: return self.help(cnt2, cnt1, diff) return self.help(cnt1, cnt2, -diff)
classSolution { public: inthelp(vector<int>& h1, vector<int>& h2, int diff){ vector<int> h(7, 0); for (int i = 1; i < 7; ++i) { h[6 - i] += h1[i]; h[i - 1] += h2[i]; } int res = 0; for (int i = 5; i && diff > 0; --i) { int t = min((diff + i - 1) / i, h[i]); res += t; diff -= t * i; } return res; }
intminOperations(vector<int>& nums1, vector<int>& nums2){ int n = nums1.size(), m = nums2.size(); if (6 * n < m || 6 * m < n) { return-1; } vector<int> cnt1(7, 0), cnt2(7, 0); int diff = 0; for (auto& i : nums1) { ++cnt1[i]; diff += i; } for (auto& i : nums2) { ++cnt2[i]; diff -= i; } if (!diff) { return0; } if (diff > 0) { returnhelp(cnt2, cnt1, diff); } returnhelp(cnt1, cnt2, -diff); } };
publicclassSolution { publicintMinOperations(int[] nums1, int[] nums2) { int n = nums1.Length, m = nums2.Length; if (6 * n < m || 6 * m < n) { return-1; } int[] cnt1 = newint[7]; int[] cnt2 = newint[7]; int diff = 0; foreach (int i in nums1) { ++cnt1[i]; diff += i; } foreach (int i in nums2) { ++cnt2[i]; diff -= i; } if (diff == 0) { return0; } if (diff > 0) { return Help(cnt2, cnt1, diff); } return Help(cnt1, cnt2, -diff); }
publicintHelp(int[] h1, int[] h2, int diff) { int[] h = newint[7]; for (int i = 1; i < 7; ++i) { h[6 - i] += h1[i]; h[i - 1] += h2[i]; } int res = 0; for (int i = 5; i > 0 && diff > 0; --i) { int t = Math.Min((diff + i - 1) / i, h[i]); res += t; diff -= t * i; } return res; } }
inthelp(constint *h1, constint *h2, int diff) { int h[7]; memset(h, 0, sizeof(h)); for (int i = 1; i < 7; ++i) { h[6 - i] += h1[i]; h[i - 1] += h2[i]; } int res = 0; for (int i = 5; i && diff > 0; --i) { int t = MIN((diff + i - 1) / i, h[i]); res += t; diff -= t * i; } return res; }
intminOperations(int* nums1, int nums1Size, int* nums2, int nums2Size) { if (6 * nums1Size < nums2Size || 6 * nums2Size < nums1Size) { return-1; } int cnt1[7], cnt2[7]; memset(cnt1, 0, sizeof(cnt1)); memset(cnt2, 0, sizeof(cnt2)); int diff = 0; for (int i = 0; i < nums1Size; i++) { ++cnt1[nums1[i]]; diff += nums1[i]; } for (int i = 0; i < nums2Size; i++) { ++cnt2[nums2[i]]; diff -= nums2[i]; } if (!diff) { return0; } if (diff > 0) { return help(cnt2, cnt1, diff); } return help(cnt1, cnt2, -diff); }
funchelp(h1 [7]int, h2 [7]int, diff int) (res int) { h := [7]int{} for i := 1; i < 7; i++ { h[6-i] += h1[i] h[i-1] += h2[i] } for i := 5; i > 0 && diff > 0; i-- { t := min((diff+i-1)/i, h[i]) res += t diff -= t * i } return res }
funcminOperations(nums1 []int, nums2 []int) (ans int) { n, m := len(nums1), len(nums2) if6*n < m || 6*m < n { return-1 } var cnt1, cnt2 [7]int diff := 0 for _, i := range nums1 { cnt1[i]++ diff += i } for _, i := range nums2 { cnt2[i]++ diff -= i } if diff == 0 { return0 } if diff > 0 { return help(cnt2, cnt1, diff) } return help(cnt1, cnt2, -diff) }
funcmin(a, b int)int { if a > b { return b } return a }
复杂度分析
时间复杂度:O(n + m),其中 n,m 分别为数组 nums}_1,nums}_2 的长度。
空间复杂度:O(C),其中 C 为数组 nums}_1,nums}_2 中元素值的取值空间,主要为用数组来模拟「哈希表」的空间开销。