Welcome to Subscribe On Youtube
Formatted question description: https://leetcode.ca/all/923.html
923. 3Sum With Multiplicity (Medium)
Given an integer array A
, and an integer target
, return the number of tuples i, j, k
such that i < j < k
and A[i] + A[j] + A[k] == target
.
As the answer can be very large, return it modulo 10^9 + 7
.
Example 1:
Input: A = [1,1,2,2,3,3,4,4,5,5], target = 8 Output: 20 Explanation: Enumerating by the values (A[i], A[j], A[k]): (1, 2, 5) occurs 8 times; (1, 3, 4) occurs 8 times; (2, 2, 4) occurs 2 times; (2, 3, 3) occurs 2 times.
Example 2:
Input: A = [1,1,2,2,2,2], target = 5 Output: 12 Explanation: A[i] = 1, A[j] = A[k] = 2 occurs 12 times: We choose one 1 from [1,1] in 2 ways, and two 2s from [2,2,2,2] in 6 ways.
Note:
3 <= A.length <= 3000
0 <= A[i] <= 100
0 <= target <= 300
Companies:
Quora
Related Topics:
Two Pointers
Solution 1.
-
class Solution { public int threeSumMulti(int[] A, int target) { final int MODULO = 1000000007; long count = 0; Arrays.sort(A); int length = A.length; int leftStart = 0, leftEnd = length - 3; for (int left = leftStart; left <= leftEnd; left++) { if (A[left] > target) break; int remainTarget = target - A[left]; int mid = left + 1, right = length - 1; while (mid < right) { int sum = A[mid] + A[right]; if (sum == remainTarget) { if (A[mid] != A[right]) { int midCount = 1, rightCount = 1; while (mid + 1 < right && A[mid] == A[mid + 1]) { mid++; midCount++; } while (right - 1 > mid && A[right] == A[right - 1]) { right--; rightCount++; } count += midCount * rightCount; count %= MODULO; mid++; right--; } else { int curCount = right - mid + 1; count += curCount * (curCount - 1) / 2; count %= MODULO; break; } } else if (sum < remainTarget) mid++; else right--; } } return (int) count; } } ############ class Solution { private static final int MOD = (int) 1e9 + 7; public int threeSumMulti(int[] arr, int target) { int[] cnt = new int[101]; for (int v : arr) { ++cnt[v]; } long ans = 0; for (int j = 0; j < arr.length; ++j) { int b = arr[j]; --cnt[b]; for (int i = 0; i < j; ++i) { int a = arr[i]; int c = target - a - b; if (c >= 0 && c <= 100) { ans = (ans + cnt[c]) % MOD; } } } return (int) ans; } }
-
// OJ: https://leetcode.com/problems/3sum-with-multiplicity/solution/ // Time: O(N^2) // Space: O(N) class Solution { public: int threeSumMulti(vector<int>& A, int target) { unordered_map<int, long> m; long ans = 0, mod = 1e9 + 7; for (int n : A) m[n]++; for (auto &[a, ca] : m) { for (auto &[b, cb] : m) { int c = target - a - b; if (m.count(c) == 0) continue; int cc = m[c]; if (a == b && b == c) ans += ca * (ca - 1) * (ca - 2) / 6; // all three are equal else if (a == b && b != c) ans += ca * (ca - 1) / 2 * cc; // first two are equal and the 3rd is not else if (a < b && b < c) ans += ca * cb * cc; // all three are not equal } } return ans % mod; } };
-
class Solution: def threeSumMulti(self, arr: List[int], target: int) -> int: cnt = Counter(arr) ans = 0 mod = 10**9 + 7 for j, b in enumerate(arr): cnt[b] -= 1 for i in range(j): a = arr[i] c = target - a - b ans = (ans + cnt[c]) % mod return ans ############ class Solution(object): def threeSumMulti(self, A, target): """ :type A: List[int] :type target: int :rtype: int """ count = collections.Counter(A) Aset = set(A) Alist = list(Aset) Alist.sort() _lenA = len(Alist) res = 0 for i in range(_lenA): for j in range(i, _lenA): c = target - Alist[i] - Alist[j] if c >= Alist[j] and c in Aset: if Alist[i] != Alist[j] != c: res += count[Alist[i]] * count[Alist[j]] * count[c] elif Alist[i] == Alist[j] and Alist[j] != c: res += count[c] * self.caculate(count[Alist[i]], 2) elif Alist[j] == c and Alist[i] != Alist[j]: res += count[Alist[i]] * self.caculate(count[Alist[j]], 2) elif Alist[i] == c and Alist[j] != c: res += count[Alist[j]] * self.caculate(count[c], 2) else: res += self.caculate(count[Alist[i]], 3) return res % (10 ** 9 + 7) def caculate(self, x, i): if i == 2: return x * (x - 1) / 2 elif i == 3: return x * (x - 1) * (x - 2) / 6
-
func threeSumMulti(arr []int, target int) int { const mod int = 1e9 + 7 cnt := [101]int{} for _, v := range arr { cnt[v]++ } ans := 0 for j, b := range arr { cnt[b]-- for i := 0; i < j; i++ { a := arr[i] c := target - a - b if c >= 0 && c <= 100 { ans += cnt[c] ans %= mod } } } return ans }