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
12 changes: 12 additions & 0 deletions create two-sum.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
public class Solution {
public int[] TwoSum(int[] nums, int target) {
Dictionary<int, int> lookup = new Dictionary<int, int>();
for (var i = 0; i < nums.Length; i++) {
if (lookup.ContainsKey(target - nums[i])) {
return new int [] { lookup[target - nums[i]], i };
}
lookup[nums[i]] = i;
}
return new int[] { };
}
}
25 changes: 25 additions & 0 deletions sum-with-multiplicity
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# Time: O(n^2), n is the number of disctinct A[i]
# Space: O(n)

import collections
import itertools


class Solution(object):
def threeSumMulti(self, A, target):
"""
:type A: List[int]
:type target: int
:rtype: int
"""
count = collections.Counter(A)
result = 0
for i, j in itertools.combinations_with_replacement(count, 2):
k = target - i - j
if i == j == k:
result += count[i] * (count[i]-1) * (count[i]-2) // 6
elif i == j != k:
result += count[i] * (count[i]-1) // 2 * count[k]
elif max(i, j) < k:
result += count[i] * count[j] * count[k]
return result % (10**9 + 7)