|
| 1 | +// Source : https://leetcode.com/problems/equal-sum-arrays-with-minimum-number-of-operations/ |
| 2 | +// Author : Hao Chen |
| 3 | +// Date : 2021-03-24 |
| 4 | + |
| 5 | +/***************************************************************************************************** |
| 6 | + * |
| 7 | + * You are given two arrays of integers nums1 and nums2, possibly of different lengths. The values in |
| 8 | + * the arrays are between 1 and 6, inclusive. |
| 9 | + * |
| 10 | + * In one operation, you can change any integer's value in any of the arrays to any value between 1 |
| 11 | + * and 6, inclusive. |
| 12 | + * |
| 13 | + * Return the minimum number of operations required to make the sum of values in nums1 equal to the |
| 14 | + * sum of values in nums2. Return -1 if it is not possible to make the sum of the two arrays |
| 15 | + * equal. |
| 16 | + * |
| 17 | + * Example 1: |
| 18 | + * |
| 19 | + * Input: nums1 = [1,2,3,4,5,6], nums2 = [1,1,2,2,2,2] |
| 20 | + * Output: 3 |
| 21 | + * Explanation: You can make the sums of nums1 and nums2 equal with 3 operations. All indices are |
| 22 | + * 0-indexed. |
| 23 | + * - Change nums2[0] to 6. nums1 = [1,2,3,4,5,6], nums2 = [6,1,2,2,2,2]. |
| 24 | + * - Change nums1[5] to 1. nums1 = [1,2,3,4,5,1], nums2 = [6,1,2,2,2,2]. |
| 25 | + * - Change nums1[2] to 2. nums1 = [1,2,2,4,5,1], nums2 = [6,1,2,2,2,2]. |
| 26 | + * |
| 27 | + * Example 2: |
| 28 | + * |
| 29 | + * Input: nums1 = [1,1,1,1,1,1,1], nums2 = [6] |
| 30 | + * Output: -1 |
| 31 | + * Explanation: There is no way to decrease the sum of nums1 or to increase the sum of nums2 to make |
| 32 | + * them equal. |
| 33 | + * |
| 34 | + * Example 3: |
| 35 | + * |
| 36 | + * Input: nums1 = [6,6], nums2 = [1] |
| 37 | + * Output: 3 |
| 38 | + * Explanation: You can make the sums of nums1 and nums2 equal with 3 operations. All indices are |
| 39 | + * 0-indexed. |
| 40 | + * - Change nums1[0] to 2. nums1 = [2,6], nums2 = [1]. |
| 41 | + * - Change nums1[1] to 2. nums1 = [2,2], nums2 = [1]. |
| 42 | + * - Change nums2[0] to 4. nums1 = [2,2], nums2 = [4]. |
| 43 | + * |
| 44 | + * Constraints: |
| 45 | + * |
| 46 | + * 1 <= nums1.length, nums2.length <= 10^5 |
| 47 | + * 1 <= nums1[i], nums2[i] <= 6 |
| 48 | + ******************************************************************************************************/ |
| 49 | + |
| 50 | +class Solution { |
| 51 | +private: |
| 52 | + void print(vector<int>& n) { |
| 53 | + cout <<"["; |
| 54 | + for(int i=0; i< n.size() - 1; i++) { |
| 55 | + cout << n[i] << ","; |
| 56 | + } |
| 57 | + cout << n[n.size()-1] << "]" <<endl; |
| 58 | + } |
| 59 | +private: |
| 60 | + int minOpsBySort(int gaps, vector<int>& small, vector<int>& big) { |
| 61 | + |
| 62 | + sort(small.begin(), small.end()); |
| 63 | + sort(big.begin(), big.end()); |
| 64 | + |
| 65 | + int op = 0; |
| 66 | + int left = 0, right = big.size() -1; |
| 67 | + while (gaps >0) { |
| 68 | + |
| 69 | + int small_gaps = left < small.size() ? 6 - small[left] : 0; |
| 70 | + int big_gaps = right >= 0 ? big[right] - 1 : 0; |
| 71 | + |
| 72 | + if (small_gaps > big_gaps) { |
| 73 | + gaps -= small_gaps; |
| 74 | + left++; |
| 75 | + }else{ |
| 76 | + gaps -= big_gaps; |
| 77 | + right--; |
| 78 | + } |
| 79 | + op++; |
| 80 | + } |
| 81 | + return op; |
| 82 | + } |
| 83 | + |
| 84 | + int minOpsByCnt1(int gaps, vector<int>& small, vector<int>& big) { |
| 85 | + int small_cnt[6] = {0} , big_cnt[6] = {0}; |
| 86 | + for (auto& n : small) small_cnt[n-1]++; |
| 87 | + for (auto& n : big) big_cnt[n-1]++; |
| 88 | + |
| 89 | + int op = 0; |
| 90 | + int left = 0, right = 5; |
| 91 | + |
| 92 | + while( gaps > 0 ) { |
| 93 | + while (left < 6 && small_cnt[left] == 0 ) left++; |
| 94 | + while ( right >=0 && big_cnt[right] == 0 ) right--; |
| 95 | + int small_gaps = left < 6 ? 6 - (left + 1) : 0; |
| 96 | + int big_gaps = right >= 0 ? right : 0; |
| 97 | + |
| 98 | + if (small_gaps > big_gaps) { |
| 99 | + gaps -= small_gaps; |
| 100 | + small_cnt[left]--; |
| 101 | + }else{ |
| 102 | + gaps -= big_gaps; |
| 103 | + big_cnt[right]--; |
| 104 | + } |
| 105 | + op++; |
| 106 | + } |
| 107 | + return op; |
| 108 | + } |
| 109 | + |
| 110 | + int minOpsByCnt2(int gaps, vector<int>& small, vector<int>& big) { |
| 111 | + int cnt[6] = {0}; |
| 112 | + for (auto& n : small) cnt[6-n]++; |
| 113 | + for (auto& n : big) cnt[n-1]++; |
| 114 | + |
| 115 | + int ops = 0; |
| 116 | + for (int i=5 ; i >= 0 && gaps > 0; i--) { |
| 117 | + if (cnt[i] == 0) continue; |
| 118 | + if (cnt[i] * i > gaps) { |
| 119 | + ops += (gaps / i + (gaps % i ? 1:0) ) ; |
| 120 | + break; |
| 121 | + } |
| 122 | + gaps -= cnt[i] * i; |
| 123 | + ops += cnt[i]; |
| 124 | + } |
| 125 | + |
| 126 | + return ops; |
| 127 | + } |
| 128 | +public: |
| 129 | + int minOperations(vector<int>& nums1, vector<int>& nums2) { |
| 130 | + int len1 = nums1.size(), len2 = nums2.size(); |
| 131 | + if ( len1 > 6*len2 || len2 > 6*len1) return -1; |
| 132 | + |
| 133 | + int sum1 = 0 , sum2 = 0; |
| 134 | + for (auto& n : nums1) sum1 += n; |
| 135 | + for (auto& n : nums2) sum2 += n; |
| 136 | + |
| 137 | + if (sum1 > sum2) { |
| 138 | + swap(sum1, sum2); |
| 139 | + swap(nums1, nums2); |
| 140 | + } |
| 141 | + int gaps = sum2 - sum1; |
| 142 | + if (gaps == 0) return 0; |
| 143 | + return minOpsByCnt2(gaps, nums1, nums2); //104ms |
| 144 | + return minOpsByCnt1(gaps, nums1, nums2); //108ms |
| 145 | + return minOpsBySort(gaps, nums1, nums2); //140ms |
| 146 | + } |
| 147 | +}; |
0 commit comments