|
| 1 | +// Source : https://leetcode.com/problems/count-nice-pairs-in-an-array/ |
| 2 | +// Author : Hao Chen |
| 3 | +// Date : 2021-04-06 |
| 4 | + |
| 5 | +/***************************************************************************************************** |
| 6 | + * |
| 7 | + * You are given an array nums that consists of non-negative integers. Let us define rev(x) as the |
| 8 | + * reverse of the non-negative integer x. For example, rev(123) = 321, and rev(120) = 21. A pair of |
| 9 | + * indices (i, j) is nice if it satisfies all of the following conditions: |
| 10 | + * |
| 11 | + * 0 <= i < j < nums.length |
| 12 | + * nums[i] + rev(nums[j]) == nums[j] + rev(nums[i]) |
| 13 | + * |
| 14 | + * Return the number of nice pairs of indices. Since that number can be too large, return it modulo |
| 15 | + * 10^9 + 7. |
| 16 | + * |
| 17 | + * Example 1: |
| 18 | + * |
| 19 | + * Input: nums = [42,11,1,97] |
| 20 | + * Output: 2 |
| 21 | + * Explanation: The two pairs are: |
| 22 | + * - (0,3) : 42 + rev(97) = 42 + 79 = 121, 97 + rev(42) = 97 + 24 = 121. |
| 23 | + * - (1,2) : 11 + rev(1) = 11 + 1 = 12, 1 + rev(11) = 1 + 11 = 12. |
| 24 | + * |
| 25 | + * Example 2: |
| 26 | + * |
| 27 | + * Input: nums = [13,10,35,24,76] |
| 28 | + * Output: 4 |
| 29 | + * |
| 30 | + * Constraints: |
| 31 | + * |
| 32 | + * 1 <= nums.length <= 10^5 |
| 33 | + * 0 <= nums[i] <= 10^9 |
| 34 | + ******************************************************************************************************/ |
| 35 | + |
| 36 | +class Solution { |
| 37 | +private: |
| 38 | + int rev(int n) { |
| 39 | + int x = 0; |
| 40 | + while(n > 0) { |
| 41 | + x = x*10 + (n % 10); |
| 42 | + n /= 10; |
| 43 | + } |
| 44 | + return x; |
| 45 | + } |
| 46 | + |
| 47 | +public: |
| 48 | + int countNicePairs(vector<int>& nums) { |
| 49 | + return countNicePairs02(nums); |
| 50 | + return countNicePairs01(nums); |
| 51 | + } |
| 52 | + int countNicePairs01(vector<int>& nums) { |
| 53 | + // suppose n' = rev(n) |
| 54 | + // define: a + b' == b + a' |
| 55 | + // then: a - a' == b - b' |
| 56 | + |
| 57 | + unordered_map<int, int> stat; |
| 58 | + for(auto& n : nums) { |
| 59 | + stat[n-rev(n)]++; |
| 60 | + |
| 61 | + } |
| 62 | + |
| 63 | + //if there are n elements has same value, |
| 64 | + // then there are n*(n-1)/2 unique pairs. |
| 65 | + int result = 0; |
| 66 | + for(auto& [n, cnt] : stat) { |
| 67 | + result = (result + cnt * (cnt -1l) / 2) % 1000000007; |
| 68 | + } |
| 69 | + return result; |
| 70 | + } |
| 71 | + |
| 72 | + int countNicePairs02(vector<int>& nums) { |
| 73 | + // suppose n' = rev(n) |
| 74 | + // define: a + b' == b + a' |
| 75 | + // then: a - a' == b - b' |
| 76 | + int result = 0; |
| 77 | + unordered_map<int, int> stat; |
| 78 | + for(auto& n : nums) { |
| 79 | + int delta = n-rev(n); |
| 80 | + stat[delta]++; |
| 81 | + result = (result + (stat[delta] - 1l)) % 1000000007 ; |
| 82 | + } |
| 83 | + |
| 84 | + return result ; |
| 85 | + } |
| 86 | +}; |
0 commit comments