1
var minOperations = function (nums1, nums2) {4
min1...max1 ... min2...max2 -> we can't make them equal5
min2...max2 ... min1...max1 -> we can't make them equal7
let min1 = nums1.length * 1;8
let max1 = nums1.length * 6;9
let min2 = nums2.length * 1;10
let max2 = nums2.length * 6;11
if (min2 > max1 || min1 > max2) {14
let sum1 = nums1.reduce((acc, cur) => acc + cur);15
let sum2 = nums2.reduce((acc, cur) => acc + cur);16
if (sum1 === sum2) return 0;18
return helper(nums1, nums2, sum1, sum2);20
return helper(nums2, nums1, sum2, sum1);22
// T.C: O(M + N), M = # of nums1, N = # of nums226
// Condition: sum of A < sum of B27
// the idea is to add the maximum possible value to sumA and28
// subtract the maximum possible value from sumB so that29
// we make sumA >= sumB as soon as possible30
function helper(A, B, sumA, sumB) {31
let freqA = new Array(7).fill(0);32
let freqB = new Array(7).fill(0);33
for (let i = 0; i < A.length; i++) {36
for (let i = 0; i < B.length; i++) {40
for (let i = 1; i <= 6; i++) {42
while (freqA[i] > 0 && sumA < sumB) {49
while (freqB[j] > 0 && sumA < sumB) {54
if (sumA >= sumB) break;