-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path1865.cpp
More file actions
47 lines (38 loc) · 997 Bytes
/
1865.cpp
File metadata and controls
47 lines (38 loc) · 997 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#include<iostream>
#include <unordered_map>
#include<vector>
class FindSumPairs {
public:
FindSumPairs(std::vector<int>& nums1, std::vector<int>& nums2) : nums1(nums1), nums2(nums2) {
for(int num : nums2) {
freq[num]++;
}
}
void add(int index, int val) {
int oldFreq = nums2[index];
--freq[oldFreq];
nums2[index] += val;
++freq[nums2[index]];
}
int count(int tot) {
int count = 0;
for(int i = 0; i < nums1.size(); ++i) {
int temp = tot - nums1[i];
count += freq[temp];
}
return count;
}
private:
std::vector<int> nums1;
std::vector<int> nums2;
std::unordered_map<int, int> freq;
};
int main() {
std::vector<int> nums1 = { 1, 1, 2, 2, 2, 3 };
std::vector<int> nums2 = { 1, 4, 5, 2, 5, 4 };
FindSumPairs obj(nums1, nums2);
std::cout << obj.count(7);
obj.add(3, 2);
std::cout << obj.count(8);
std::cin.get();
}