Skip to content

Commit 6e596c6

Browse files
authored
Merge pull request #335 from cheehwatang/add-1775-EqualSumArraysWithMinimumNumberOfOperations
Add 1775. Equal Sum Arrays With Minimum Number of Operations (Counting)
2 parents b88a113 + 4f2c94a commit 6e596c6

File tree

2 files changed

+121
-13
lines changed

2 files changed

+121
-13
lines changed

README.md

Lines changed: 55 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,19 @@
2727
<th>Solution</th>
2828
<th>Topics</th>
2929
</tr>
30+
<tr>
31+
<td align="center">September 14th</td>
32+
<td>1775. <a href="https://leetcode.com/problems/equal-sum-arrays-with-minimum-number-of-operations/">Equal Sum Arrays With Minimum Number of Operations</a></td>
33+
<td align="center">$\text{\color{Dandelion}Medium}$</td>
34+
<td align="center">
35+
<a href="https://github.com/cheehwatang/leetcode-java/blob/main/solutions/1775.%20Equal%20Sum%20Arrays%20With%20Minimum%20Number%20of%20Operations/EqualSumArraysWithMinimumNumberOfOperations_Counting.java">Counting</a>
36+
</td>
37+
<td align="center">
38+
<a href="#array">Array</a>,
39+
<a href="#counting">Counting</a>,
40+
<a href="#greedy">Greedy</a>
41+
</td>
42+
</tr>
3043
<tr>
3144
<td align="center">September 13th</td>
3245
<td>1814. <a href="https://leetcode.com/problems/count-nice-pairs-in-an-array/">Count Nice Pairs in an Array</a></td>
@@ -76,19 +89,6 @@
7689
<a href="#array">Array</a>
7790
</td>
7891
</tr>
79-
<tr>
80-
<td align="center">September 9th</td>
81-
<td>2028. <a href="https://leetcode.com/problems/find-missing-observations/">Find Missing Observations</a></td>
82-
<td align="center">$\text{\color{Dandelion}Medium}$</td>
83-
<td align="center">
84-
<a href="https://github.com/cheehwatang/leetcode-java/blob/main/solutions/2028.%20Find%20Missing%20Observations/FindMissingObservations.java">Math</a>
85-
</td>
86-
<td align="center">
87-
<a href="#array">Array</a>,
88-
<a href="#math">Math</a>,
89-
<a href="#simulation">Simulation</a>
90-
</td>
91-
</tr>
9292
</table>
9393
</br>
9494
<hr>
@@ -1454,6 +1454,20 @@
14541454
</td>
14551455
<td></td>
14561456
</tr>
1457+
<tr>
1458+
<td align="center">1775</td>
1459+
<td><a href="https://leetcode.com/problems/equal-sum-arrays-with-minimum-number-of-operations/">Equal Sum Arrays With Minimum Number of Operations</a></td>
1460+
<td align="center">
1461+
<a href="https://github.com/cheehwatang/leetcode-java/blob/main/solutions/1775.%20Equal%20Sum%20Arrays%20With%20Minimum%20Number%20of%20Operations/EqualSumArraysWithMinimumNumberOfOperations_Counting.java">Java</a>
1462+
</td>
1463+
<td align="center">$\text{\color{Dandelion}Medium}$</td>
1464+
<td align="center">
1465+
<a href="#array">Array</a>,
1466+
<a href="#counting">Counting</a>,
1467+
<a href="#greedy">Greedy</a>
1468+
</td>
1469+
<td></td>
1470+
</tr>
14571471
<tr>
14581472
<td align="center">1814</td>
14591473
<td><a href="https://leetcode.com/problems/count-nice-pairs-in-an-array/">Count Nice Pairs in an Array</a></td>
@@ -3395,6 +3409,20 @@
33953409
</td>
33963410
<td></td>
33973411
</tr>
3412+
<tr>
3413+
<td align="center">1775</td>
3414+
<td><a href="https://leetcode.com/problems/equal-sum-arrays-with-minimum-number-of-operations/">Equal Sum Arrays With Minimum Number of Operations</a></td>
3415+
<td align="center">
3416+
<a href="https://github.com/cheehwatang/leetcode-java/blob/main/solutions/1775.%20Equal%20Sum%20Arrays%20With%20Minimum%20Number%20of%20Operations/EqualSumArraysWithMinimumNumberOfOperations_Counting.java">Java</a>
3417+
</td>
3418+
<td align="center">$\text{\color{Dandelion}Medium}$</td>
3419+
<td align="center">
3420+
<a href="#array">Array</a>,
3421+
<a href="#counting">Counting</a>,
3422+
<a href="#greedy">Greedy</a>
3423+
</td>
3424+
<td></td>
3425+
</tr>
33983426
<tr>
33993427
<td align="center">1814</td>
34003428
<td><a href="https://leetcode.com/problems/count-nice-pairs-in-an-array/">Count Nice Pairs in an Array</a></td>
@@ -4841,6 +4869,20 @@
48414869
<a href="https://github.com/cheehwatang/leetcode-java/blob/main/solutions/1578.%20Minimum%20Time%20to%20Make%20Rope%20Colorful/MinimumTimeToMakeRopeColorful_DynamicProgramming.java"><em>Dynamic Programming</em></a>
48424870
</td>
48434871
</tr>
4872+
<tr>
4873+
<td align="center">1775</td>
4874+
<td><a href="https://leetcode.com/problems/equal-sum-arrays-with-minimum-number-of-operations/">Equal Sum Arrays With Minimum Number of Operations</a></td>
4875+
<td align="center">
4876+
<a href="https://github.com/cheehwatang/leetcode-java/blob/main/solutions/1775.%20Equal%20Sum%20Arrays%20With%20Minimum%20Number%20of%20Operations/EqualSumArraysWithMinimumNumberOfOperations_Counting.java">Java</a>
4877+
</td>
4878+
<td align="center">$\text{\color{Dandelion}Medium}$</td>
4879+
<td align="center">
4880+
<a href="#array">Array</a>,
4881+
<a href="#counting">Counting</a>,
4882+
<a href="#greedy">Greedy</a>
4883+
</td>
4884+
<td></td>
4885+
</tr>
48444886
<tr>
48454887
<td align="center">1833</td>
48464888
<td><a href="https://leetcode.com/problems/maximum-ice-cream-bars/">Maximum Ice Cream Bars</a></td>
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
package com.cheehwatang.leetcode;
2+
3+
// Time Complexity : O(n + m),
4+
// where 'n' is the length of 'nums1', and 'm' is the length of 'nums2'.
5+
// We traverse 'nums1' once, and 'nums2' once.
6+
//
7+
// Space Complexity : O(1),
8+
// as the auxiliary space used is independent of the input.
9+
10+
public class EqualSumArraysWithMinimumNumberOfOperations_Counting {
11+
12+
// Approach:
13+
// Using counting array to keep track of the frequency of both arrays.
14+
// Since both array's elements can be manipulated, the counting array records the inversion of the second array.
15+
// Example: nums1 = [1,1], nums2 = [6,5,4], counting array = [3,1,1,0,0,0].
16+
// This is because, to make the sum equal, in this example,
17+
// we can either increase the 1 in 'nums1' to 6 or decrease the 6 in 'nums2' to 1.
18+
// In both cases, the 'difference' is decreased by 5.
19+
// Once we have the counting array, and the sum of both arrays,
20+
// we get change the smallest number in the counting array first as it has the largest impact on the 'difference'
21+
// (Greedy Approach).
22+
23+
public int minOperations(int[] nums1, int[] nums2) {
24+
// Case where it is impossible to get equal sum,
25+
// when one array is longer than 6 multiples of the second array length.
26+
if ((nums1.length * 6 < nums2.length) || (nums1.length > nums2.length * 6)) return -1;
27+
28+
// Use the countArray to record the frequency of each number in 'nums1',
29+
// as well as the inversion of the numbers in 'nums2'.
30+
// Additionally, record the sum of each array.
31+
int[] countArray = new int[7];
32+
int sumNum1 = 0;
33+
for (int number : nums1) {
34+
countArray[number]++;
35+
sumNum1 += number;
36+
}
37+
int sumNum2 = 0;
38+
for (int number : nums2) {
39+
countArray[7 - number]++;
40+
sumNum2 += number;
41+
}
42+
43+
int count = 0;
44+
int difference = sumNum1 - sumNum2;
45+
// In both cases, from the number with the largest impact (1 if difference is negative, 6 if difference is positive),
46+
// the operations to make is either:
47+
// - all the frequency of the number (with remainders of the difference), or
48+
// - the remainders of the difference divided by the number.
49+
if (difference < 0) {
50+
for (int i = 1; i < 6; i++) {
51+
int operations = Math.min(countArray[i], -difference / (6 - i) + (-difference % (6 - i) == 0 ? 0 : 1));
52+
difference += operations * (6 - i);
53+
count += operations;
54+
if (difference >= 0) break;
55+
}
56+
} else if (difference > 0) {
57+
for (int i = 6; i > 1; i--) {
58+
int operations = Math.min(countArray[i], difference / (i - 1) + (difference % (i - 1) == 0 ? 0 : 1));
59+
difference -= operations * (i - 1);
60+
count += operations;
61+
if (difference <= 0) break;
62+
}
63+
}
64+
return count;
65+
}
66+
}

0 commit comments

Comments
 (0)