From d5cb4365cf42dc097356286a93ad5d76b0ea025f Mon Sep 17 00:00:00 2001 From: Siya Pandey <85756788+siyapandeyvsp@users.noreply.github.com> Date: Fri, 21 Oct 2022 19:07:32 +0530 Subject: [PATCH 1/3] leetcode C# problem --- create two-sum.cs | 12 ++++++++++++ 1 file changed, 12 insertions(+) create mode 100644 create two-sum.cs diff --git a/create two-sum.cs b/create two-sum.cs new file mode 100644 index 0000000..2b95fda --- /dev/null +++ b/create two-sum.cs @@ -0,0 +1,12 @@ +public class Solution { + public int[] TwoSum(int[] nums, int target) { + Dictionary lookup = new Dictionary(); + 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[] { }; + } +} From efa9b2f7368b395f519ca8e4e2f721152db5ac9a Mon Sep 17 00:00:00 2001 From: Siya Pandey <85756788+siyapandeyvsp@users.noreply.github.com> Date: Fri, 21 Oct 2022 19:10:29 +0530 Subject: [PATCH 2/3] Create sum-with-multiplicity --- sum-with-multiplicity | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) create mode 100644 sum-with-multiplicity diff --git a/sum-with-multiplicity b/sum-with-multiplicity new file mode 100644 index 0000000..d41813d --- /dev/null +++ b/sum-with-multiplicity @@ -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) From 81090a36dd37a0eed6ec6fd37e07672cb33b1238 Mon Sep 17 00:00:00 2001 From: Siya Pandey <85756788+siyapandeyvsp@users.noreply.github.com> Date: Fri, 21 Oct 2022 19:11:05 +0530 Subject: [PATCH 3/3] Create sum-with-multiplicity --- sum-with-multiplicity | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) create mode 100644 sum-with-multiplicity diff --git a/sum-with-multiplicity b/sum-with-multiplicity new file mode 100644 index 0000000..d41813d --- /dev/null +++ b/sum-with-multiplicity @@ -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)