Skip to content

Commit 494c2f0

Browse files
committed
Runtime: 438 ms (Top 14.29%) | Memory: 113.8 MB (Top 74.48%)
1 parent d95357d commit 494c2f0

File tree

1 file changed

+15
-14
lines changed

1 file changed

+15
-14
lines changed

scripts/algorithms/E/Equal Sum Arrays With Minimum Number of Operations/Equal Sum Arrays With Minimum Number of Operations.cpp

+15-14
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,25 @@
1+
// Runtime: 438 ms (Top 14.29%) | Memory: 113.8 MB (Top 74.48%)
12
/**
2-
* @brief
3+
* @brief
34
* Given two array, return the minimum numbers we have to change the values to make two arrays sum equal
4-
*
5+
*
56
* [Observation]
67
* Since the value is from 1 ~ 6
7-
*
8+
*
89
* 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+
*
1314
* If there is a answer -> sum s, value will be between s1 and s2
1415
* So, let's say if s1 is smaller, we would like to increase s1's element and decrease s2's element
1516
* -> 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?
1819
* 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.
2021
* -> sorting
21-
*
22+
*
2223
* @algo sorting + greedy
2324
* Time O(NlogN) for sorting
2425
* Space O(1)
@@ -32,15 +33,15 @@ class Solution {
3233
if(6*l1 < l2 || 6*l2 < l1) {
3334
return -1;
3435
}
35-
36+
3637
int sum1 = accumulate(nums1.begin(), nums1.end(), 0);
3738
int sum2 = accumulate(nums2.begin(), nums2.end(), 0);
3839
if(sum1 > sum2) return minOperations(nums2, nums1);
3940

4041
sort(nums1.begin(), nums1.end());
4142
sort(nums2.begin(), nums2.end(), greater<int>());
4243
// let us design the way where sum1 <= sum2
43-
int ans = 0, ptr1 = 0, ptr2 = 0;
44+
int ans = 0, ptr1 = 0, ptr2 = 0;
4445
int diff = sum2 - sum1;
4546

4647
while(diff > 0) {
@@ -57,4 +58,4 @@ class Solution {
5758
}
5859
return ans;
5960
}
60-
};
61+
};

0 commit comments

Comments
 (0)