Skip to content

Commit

Permalink
--
Browse files Browse the repository at this point in the history
  • Loading branch information
SamirPaulb committed Apr 9, 2022
1 parent 050854c commit 6d98060
Show file tree
Hide file tree
Showing 5 changed files with 116 additions and 2 deletions.
19 changes: 19 additions & 0 deletions 12_Backtracking/03. Permutations I.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,21 @@
# https://leetcode.com/problems/permutations/

class Solution:
def permute(self, nums: List[int]) -> List[List[int]]:
nums.sort()
res = []

def dfs(arr, path):
if not arr:
res.append(path)
for i in range(len(arr)):
if i > 0 and arr[i] == arr[i-1]:
continue
dfs(arr[:i] + arr[i+1:], path + [arr[i]])

dfs(nums, [])
return res


# Time: O(N^2) # as for each element we are making (n-1) traversal again
# Space: O(N) # as we are creating n-1 subarrays
21 changes: 20 additions & 1 deletion 12_Backtracking/04. Permutations II.py
Original file line number Diff line number Diff line change
@@ -1 +1,20 @@
# https://leetcode.com/problems/permutations-ii/
# https://leetcode.com/problems/permutations-ii/

class Solution:
def permuteUnique(self, nums: List[int]) -> List[List[int]]:
nums.sort()
res = []

def dfs(arr, path):
if not arr:
res.append(path)
for i in range(len(arr)):
if i > 0 and arr[i] == arr[i-1]:
continue
dfs(arr[:i] + arr[i+1:], path + [arr[i]])

dfs(nums, [])
return res

# Time: O(N^2) # as for each element we are making (n-1) traversal again
# Space: O(N) # as we are creating n-1 subarrays
4 changes: 3 additions & 1 deletion 14_Queue/01. Sliding Window Maximum.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,9 @@ def maxSlidingWindow(self, nums, k):
res.append(nums[q[0]]) # as q[0] is largest value in that window

return res


# Time: O(N)
# Space: O(k)

'''
Deque (Doubly Ended Queue) in Python is implemented using the module “collections“.
Expand Down
42 changes: 42 additions & 0 deletions 16_String/04. Basic Calculator II.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
# https://leetcode.com/problems/basic-calculator-ii/

class Solution:
def calculate(self, s: str) -> int:
n = len(s)
sign = '+'
stack = []

i = 0
while i < n:
if s[i].isdigit():
num = s[i]
i += 1
while i < n and s[i].isdigit():
num += s[i]
i += 1
i -= 1
num = int(num)

if sign == '+':
stack.append(num)
elif sign == '-':
stack.append(-num)
elif sign == '*':
newNum = stack.pop() * num
stack.append(newNum)
elif sign == '/':
newNum = math.trunc(stack.pop() / num)
# or can use newNum = int(stack.pop() / num)
# for negative numbers // will not work
stack.append(newNum)

elif s[i] != ' ':
sign = s[i]
i += 1

return sum(stack)


# Time: O(N)
# Space: O(N)

32 changes: 32 additions & 0 deletions 16_String/05. Basic Calculator I.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# https://leetcode.com/problems/basic-calculator/

class Solution:
def calculate(self, s: str) -> int:
res, num, sign, stack = 0, 0, 1, []

for ch in s:
if ch.isdigit():
num = num*10 + ord(ch) - ord('0')

elif ch in ['+', '-']:
res += sign * num
num = 0
sign = 1 if ch == '+' else -1

elif ch == '(':
stack.append(res)
stack.append(sign)
res = 0
sign = 1

elif ch == ')':
res += sign * num
res *= stack.pop()
res += stack.pop()
num = 0

return res + sign * num

# Time: O(N)
# Space: O(N)

0 comments on commit 6d98060

Please sign in to comment.