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 29ac15a

Browse files
Merge pull request #324 from cheehwatang/add-923-3SumWithMultiplicity
Add 923. 3Sum With Multiplicity (Hash Table)
2 parents a4c50bb + 333ee50 commit 29ac15a

File tree

2 files changed

+76
-5
lines changed

2 files changed

+76
-5
lines changed

‎README.md‎

Lines changed: 38 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,18 @@
2727
<th>Solution</th>
2828
<th>Topics</th>
2929
</tr>
30+
<tr>
31+
<td align="center">September 3rd</td>
32+
<td>923. <a href="https://leetcode.com/problems/3sum-with-multiplicity/">3Sum With Multiplicity</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/923.%203Sum%20With%20Multiplicity/ThreeSumWithMultiplicity_HashTable.java">Hash Table</a>
36+
</td>
37+
<td align="center">
38+
<a href="#array">Array</a>,
39+
<a href="#hash-table">Hash Table</a>
40+
</td>
41+
</tr>
3042
<tr>
3143
<td align="center">September 2nd</td>
3244
<td>923. <a href="https://leetcode.com/problems/3sum-with-multiplicity/">3Sum With Multiplicity</a></td>
@@ -954,13 +966,15 @@
954966
<tr>
955967
<td align="center">923</td>
956968
<td><a href="https://leetcode.com/problems/3sum-with-multiplicity/">3Sum With Multiplicity</a></td>
957-
<td align="center">
958-
<a href="https://github.com/cheehwatang/leetcode-java/blob/main/solutions/923.%203Sum%20With%20Multiplicity/ThreeSumWithMultiplicity_Counting.java">Java</a>
969+
<td align="center">Java with
970+
<a href="https://github.com/cheehwatang/leetcode-java/blob/main/solutions/923.%203Sum%20With%20Multiplicity/ThreeSumWithMultiplicity_Counting.java">Counting</a> or
971+
<a href="https://github.com/cheehwatang/leetcode-java/blob/main/solutions/923.%203Sum%20With%20Multiplicity/ThreeSumWithMultiplicity_HashTable.java">Hash Table</a>
959972
</td>
960973
<td align="center">$\text{\color{Dandelion}Medium}$</td>
961974
<td align="center">
962975
<a href="#array">Array</a>,
963-
<a href="#counting">Counting</a>
976+
<a href="#counting">Counting</a>,
977+
<a href="#hash-table">Hash Table</a>
964978
</td>
965979
<td></td>
966980
</tr>
@@ -3291,9 +3305,12 @@
32913305
<td align="center">$\text{\color{Dandelion}Medium}$</td>
32923306
<td align="center">
32933307
<a href="#array">Array</a>,
3294-
<a href="#counting">Counting</a>
3308+
<a href="#counting">Counting</a>,
3309+
<a href="#hash-table">Hash Table</a>
3310+
</td>
3311+
<td>Solution Using
3312+
<a href="https://github.com/cheehwatang/leetcode-java/blob/main/solutions/923.%203Sum%20With%20Multiplicity/ThreeSumWithMultiplicity_HashTable.java"><em>Hash Table</em></a>
32953313
</td>
3296-
<td></td>
32973314
</tr>
32983315
<tr>
32993316
<td align="center">1497</td>
@@ -5241,6 +5258,22 @@
52415258
</td>
52425259
<td></td>
52435260
</tr>
5261+
<tr>
5262+
<td align="center">923</td>
5263+
<td><a href="https://leetcode.com/problems/3sum-with-multiplicity/">3Sum With Multiplicity</a></td>
5264+
<td align="center">
5265+
<a href="https://github.com/cheehwatang/leetcode-java/blob/main/solutions/923.%203Sum%20With%20Multiplicity/ThreeSumWithMultiplicity_HashTable.java">Java</a>
5266+
</td>
5267+
<td align="center">$\text{\color{Dandelion}Medium}$</td>
5268+
<td align="center">
5269+
<a href="#array">Array</a>,
5270+
<a href="#counting">Counting</a>,
5271+
<a href="#hash-table">Hash Table</a>
5272+
</td>
5273+
<td>Solution Using
5274+
<a href="https://github.com/cheehwatang/leetcode-java/blob/main/solutions/923.%203Sum%20With%20Multiplicity/ThreeSumWithMultiplicity_Counting.java"><em>Counting</em></a>
5275+
</td>
5276+
</tr>
52445277
<tr>
52455278
<td align="center">929</td>
52465279
<td><a href="https://leetcode.com/problems/unique-email-addresses/">Unique Email Addresses</a></td>
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package com.cheehwatang.leetcode;
2+
3+
import java.util.HashMap;
4+
import java.util.Map;
5+
6+
// Time Complexity : O(n^2),
7+
// where 'n' is the length of 'arr'.
8+
// We traverse 'arr' in a nested for-loop, checking each triplet for each 'arr[i]'.
9+
//
10+
// Space Complexity : O(n^2),
11+
// where 'n' is the length of 'arr'.
12+
// For each 'arr[i]', there are 'n' number of possible differences to store in the HashTable.
13+
14+
public class ThreeSumWithMultiplicity_HashTable {
15+
16+
// Approach:
17+
// Using a HashTable to record "target - arr[k]" of the equation, then traverse both 'i' and 'j' to check if equal.
18+
// Note that we rearranged the equation as arr[i] + arr[j] == target - arr[k].
19+
// Each loop recording the frequency of "target - arr[k]", add the frequency if equal.
20+
21+
public int threeSumMulti(int[] arr, int target) {
22+
int n = arr.length;
23+
// Store the first value of 'arr[k]' which is arr[n - 1], starting from right to left each iteration.
24+
Map<Integer, Integer> map = new HashMap<>();
25+
map.put(target - arr[n - 1], 1);
26+
long count = 0;
27+
for (int j = n - 2; j >= 0; j--) {
28+
// Add the frequency of "target - arr[k]" to the count if found.
29+
for (int i = j - 1; i >= 0; i--) {
30+
count += map.getOrDefault(arr[i] + arr[j], 0);
31+
}
32+
// Once done with all the 'j' for this loop, increase the frequency for "target - arr[j]".
33+
int difference = target - arr[j];
34+
map.put(difference, map.getOrDefault(difference, 0) + 1);
35+
}
36+
return (int) (count % (1e9 + 7));
37+
}
38+
}

0 commit comments

Comments
(0)

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