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 48fc241

Browse files
Merge pull request #325 from cheehwatang/add-923-3SumWithMultiplicity
Add 923. 3Sum With Multiplicity (Sorting & Two Pointers)
2 parents 29ac15a + 4df6486 commit 48fc241

File tree

2 files changed

+143
-30
lines changed

2 files changed

+143
-30
lines changed

‎README.md‎

Lines changed: 67 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,19 @@
2727
<th>Solution</th>
2828
<th>Topics</th>
2929
</tr>
30+
<tr>
31+
<td align="center">September 4th</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_Sorting_TwoPointers.java">Sorting & Two Pointers</a>
36+
</td>
37+
<td align="center">
38+
<a href="#array">Array</a>,
39+
<a href="#sorting">Sorting</a>,
40+
<a href="#two-pointers">Two Pointers</a>
41+
</td>
42+
</tr>
3043
<tr>
3144
<td align="center">September 3rd</td>
3245
<td>923. <a href="https://leetcode.com/problems/3sum-with-multiplicity/">3Sum With Multiplicity</a></td>
@@ -77,29 +90,6 @@
7790
<a href="#two-pointers">Two Pointers</a>
7891
</td>
7992
</tr>
80-
<tr>
81-
<td align="center">August 30th</td>
82-
<td>121. <a href="https://leetcode.com/problems/best-time-to-buy-and-sell-stock/">Best Time to Buy and Sell Stock</a></td>
83-
<td align="center">$\text{\color{TealBlue}Easy}$</td>
84-
<td align="center">
85-
<a href="https://github.com/cheehwatang/leetcode-java/blob/main/solutions/121.%20Best%20Time%20to%20Buy%20and%20Sell%20Stock/BestTimeToBuyAndSellStock_DynamicProgramming.java">Dynamic Programming</a>
86-
</td>
87-
<td align="center">
88-
<a href="#array">Array</a>,
89-
<a href="#dynamic-programming">Dynamic Programming</a>
90-
</td>
91-
</tr>
92-
<tr>
93-
<td align="center">August 29th</td>
94-
<td>121. <a href="https://leetcode.com/problems/best-time-to-buy-and-sell-stock/">Best Time to Buy and Sell Stock</a></td>
95-
<td align="center">$\text{\color{TealBlue}Easy}$</td>
96-
<td align="center">
97-
<a href="https://github.com/cheehwatang/leetcode-java/blob/main/solutions/121.%20Best%20Time%20to%20Buy%20and%20Sell%20Stock/BestTimeToBuyAndSellStock.java">Array</a>
98-
</td>
99-
<td align="center">
100-
<a href="#array">Array</a>
101-
</td>
102-
</tr>
10393
</table>
10494
</br>
10595
<hr>
@@ -967,14 +957,17 @@
967957
<td align="center">923</td>
968958
<td><a href="https://leetcode.com/problems/3sum-with-multiplicity/">3Sum With Multiplicity</a></td>
969959
<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>
960+
<a href="https://github.com/cheehwatang/leetcode-java/blob/main/solutions/923.%203Sum%20With%20Multiplicity/ThreeSumWithMultiplicity_Counting.java">Counting</a>
961+
<a href="https://github.com/cheehwatang/leetcode-java/blob/main/solutions/923.%203Sum%20With%20Multiplicity/ThreeSumWithMultiplicity_HashTable.java">Hash Table</a> or
962+
<a href="https://github.com/cheehwatang/leetcode-java/blob/main/solutions/923.%203Sum%20With%20Multiplicity/ThreeSumWithMultiplicity_Sorting_TwoPointers.java">Sorting & Two Pointers</a>
972963
</td>
973964
<td align="center">$\text{\color{Dandelion}Medium}$</td>
974965
<td align="center">
975966
<a href="#array">Array</a>,
976967
<a href="#counting">Counting</a>,
977-
<a href="#hash-table">Hash Table</a>
968+
<a href="#hash-table">Hash Table</a>,
969+
<a href="#sorting">Sorting</a>,
970+
<a href="#two-pointers">Two Pointers</a>
978971
</td>
979972
<td></td>
980973
</tr>
@@ -3306,10 +3299,13 @@
33063299
<td align="center">
33073300
<a href="#array">Array</a>,
33083301
<a href="#counting">Counting</a>,
3309-
<a href="#hash-table">Hash Table</a>
3302+
<a href="#hash-table">Hash Table</a>,
3303+
<a href="#sorting">Sorting</a>,
3304+
<a href="#two-pointers">Two Pointers</a>
33103305
</td>
33113306
<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>
3307+
<a href="https://github.com/cheehwatang/leetcode-java/blob/main/solutions/923.%203Sum%20With%20Multiplicity/ThreeSumWithMultiplicity_HashTable.java"><em>Hash Table</em></a> or
3308+
<a href="https://github.com/cheehwatang/leetcode-java/blob/main/solutions/923.%203Sum%20With%20Multiplicity/ThreeSumWithMultiplicity_Sorting_TwoPointers.java"><em>Sorting & Two Pointers</em></a>
33133309
</td>
33143310
</tr>
33153311
<tr>
@@ -5268,10 +5264,13 @@
52685264
<td align="center">
52695265
<a href="#array">Array</a>,
52705266
<a href="#counting">Counting</a>,
5271-
<a href="#hash-table">Hash Table</a>
5267+
<a href="#hash-table">Hash Table</a>,
5268+
<a href="#sorting">Sorting</a>,
5269+
<a href="#two-pointers">Two Pointers</a>
52725270
</td>
52735271
<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>
5272+
<a href="https://github.com/cheehwatang/leetcode-java/blob/main/solutions/923.%203Sum%20With%20Multiplicity/ThreeSumWithMultiplicity_Counting.java"><em>Counting</em></a> or
5273+
<a href="https://github.com/cheehwatang/leetcode-java/blob/main/solutions/923.%203Sum%20With%20Multiplicity/ThreeSumWithMultiplicity_Sorting_TwoPointers.java"><em>Sorting & Two Pointers</em></a>
52755274
</td>
52765275
</tr>
52775276
<tr>
@@ -7587,6 +7586,25 @@
75877586
</td>
75887587
<td></td>
75897588
</tr>
7589+
<tr>
7590+
<td align="center">923</td>
7591+
<td><a href="https://leetcode.com/problems/3sum-with-multiplicity/">3Sum With Multiplicity</a></td>
7592+
<td align="center">
7593+
<a href="https://github.com/cheehwatang/leetcode-java/blob/main/solutions/923.%203Sum%20With%20Multiplicity/ThreeSumWithMultiplicity_Sorting_TwoPointers.java">Java</a>
7594+
</td>
7595+
<td align="center">$\text{\color{Dandelion}Medium}$</td>
7596+
<td align="center">
7597+
<a href="#array">Array</a>,
7598+
<a href="#counting">Counting</a>,
7599+
<a href="#hash-table">Hash Table</a>,
7600+
<a href="#sorting">Sorting</a>,
7601+
<a href="#two-pointers">Two Pointers</a>
7602+
</td>
7603+
<td>Solution Using
7604+
<a href="https://github.com/cheehwatang/leetcode-java/blob/main/solutions/923.%203Sum%20With%20Multiplicity/ThreeSumWithMultiplicity_Counting.java"><em>Counting</em></a>
7605+
<a href="https://github.com/cheehwatang/leetcode-java/blob/main/solutions/923.%203Sum%20With%20Multiplicity/ThreeSumWithMultiplicity_HashTable.java"><em>Hash Table</em></a>
7606+
</td>
7607+
</tr>
75907608
<tr>
75917609
<td align="center">948</td>
75927610
<td><a href="https://leetcode.com/problems/bag-of-tokens/">Bag of Tokens</a></td>
@@ -9570,6 +9588,25 @@
95709588
</td>
95719589
<td></td>
95729590
</tr>
9591+
<tr>
9592+
<td align="center">923</td>
9593+
<td><a href="https://leetcode.com/problems/3sum-with-multiplicity/">3Sum With Multiplicity</a></td>
9594+
<td align="center">
9595+
<a href="https://github.com/cheehwatang/leetcode-java/blob/main/solutions/923.%203Sum%20With%20Multiplicity/ThreeSumWithMultiplicity_Sorting_TwoPointers.java">Java</a>
9596+
</td>
9597+
<td align="center">$\text{\color{Dandelion}Medium}$</td>
9598+
<td align="center">
9599+
<a href="#array">Array</a>,
9600+
<a href="#counting">Counting</a>,
9601+
<a href="#hash-table">Hash Table</a>,
9602+
<a href="#sorting">Sorting</a>,
9603+
<a href="#two-pointers">Two Pointers</a>
9604+
</td>
9605+
<td>Solution Using
9606+
<a href="https://github.com/cheehwatang/leetcode-java/blob/main/solutions/923.%203Sum%20With%20Multiplicity/ThreeSumWithMultiplicity_Counting.java"><em>Counting</em></a>
9607+
<a href="https://github.com/cheehwatang/leetcode-java/blob/main/solutions/923.%203Sum%20With%20Multiplicity/ThreeSumWithMultiplicity_HashTable.java"><em>Hash Table</em></a>
9608+
</td>
9609+
</tr>
95739610
<tr>
95749611
<td align="center">948</td>
95759612
<td><a href="https://leetcode.com/problems/bag-of-tokens/">Bag of Tokens</a></td>
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
package com.cheehwatang.leetcode;
2+
3+
import java.util.Arrays;
4+
5+
// Time Complexity : O(n^2),
6+
// where 'n' is the length of 'arr'.
7+
// We traverse 'arr'. With each 'arr[i]', we use two pointers to traverse to find the triplets.
8+
// Arrays.sort() function has a time complexity of O(n logn).
9+
//
10+
// Space Complexity : O(1),
11+
// as the auxiliary space used is independent of the input 'arr'.
12+
13+
public class ThreeSumWithMultiplicity_Sorting_TwoPointers {
14+
15+
// Approach:
16+
// Using sorting and two pointers to traverse the sorted array and check each number if they sum to 'target'.
17+
// Note that, i < j < k and arr[i] + arr[j] + arr[k] == target,
18+
// do not require the index as the sum is not dependent on the index, so sorting is fine.
19+
20+
public int threeSumMulti(int[] arr, int target) {
21+
int n = arr.length;
22+
Arrays.sort(arr);
23+
long count = 0;
24+
25+
// With each 'i', use two pointers 'j' and 'k' to traverse the remaining numbers to the right of 'i'.
26+
for (int i = 0; i < n - 2; i++) {
27+
// If the smallest number is greater than 'target',
28+
// then the remaining numbers (which are greater) are impossible to sum to be equal to 'target'.
29+
if (arr[i] + arr[i + 1] + arr[i + 2] > target) break;
30+
// If the largest number is less than 'target', meaning we need to increase 'i' to get a larger number.
31+
if (arr[i] + arr[n - 1] + arr[n - 2] < target) continue;
32+
33+
// After the check, traverse with 'j' and 'k' from both ends.
34+
int j = i + 1;
35+
int k = n - 1;
36+
while (j < k) {
37+
int sum = arr[i] + arr[j] + arr[k];
38+
// Move 'j' to right if sum is less than target.
39+
if (sum < target) j++;
40+
// Move 'k' to left if sum is greater than target.
41+
else if (sum > target) k--;
42+
// If it is equal, keep track of the frequency for both 'arr[j]' and 'arr[k]'.
43+
else {
44+
int countJ = 1;
45+
while (j < k && arr[j] == arr[j + 1]) {
46+
j++;
47+
countJ++;
48+
}
49+
int countK = 1;
50+
while (j < k && arr[k] == arr[k - 1]) {
51+
k--;
52+
countK++;
53+
}
54+
// If both are same, get the geometric sum for 'countJ' + 'countK' - 1.
55+
// Example: [1,2,2,2,2], target = 5. With arr[i] = 1, arr[j] == arr[k] = 2.
56+
// Note that the "j < k" will break with 'j' overlapping with 'k'.
57+
// Thus, 'countJ' = 4 and 'countK' = 1.
58+
// 'countJ' + 'countK' - 1 = 4, which results in 4 * 3 / 2 = 6 possible combinations.
59+
if (arr[j] == arr[k]) {
60+
int frequency = countJ + countK - 1;
61+
count += (long) frequency * (frequency - 1) / 2;
62+
}
63+
// If they are different, the count increase by the multiplication for both.
64+
// Example: [1,2,2,3,3], target = 6. With arr[i] = 1, arr[j] = 2 and arr[k] = 3.
65+
// There are 2 * 2 = 4 possible combinations for the 2 and 3.
66+
else
67+
count += (long) countJ * countK;
68+
// Once done, move both pointers to continue check the other numbers.
69+
j++;
70+
k--;
71+
}
72+
}
73+
}
74+
return (int) (count % (1e9 + 7));
75+
}
76+
}

0 commit comments

Comments
(0)

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