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 a4c50bb

Browse files
Merge pull request #323 from cheehwatang/add-923-3SumWithMultiplicity
Add 923. 3Sum With Multiplicity (Counting)
2 parents a07afec + cb2b7d7 commit a4c50bb

File tree

2 files changed

+91
-13
lines changed

2 files changed

+91
-13
lines changed

‎README.md‎

Lines changed: 38 additions & 13 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 2nd</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_Counting.java">Counting</a>
36+
</td>
37+
<td align="center">
38+
<a href="#array">Array</a>,
39+
<a href="#counting">Counting</a>
40+
</td>
41+
</tr>
3042
<tr>
3143
<td align="center">September 1st</td>
3244
<td>16. <a href="https://leetcode.com/problems/3sum-closest/">3Sum Closest</a></td>
@@ -76,19 +88,6 @@
7688
<a href="#array">Array</a>
7789
</td>
7890
</tr>
79-
<tr>
80-
<td align="center">August 28th</td>
81-
<td>3. <a href="https://leetcode.com/problems/longest-substring-without-repeating-characters/">Longest Substring Without Repeating Characters</a></td>
82-
<td align="center">$\text{\color{Dandelion}Medium}$</td>
83-
<td align="center">
84-
<a href="https://github.com/cheehwatang/leetcode-java/blob/main/solutions/3.%20Longest%20Substring%20Without%20Repeating%20Characters/LongestSubstringWithoutRepeatingCharacters_HashTable.java">Hash Table & Sliding Window</a>
85-
</td>
86-
<td align="center">
87-
<a href="#hash-table">Hash Table</a>,
88-
<a href="#sliding-window">Sliding Window</a>,
89-
<a href="#string">String</a>
90-
</td>
91-
</tr>
9291
</table>
9392
</br>
9493
<hr>
@@ -952,6 +951,19 @@
952951
</td>
953952
<td></td>
954953
</tr>
954+
<tr>
955+
<td align="center">923</td>
956+
<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>
959+
</td>
960+
<td align="center">$\text{\color{Dandelion}Medium}$</td>
961+
<td align="center">
962+
<a href="#array">Array</a>,
963+
<a href="#counting">Counting</a>
964+
</td>
965+
<td></td>
966+
</tr>
955967
<tr>
956968
<td align="center">929</td>
957969
<td><a href="https://leetcode.com/problems/unique-email-addresses/">Unique Email Addresses</a></td>
@@ -3270,6 +3282,19 @@
32703282
<a href="https://github.com/cheehwatang/leetcode-java/blob/main/solutions/383.%20Ransom%20Note/RansomNote_HashTable.java"><em>Hash Table</em></a>
32713283
</td>
32723284
</tr>
3285+
<tr>
3286+
<td align="center">923</td>
3287+
<td><a href="https://leetcode.com/problems/3sum-with-multiplicity/">3Sum With Multiplicity</a></td>
3288+
<td align="center">
3289+
<a href="https://github.com/cheehwatang/leetcode-java/blob/main/solutions/923.%203Sum%20With%20Multiplicity/ThreeSumWithMultiplicity_Counting.java">Java</a>
3290+
</td>
3291+
<td align="center">$\text{\color{Dandelion}Medium}$</td>
3292+
<td align="center">
3293+
<a href="#array">Array</a>,
3294+
<a href="#counting">Counting</a>
3295+
</td>
3296+
<td></td>
3297+
</tr>
32733298
<tr>
32743299
<td align="center">1497</td>
32753300
<td><a href="https://leetcode.com/problems/check-if-array-pairs-are-divisible-by-k/">Check If Array Pairs Are Divisible by k</a></td>
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
package com.cheehwatang.leetcode;
2+
3+
// Time Complexity : O(n),
4+
// where 'n' is the length of 'arr'.
5+
// We traverse 'arr' once to count the frequency of the elements.
6+
//
7+
// Space Complexity : O(1),
8+
// as the auxiliary space used is independent of the input 'arr',
9+
// with the counting array with size of 101 regardless of the input 'arr'.
10+
11+
public class ThreeSumWithMultiplicity_Counting {
12+
13+
// Approach:
14+
// Using a counting array, get the frequency of all the integers,
15+
// then check all the possible combinations to get the sum.
16+
// Note that, i < j < k and arr[i] + arr[j] + arr[k] == target,
17+
// do not require the index as the sum is not dependent on the index.
18+
19+
public int threeSumMulti(int[] arr, int target) {
20+
21+
// With the constraint of 0 <= arr[i] <= 100, count the frequency of all the integers/
22+
int[] counting = new int[101];
23+
for (int integer : arr) counting[integer]++;
24+
25+
// Keep the count as long type, so as only need to perform the modulo once at the end.
26+
long count = 0;
27+
28+
// Traverse the counting array in two for-loops to get the value for 'i' and 'j'.
29+
for (int i = 0; i <= 100; i++) {
30+
for (int j = i; j <= 100; j++) {
31+
// Get the k integer to check if it is in the counting array.
32+
int k = target - i - j;
33+
if (k < 0 || k > 100) continue;
34+
if (counting[k] > 0) {
35+
// If the triplet is of the same integers (eg. [1,1,1], target = 3),
36+
// then perform geometric sum for 3 elements.
37+
if (i == k && j == k)
38+
count += (long) counting[i] * (counting[i] - 1) * (counting[i] - 2) / 6;
39+
// If i and j is identical, but different from k (eg. [1,1,2], target = 4),
40+
// then perform geometric sum for i & j, then multiply with k.
41+
else if (i == j)
42+
count += (long) counting[i] * (counting[i] - 1) / 2 * counting[k];
43+
// If both i and j is smaller than k, then get the multiples of all the frequency.
44+
// This is to make sure no duplicates of results
45+
// (eg, [1,2,3], target = 6), only [1,2,3] is counted, but not [2,1,3] or [3,2,1], etc.)
46+
else if (i < k && j < k)
47+
count += (long) counting[i] * counting[j] * counting[k];
48+
}
49+
}
50+
}
51+
return (int) (count % (1e9 + 7));
52+
}
53+
}

0 commit comments

Comments
(0)

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