1
+ // Runtime: 438 ms (Top 14.29%) | Memory: 113.8 MB (Top 74.48%)
1
2
/* *
2
- * @brief
3
+ * @brief
3
4
* Given two array, return the minimum numbers we have to change the values to make two arrays sum equal
4
- *
5
+ *
5
6
* [Observation]
6
7
* Since the value is from 1 ~ 6
7
- *
8
+ *
8
9
* Min sum of the array = len(arr)
9
- * Max sum of the array = 6 len(arr)
10
- *
11
- * When to arrays range cannot overlap -> no answer
12
- *
10
+ * Max sum of the array = 6 len(arr)
11
+ *
12
+ * When to arrays range cannot overlap -> no answer
13
+ *
13
14
* If there is a answer -> sum s, value will be between s1 and s2
14
15
* So, let's say if s1 is smaller, we would like to increase s1's element and decrease s2's element
15
16
* -> We only have to design for s1 is smaller than s2.
16
- *
17
- * [Key] Which element's should we increase and decrease?
17
+ *
18
+ * [Key] Which element's should we increase and decrease?
18
19
* To minimize the changing elements, we change the number who can mostly decrease the differences.
19
- * So, we compare the smallest element in num1 and largest in num2.
20
+ * So, we compare the smallest element in num1 and largest in num2.
20
21
* -> sorting
21
- *
22
+ *
22
23
* @algo sorting + greedy
23
24
* Time O(NlogN) for sorting
24
25
* Space O(1)
@@ -32,15 +33,15 @@ class Solution {
32
33
if (6 *l1 < l2 || 6 *l2 < l1) {
33
34
return -1 ;
34
35
}
35
-
36
+
36
37
int sum1 = accumulate (nums1.begin (), nums1.end (), 0 );
37
38
int sum2 = accumulate (nums2.begin (), nums2.end (), 0 );
38
39
if (sum1 > sum2) return minOperations (nums2, nums1);
39
40
40
41
sort (nums1.begin (), nums1.end ());
41
42
sort (nums2.begin (), nums2.end (), greater<int >());
42
43
// let us design the way where sum1 <= sum2
43
- int ans = 0 , ptr1 = 0 , ptr2 = 0 ;
44
+ int ans = 0 , ptr1 = 0 , ptr2 = 0 ;
44
45
int diff = sum2 - sum1;
45
46
46
47
while (diff > 0 ) {
@@ -57,4 +58,4 @@ class Solution {
57
58
}
58
59
return ans;
59
60
}
60
- };
61
+ };
0 commit comments