forked from SamirPaulb/DSAlgo
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
050854c
commit 6d98060
Showing
5 changed files
with
116 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
|