Skip to content

Commit 394b4e1

Browse files
committed
2333 added.
1 parent 0071e36 commit 394b4e1

File tree

3 files changed

+83
-0
lines changed

3 files changed

+83
-0
lines changed
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
cmake_minimum_required(VERSION 3.22)
2+
project(C)
3+
4+
set(CMAKE_CXX_STANDARD 14)
5+
6+
add_executable(C main.cpp)
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
/// Source : https://leetcode.com/problems/minimum-sum-of-squared-difference/
2+
/// Author : liuyubobobo
3+
/// Time : 2022-07-10
4+
5+
#include <iostream>
6+
#include <vector>
7+
#include <algorithm>
8+
9+
using namespace std;
10+
11+
12+
/// Soring and Greedy
13+
/// Time Complexity: O(nlogn)
14+
/// Space Complexity: O(n)
15+
class Solution {
16+
public:
17+
long long minSumSquareDiff(vector<int>& nums1, vector<int>& nums2, int k1, int k2) {
18+
19+
int n = nums1.size();
20+
vector<long long> d(n);
21+
for(int i = 0; i < n; i ++) d[i] = abs(nums1[i] - nums2[i]);
22+
23+
sort(d.begin(), d.end(), greater<int>());
24+
d.push_back(0);
25+
n ++;
26+
27+
long long k = k1 + k2;
28+
29+
long long pre = d[0];
30+
int i;
31+
for(i = 1; i < n; i ++){
32+
long long cur = d[i];
33+
long long diff = pre - cur;
34+
if(diff * i <= k){
35+
k -= diff * i;
36+
pre = cur;
37+
}
38+
else{
39+
break;
40+
}
41+
}
42+
43+
if(i < n){
44+
for(int j = 0; j < i; j ++) d[j] = pre;
45+
46+
for(int j = 0; j < i; j ++){
47+
d[j] -= k / i;
48+
if(j < k % i) d[j] --;
49+
}
50+
}
51+
else return 0;
52+
53+
long long res = 0;
54+
for(int i = 0; i < n; i ++)
55+
res += d[i] * d[i];
56+
return res;
57+
}
58+
};
59+
60+
61+
int main() {
62+
63+
vector<int> nums11 = {1, 2, 3, 4}, nums12 = {2, 10, 20, 19};
64+
cout << Solution().minSumSquareDiff(nums11, nums12, 0, 0) << '\n';
65+
// 579
66+
67+
vector<int> nums21 = {1, 4, 10, 12}, nums22 = {5, 8, 6, 9};
68+
cout << Solution().minSumSquareDiff(nums21, nums22, 1, 1) << '\n';
69+
// 43
70+
71+
vector<int> nums31 = {19, 18, 19, 18, 18, 19, 19}, nums32 = {1, 0, 1, 0, 0, 1, 1};
72+
cout << Solution().minSumSquareDiff(nums31, nums32, 10, 33) << '\n';
73+
// 985
74+
75+
return 0;
76+
}

readme.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2176,6 +2176,7 @@ email: [liuyubobobo@gmail.com](mailto:liuyubobobo@gmail.com)
21762176
| 2327 | [Number of People Aware of a Secret](https://leetcode.com/problems/number-of-people-aware-of-a-secret/) | [] | [C++](2001-2500/2327-Number-of-People-Aware-of-a-Secret/cpp-2327/) | | |
21772177
| 2328 | [Number of Increasing Paths in a Grid](https://leetcode.com/problems/number-of-increasing-paths-in-a-grid/) | [] | [C++](2001-2500/2328-Number-of-Increasing-Paths-in-a-Grid/cpp-2328/) | | |
21782178
| | | | | | |
2179+
| 2333 | [Minimum Sum of Squared Difference](https://leetcode.com/problems/minimum-sum-of-squared-difference/) | [] | [C++](2001-2500/2333-Minimum-Sum-of-Squared-Difference/cpp-2333/) | | |
21792180
| 2334 | [Subarray With Elements Greater Than Varying Threshold](https://leetcode.com/problems/subarray-with-elements-greater-than-varying-threshold/) | [] | [C++](2001-2500/2334-Subarray-With-Elements-Greater-Than-Varying-Threshold/cpp-2334/) | | |
21802181
| 2335 | [Minimum Amount of Time to Fill Cups](https://leetcode.com/problems/minimum-amount-of-time-to-fill-cups/) | [] | [C++](2001-2500/2335-Minimum-Amount-of-Time-to-Fill-Cups/cpp-2335/) | | |
21812182
| 2336 | [Smallest Number in Infinite Set](https://leetcode.com/problems/smallest-number-in-infinite-set/) | [] | [C++](2001-2500/2336-Smallest-Number-in-Infinite-Set/cpp-2336/) | | |

0 commit comments

Comments
 (0)