Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 38 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,18 @@
<th>Solution</th>
<th>Topics</th>
</tr>
<tr>
<td align="center">September 2nd</td>
<td>923. <a href="https://leetcode.com/problems/3sum-with-multiplicity/">3Sum With Multiplicity</a></td>
<td align="center">$\text{\color{Dandelion}Medium}$</td>
<td align="center">
<a href="https://github.com/cheehwatang/leetcode-java/blob/main/solutions/923.%203Sum%20With%20Multiplicity/ThreeSumWithMultiplicity_Counting.java">Counting</a>
</td>
<td align="center">
<a href="#array">Array</a>,
<a href="#counting">Counting</a>
</td>
</tr>
<tr>
<td align="center">September 1st</td>
<td>16. <a href="https://leetcode.com/problems/3sum-closest/">3Sum Closest</a></td>
Expand Down Expand Up @@ -76,19 +88,6 @@
<a href="#array">Array</a>
</td>
</tr>
<tr>
<td align="center">August 28th</td>
<td>3. <a href="https://leetcode.com/problems/longest-substring-without-repeating-characters/">Longest Substring Without Repeating Characters</a></td>
<td align="center">$\text{\color{Dandelion}Medium}$</td>
<td align="center">
<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>
</td>
<td align="center">
<a href="#hash-table">Hash Table</a>,
<a href="#sliding-window">Sliding Window</a>,
<a href="#string">String</a>
</td>
</tr>
</table>
</br>
<hr>
Expand Down Expand Up @@ -952,6 +951,19 @@
</td>
<td></td>
</tr>
<tr>
<td align="center">923</td>
<td><a href="https://leetcode.com/problems/3sum-with-multiplicity/">3Sum With Multiplicity</a></td>
<td align="center">
<a href="https://github.com/cheehwatang/leetcode-java/blob/main/solutions/923.%203Sum%20With%20Multiplicity/ThreeSumWithMultiplicity_Counting.java">Java</a>
</td>
<td align="center">$\text{\color{Dandelion}Medium}$</td>
<td align="center">
<a href="#array">Array</a>,
<a href="#counting">Counting</a>
</td>
<td></td>
</tr>
<tr>
<td align="center">929</td>
<td><a href="https://leetcode.com/problems/unique-email-addresses/">Unique Email Addresses</a></td>
Expand Down Expand Up @@ -3270,6 +3282,19 @@
<a href="https://github.com/cheehwatang/leetcode-java/blob/main/solutions/383.%20Ransom%20Note/RansomNote_HashTable.java"><em>Hash Table</em></a>
</td>
</tr>
<tr>
<td align="center">923</td>
<td><a href="https://leetcode.com/problems/3sum-with-multiplicity/">3Sum With Multiplicity</a></td>
<td align="center">
<a href="https://github.com/cheehwatang/leetcode-java/blob/main/solutions/923.%203Sum%20With%20Multiplicity/ThreeSumWithMultiplicity_Counting.java">Java</a>
</td>
<td align="center">$\text{\color{Dandelion}Medium}$</td>
<td align="center">
<a href="#array">Array</a>,
<a href="#counting">Counting</a>
</td>
<td></td>
</tr>
<tr>
<td align="center">1497</td>
<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>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package com.cheehwatang.leetcode;

// Time Complexity : O(n),
// where 'n' is the length of 'arr'.
// We traverse 'arr' once to count the frequency of the elements.
//
// Space Complexity : O(1),
// as the auxiliary space used is independent of the input 'arr',
// with the counting array with size of 101 regardless of the input 'arr'.

public class ThreeSumWithMultiplicity_Counting {

// Approach:
// Using a counting array, get the frequency of all the integers,
// then check all the possible combinations to get the sum.
// Note that, i < j < k and arr[i] + arr[j] + arr[k] == target,
// do not require the index as the sum is not dependent on the index.

public int threeSumMulti(int[] arr, int target) {

// With the constraint of 0 <= arr[i] <= 100, count the frequency of all the integers/
int[] counting = new int[101];
for (int integer : arr) counting[integer]++;

// Keep the count as long type, so as only need to perform the modulo once at the end.
long count = 0;

// Traverse the counting array in two for-loops to get the value for 'i' and 'j'.
for (int i = 0; i <= 100; i++) {
for (int j = i; j <= 100; j++) {
// Get the k integer to check if it is in the counting array.
int k = target - i - j;
if (k < 0 || k > 100) continue;
if (counting[k] > 0) {
// If the triplet is of the same integers (eg. [1,1,1], target = 3),
// then perform geometric sum for 3 elements.
if (i == k && j == k)
count += (long) counting[i] * (counting[i] - 1) * (counting[i] - 2) / 6;
// If i and j is identical, but different from k (eg. [1,1,2], target = 4),
// then perform geometric sum for i & j, then multiply with k.
else if (i == j)
count += (long) counting[i] * (counting[i] - 1) / 2 * counting[k];
// If both i and j is smaller than k, then get the multiples of all the frequency.
// This is to make sure no duplicates of results
// (eg, [1,2,3], target = 6), only [1,2,3] is counted, but not [2,1,3] or [3,2,1], etc.)
else if (i < k && j < k)
count += (long) counting[i] * counting[j] * counting[k];
}
}
}
return (int) (count % (1e9 + 7));
}
}