Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit b88a113

Browse files
Merge pull request #334 from cheehwatang/add-1814-CountNicePairsInAnArray
Add 1814. Count Nice Pairs in an Array
2 parents 9b066da + 37ed974 commit b88a113

File tree

2 files changed

+124
-11
lines changed

2 files changed

+124
-11
lines changed

‎README.md‎

Lines changed: 74 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,20 @@
2727
<th>Solution</th>
2828
<th>Topics</th>
2929
</tr>
30+
<tr>
31+
<td align="center">September 13th</td>
32+
<td>1814. <a href="https://leetcode.com/problems/count-nice-pairs-in-an-array/">Count Nice Pairs in an Array</a></td>
33+
<td align="center">$\text{\color{Dandelion}Medium}$</td>
34+
<td align="center">
35+
<a href="https://github.com/cheehwatang/leetcode-java/blob/main/solutions/1814.%20Count%20Nice%20Pairs%20in%20an%20Array/CountNicePairsInAnArray.java">Counting & Hash Table</a>
36+
</td>
37+
<td align="center">
38+
<a href="#array">Array</a>,
39+
<a href="#counting">Counting</a>,
40+
<a href="#hash-table">Hash Table</a>,
41+
<a href="#math">Math</a>
42+
</td>
43+
</tr>
3044
<tr>
3145
<td align="center">September 12th</td>
3246
<td>2006. <a href="https://leetcode.com/problems/count-number-of-pairs-with-absolute-difference-k/">Count Number of Pairs With Absolute Difference K</a></td>
@@ -75,17 +89,6 @@
7589
<a href="#simulation">Simulation</a>
7690
</td>
7791
</tr>
78-
<tr>
79-
<td align="center">September 8th</td>
80-
<td>1155. <a href="https://leetcode.com/problems/number-of-dice-rolls-with-target-sum/">Number of Dice Rolls With Target Sum</a></td>
81-
<td align="center">$\text{\color{Dandelion}Medium}$</td>
82-
<td align="center">
83-
<a href="https://github.com/cheehwatang/leetcode-java/blob/main/solutions/1155.%20Number%20of%20Dice%20Rolls%20With%20Target%20Sum/NumberOfDiceRollsWithTargetSum_Memoization.java">Dynamic Programming - Memoization</a>
84-
</td>
85-
<td align="center">
86-
<a href="#dynamic-programming">Dynamic Programming</a>
87-
</td>
88-
</tr>
8992
</table>
9093
</br>
9194
<hr>
@@ -1451,6 +1454,21 @@
14511454
</td>
14521455
<td></td>
14531456
</tr>
1457+
<tr>
1458+
<td align="center">1814</td>
1459+
<td><a href="https://leetcode.com/problems/count-nice-pairs-in-an-array/">Count Nice Pairs in an Array</a></td>
1460+
<td align="center">
1461+
<a href="https://github.com/cheehwatang/leetcode-java/blob/main/solutions/1814.%20Count%20Nice%20Pairs%20in%20an%20Array/CountNicePairsInAnArray.java">Java</a>
1462+
</td>
1463+
<td align="center">$\text{\color{Dandelion}Medium}$</td>
1464+
<td align="center">
1465+
<a href="#array">Array</a>,
1466+
<a href="#counting">Counting</a>,
1467+
<a href="#hash-table">Hash Table</a>,
1468+
<a href="#math">Math</a>
1469+
</td>
1470+
<td></td>
1471+
</tr>
14541472
<tr>
14551473
<td align="center">1833</td>
14561474
<td><a href="https://leetcode.com/problems/maximum-ice-cream-bars/">Maximum Ice Cream Bars</a></td>
@@ -3377,6 +3395,21 @@
33773395
</td>
33783396
<td></td>
33793397
</tr>
3398+
<tr>
3399+
<td align="center">1814</td>
3400+
<td><a href="https://leetcode.com/problems/count-nice-pairs-in-an-array/">Count Nice Pairs in an Array</a></td>
3401+
<td align="center">
3402+
<a href="https://github.com/cheehwatang/leetcode-java/blob/main/solutions/1814.%20Count%20Nice%20Pairs%20in%20an%20Array/CountNicePairsInAnArray.java">Java</a>
3403+
</td>
3404+
<td align="center">$\text{\color{Dandelion}Medium}$</td>
3405+
<td align="center">
3406+
<a href="#array">Array</a>,
3407+
<a href="#counting">Counting</a>,
3408+
<a href="#hash-table">Hash Table</a>,
3409+
<a href="#math">Math</a>
3410+
</td>
3411+
<td></td>
3412+
</tr>
33803413
<tr>
33813414
<td align="center">2006</td>
33823415
<td><a href="https://leetcode.com/problems/count-number-of-pairs-with-absolute-difference-k/">Count Number of Pairs With Absolute Difference K</a></td>
@@ -5515,6 +5548,21 @@
55155548
</td>
55165549
<td></td>
55175550
</tr>
5551+
<tr>
5552+
<td align="center">1814</td>
5553+
<td><a href="https://leetcode.com/problems/count-nice-pairs-in-an-array/">Count Nice Pairs in an Array</a></td>
5554+
<td align="center">
5555+
<a href="https://github.com/cheehwatang/leetcode-java/blob/main/solutions/1814.%20Count%20Nice%20Pairs%20in%20an%20Array/CountNicePairsInAnArray.java">Java</a>
5556+
</td>
5557+
<td align="center">$\text{\color{Dandelion}Medium}$</td>
5558+
<td align="center">
5559+
<a href="#array">Array</a>,
5560+
<a href="#counting">Counting</a>,
5561+
<a href="#hash-table">Hash Table</a>,
5562+
<a href="#math">Math</a>
5563+
</td>
5564+
<td></td>
5565+
</tr>
55185566
<tr>
55195567
<td align="center">1832</td>
55205568
<td><a href="https://leetcode.com/problems/check-if-the-sentence-is-pangram/">Check if the Sentence is Pangram</a></td>
@@ -6459,6 +6507,21 @@
64596507
<a href="https://github.com/cheehwatang/leetcode-java/blob/main/solutions/1680.%20Concatenation%20of%20Consecutive%20Binary%20Numbers/ConcatenationOfConsecutiveBinaryNumbers_BitManipulation.java"><em>Bit Manipulation</em></a>
64606508
</td>
64616509
</tr>
6510+
<tr>
6511+
<td align="center">1814</td>
6512+
<td><a href="https://leetcode.com/problems/count-nice-pairs-in-an-array/">Count Nice Pairs in an Array</a></td>
6513+
<td align="center">
6514+
<a href="https://github.com/cheehwatang/leetcode-java/blob/main/solutions/1814.%20Count%20Nice%20Pairs%20in%20an%20Array/CountNicePairsInAnArray.java">Java</a>
6515+
</td>
6516+
<td align="center">$\text{\color{Dandelion}Medium}$</td>
6517+
<td align="center">
6518+
<a href="#array">Array</a>,
6519+
<a href="#counting">Counting</a>,
6520+
<a href="#hash-table">Hash Table</a>,
6521+
<a href="#math">Math</a>
6522+
</td>
6523+
<td></td>
6524+
</tr>
64626525
<tr>
64636526
<td align="center">1979</td>
64646527
<td><a href="https://leetcode.com/problems/find-greatest-common-divisor-of-array/">Find Greatest Common Divisor of Array</a></td>
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
package com.cheehwatang.leetcode;
2+
3+
import java.util.HashMap;
4+
import java.util.Map;
5+
6+
// Time Complexity : O(n),
7+
// where 'n' is the length of 'nums'.
8+
// We traverse 'nums' to determine and count the relative values.
9+
//
10+
// Space Complexity : O(n),
11+
// where 'n' is the length of 'nums'.
12+
// We use a Hash Table to count the relative values, with a maximum size of 'n'.
13+
14+
public class CountNicePairsInAnArray {
15+
16+
// Approach:
17+
// First we need to understand the bad pair, i < j and nums[i] + rev(nums[j]) == nums[j] + rev(nums[i]).
18+
// With the brute force, we can check each and every pair if it is a nice pair,
19+
// but the algorithm will exceed the time limit.
20+
// Thus, let's change the equation for the bad pair as follows:
21+
// nums[i] - rev(nums[i]) == nums[j] - rev(nums[j]).
22+
// Use a HashMap to record the nums[i] - rev(nums[i]) for all the numbers, and the frequency.
23+
24+
public int countNicePairs(int[] nums) {
25+
26+
// Use long type, so as not the keep doing the modulo function, only need to do once at the end.
27+
long nicePairs = 0;
28+
Map<Integer, Integer> map = new HashMap<>();
29+
30+
for (int number : nums) {
31+
// Calculate the difference (relative value) between the nums[i] and rev(nums[i]).
32+
int relative = number - reverse(number);
33+
// If the map already had numbers with the same relative value, then add the frequency as the nice pairs.
34+
nicePairs += map.getOrDefault(relative, 0);
35+
// Increase the count in the map.
36+
map.put(relative, map.getOrDefault(relative, 0) + 1);
37+
}
38+
return (int) (nicePairs % (1e9 + 7));
39+
}
40+
41+
// Method to reverse the number.
42+
private int reverse(int number) {
43+
int reverse = 0;
44+
while (number > 0) {
45+
reverse = reverse * 10 + number % 10;
46+
number /= 10;
47+
}
48+
return reverse;
49+
}
50+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /