generated from threeal/project-starter
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsolution.cpp
54 lines (47 loc) · 1.3 KB
/
solution.cpp
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
46
47
48
49
50
51
52
53
54
// First of all, nice pairs is if:
//
// nums[i] + rev(nums[j]) == nums[j] + rev(nums[i])
//
// Let's adjust the equation to be:
//
// nums[i] - rev(nums[i]) == nums[j] - rev(nums[j])
//
// So, since everything is in `num - rev(num)`, we can just iterate over each number
// and then store the count of each result of `num - rev(num)`.
//
// After counting each result, we can just sum them together using:
//
// n * (n - 1) / 2
//
// And don't forget to divide it by modulo 10e9+7
#include <unordered_map>
#include <vector>
int rev(int val);
class Solution {
public:
int countNicePairs(std::vector<int>& nums) {
// Count the number of the same result of `num - rev(num)`.
std::unordered_map<int, long> diffsCounts;
for (const auto num : nums) {
++diffsCounts[num - rev(num)];
}
// Calculate the total of each result count using `n * (n - 1) / 2`
// and divide it by modulo 10e9+7.
long totalCount = 0;
for (const auto [diff, count] : diffsCounts) {
totalCount += count * (count - 1) / 2;
totalCount %= 1000000007;
}
return totalCount;
}
};
// This function will reverse the number.
// For example 24 will be reversed to 42.
int rev(int num) {
int revNum = 0;
while (num > 0) {
revNum = revNum * 10 + num % 10;
num /= 10;
}
return revNum;
}