From 2fec57fdf0cc5b46727828280dd260a68734039f Mon Sep 17 00:00:00 2001 From: Atharva Kharage Date: Sun, 6 Oct 2024 12:49:59 -0700 Subject: [PATCH] Competitive coding 3 complete --- Problem1.py | 35 +++++++++++++++++++++++++++++++++++ Problem2.py | 30 ++++++++++++++++++++++++++++++ 2 files changed, 65 insertions(+) create mode 100644 Problem1.py create mode 100644 Problem2.py diff --git a/Problem1.py b/Problem1.py new file mode 100644 index 00000000..f85ce0b7 --- /dev/null +++ b/Problem1.py @@ -0,0 +1,35 @@ +# 532. K-diff Pairs in an Array +class Solution: + def findPairs(self, nums: List[int], k: int) -> int: + Map = {} + count = 0 + + for n in nums: + if n not in Map: + Map[n] = 1 + else: + Map[n] += 1 + print(Map) + + if k == 0: + for n in nums: + if n in Map: + if Map[n] > 1: + count += 1 + Map[n] = 0 + return count + + + for n in nums: + value = n-k + if value in Map: + count += 1 + Map.pop(value) + else: + count += 0 + + return count + + + + \ No newline at end of file diff --git a/Problem2.py b/Problem2.py new file mode 100644 index 00000000..5f7023fb --- /dev/null +++ b/Problem2.py @@ -0,0 +1,30 @@ +# 118. Pascal's Triangle +class Solution: + def generate(self, numRows: int) -> List[List[int]]: + output = [] + def getRow(r,c): + result = [1] + ans = 1 + + for i in range(1,r+1): + ans = ans * ((r+1)-i) // i + result.append(ans) + return result + + for i in range(numRows): + output.append(getRow(i,i+1)) + + return output + + + + + + + + + + + + + \ No newline at end of file