Skip to content

Commit a91b87c

Browse files
committed
923
1 parent afd4201 commit a91b87c

File tree

1 file changed

+22
-0
lines changed

1 file changed

+22
-0
lines changed

code/923.3-sum-with-multiplicity.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
#
2+
# @lc app=leetcode id=923 lang=python3
3+
#
4+
# [923] 3Sum With Multiplicity
5+
#
6+
7+
# @lc code=start
8+
class Solution:
9+
def threeSumMulti(self, arr: List[int], target: int) -> int:
10+
cnt, ans = collections.Counter(arr), 0
11+
for i in cnt:
12+
for j in cnt:
13+
k = target - i - j
14+
if i == j == k:
15+
ans += cnt[i] * (cnt[i] - 1) * (cnt[i] - 2) // 6
16+
elif i == j:
17+
ans += cnt[i] * (cnt[i] - 1) // 2 * cnt[k]
18+
elif i < j < k:
19+
ans += cnt[i] * cnt[j] * cnt[k]
20+
return ans % (10**9 + 7)
21+
# @lc code=end
22+

0 commit comments

Comments
 (0)