-
Notifications
You must be signed in to change notification settings - Fork 0
Sourcery refactored main branch #1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,20 +1,20 @@ | ||
# BOTTOM-UP Dynamic Programming | ||
class Solution: | ||
def isMatch(self, s: str, p: str) -> bool: | ||
cache = [[False] * (len(p) + 1) for i in range(len(s) + 1)] | ||
cache = [[False] * (len(p) + 1) for _ in range(len(s) + 1)] | ||
cache[len(s)][len(p)] = True | ||
|
||
for i in range(len(s), -1, -1): | ||
for j in range(len(p) - 1, -1 ,-1): | ||
match = i < len(s) and (s[i] == p[j] or p[j] == ".") | ||
|
||
if (j + 1) < len(p) and p[j + 1] == "*": | ||
cache[i][j] = cache[i][j + 2] | ||
if match: | ||
cache[i][j] = cache[i + 1][j] or cache[i][j] | ||
elif match: | ||
cache[i][j] = cache[i+1][j+1] | ||
|
||
Comment on lines
-4
to
+17
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
return cache[0][0] | ||
|
||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,7 +7,12 @@ | |
|
||
class Solution: | ||
def isSameTree(self, p: TreeNode, q: TreeNode) -> bool: | ||
if not p and not q: return True | ||
if p and q and p.val == q.val: | ||
return self.isSameTree(p.left, q.left) and self.isSameTree(p.right, q.right) | ||
else: return False | ||
if p or q: | ||
return ( | ||
self.isSameTree(p.left, q.left) | ||
and self.isSameTree(p.right, q.right) | ||
if p and q and p.val == q.val | ||
else False | ||
) | ||
|
||
else: return True | ||
Comment on lines
-10
to
+18
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,11 +10,11 @@ def levelOrder(self, root: TreeNode) -> List[List[int]]: | |
res = [] | ||
q = collections.deque() | ||
if root: q.append(root) | ||
|
||
while q: | ||
val = [] | ||
for i in range(len(q)): | ||
|
||
for _ in range(len(q)): | ||
Comment on lines
-13
to
+17
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
node = q.popleft() | ||
val.append(node.val) | ||
if node.left: q.append(node.left) | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,20 +1,21 @@ | ||
# RECURSIVE DFS | ||
class Solution: | ||
def maxDepth(self, root: TreeNode) -> int: | ||
if not root: | ||
return 0 | ||
|
||
return 1 + max(self.maxDepth(root.left), self.maxDepth(root.right)) | ||
return ( | ||
1 + max(self.maxDepth(root.left), self.maxDepth(root.right)) | ||
if root | ||
else 0 | ||
) | ||
Comment on lines
-4
to
+8
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
|
||
# ITERATIVE DFS | ||
class Solution: | ||
def maxDepth(self, root: TreeNode) -> int: | ||
stack = [[root, 1]] | ||
res = 0 | ||
|
||
while stack: | ||
node, depth = stack.pop() | ||
|
||
Comment on lines
-14
to
+18
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Found the following improvement in Function |
||
if node: | ||
res = max(res, depth) | ||
stack.append([node.left, depth + 1]) | ||
|
@@ -26,12 +27,12 @@ class Solution: | |
def maxDepth(self, root: TreeNode) -> int: | ||
if not root: | ||
return 0 | ||
|
||
level = 0 | ||
q = deque([root]) | ||
while q: | ||
|
||
for i in range(len(q)): | ||
for _ in range(len(q)): | ||
Comment on lines
-29
to
+35
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
node = q.popleft() | ||
if node.left: | ||
q.append(node.left) | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,11 +2,11 @@ class Solution: | |
def maxArea(self, height: List[int]) -> int: | ||
l, r = 0, len(height) - 1 | ||
res = 0 | ||
|
||
while l < r: | ||
res = max(res, min(height[l], height[r]) * (r - l)) | ||
if height[l] < height[r]: | ||
l += 1 | ||
elif height[r] <= height[l]: | ||
else: | ||
Comment on lines
-5
to
+10
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
r -= 1 | ||
return res |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,12 @@ | ||
class Solution: | ||
def longestCommonSubsequence(self, text1: str, text2: str) -> int: | ||
dp = [[0 for j in range(len(text2) + 1)] for i in range(len(text1) + 1)] | ||
dp = [[0 for _ in range(len(text2) + 1)] for _ in range(len(text1) + 1)] | ||
|
||
for i in range(len(text1) - 1, -1, -1): | ||
for j in range(len(text2) - 1, -1, -1): | ||
if text1[i] == text2[j]: | ||
dp[i][j] = 1 + dp[i + 1][j + 1] | ||
else: | ||
dp[i][j] = max(dp[i][j + 1], dp[i + 1][j]) | ||
|
||
Comment on lines
-3
to
+11
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
return dp[0][0] |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,12 @@ | ||
class Solution: | ||
def numDistinct(self, s: str, t: str) -> int: | ||
cache = {} | ||
|
||
for i in range(len(s) + 1): | ||
cache[(i, len(t))] = 1 | ||
for j in range(len(t)): | ||
cache[(len(s), j)] = 0 | ||
|
||
Comment on lines
-4
to
+9
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Found the following improvement in Function |
||
for i in range(len(s) - 1, -1, -1): | ||
for j in range(len(t) - 1, -1, -1): | ||
if s[i] == t[j]: | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,7 @@ | ||
class Solution: | ||
def maxProfit(self, prices: List[int]) -> int: | ||
res = 0 | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Found the following improvement in Function |
||
l = 0 | ||
for r in range(1, len(prices)): | ||
if prices[r] < prices[l]: | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,24 +2,24 @@ class Solution: | |
def ladderLength(self, beginWord: str, endWord: str, wordList: List[str]) -> int: | ||
if endWord not in wordList: | ||
return 0 | ||
|
||
nei = collections.defaultdict(list) | ||
wordList.append(beginWord) | ||
for word in wordList: | ||
for j in range(len(word)): | ||
pattern = word[:j] + "*" + word[j + 1:] | ||
pattern = f"{word[:j]}*{word[j + 1:]}" | ||
nei[pattern].append(word) | ||
visit = set([beginWord]) | ||
|
||
visit = {beginWord} | ||
q = deque([beginWord]) | ||
res = 1 | ||
while q: | ||
for i in range(len(q)): | ||
for _ in range(len(q)): | ||
word = q.popleft() | ||
if word == endWord: | ||
return res | ||
for j in range(len(word)): | ||
pattern = word[:j] + "*" + word[j + 1:] | ||
pattern = f"{word[:j]}*{word[j + 1:]}" | ||
Comment on lines
-5
to
+22
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
for neiWord in nei[pattern]: | ||
if neiWord not in visit: | ||
visit.add(neiWord) | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,7 +2,7 @@ class Solution: | |
def longestConsecutive(self, nums: List[int]) -> int: | ||
numSet = set(nums) | ||
longest = 0 | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Found the following improvement in Function |
||
for n in nums: | ||
# check if its the start of a sequence | ||
if (n - 1) not in numSet: | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,7 +10,7 @@ def __init__(self, x: int, next: 'Node' = None, random: 'Node' = None): | |
class Solution: | ||
def copyRandomList(self, head: 'Node') -> 'Node': | ||
oldToCopy = { None : None } | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Found the following improvement in Function |
||
cur = head | ||
while cur: | ||
copy = Node(cur.val) | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,7 +7,7 @@ | |
class Solution: | ||
def hasCycle(self, head: ListNode) -> bool: | ||
slow, fast = head, head | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Found the following improvement in Function |
||
while fast and fast.next: | ||
slow = slow.next | ||
fast = fast.next.next | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,7 +8,7 @@ class LRUCache: | |
def __init__(self, capacity: int): | ||
self.cap = capacity | ||
self.cache = {} # map key to node | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Found the following improvement in Function |
||
self.left, self.right = Node(0, 0), Node(0, 0) | ||
self.left.next, self.right.prev = self.right, self.left | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,11 +2,11 @@ class Solution: | |
def threeSum(self, nums: List[int]) -> List[List[int]]: | ||
res = [] | ||
nums.sort() | ||
|
||
for i, a in enumerate(nums): | ||
if i > 0 and a == nums[i - 1]: | ||
continue | ||
|
||
Comment on lines
-5
to
+9
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Found the following improvement in Function |
||
l, r = i + 1, len(nums) - 1 | ||
while l < r: | ||
threeSum = a + nums[l] + nums[r] | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,13 +2,13 @@ class Solution: | |
def evalRPN(self, tokens: List[str]) -> int: | ||
stack = [] | ||
for c in tokens: | ||
if c == "+": | ||
if c == "*": | ||
stack.append(stack.pop() * stack.pop()) | ||
elif c == "+": | ||
stack.append(stack.pop() + stack.pop()) | ||
elif c == "-": | ||
a, b = stack.pop(), stack.pop() | ||
stack.append(b - a) | ||
elif c == "*": | ||
stack.append(stack.pop() * stack.pop()) | ||
Comment on lines
-5
to
-11
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
elif c == "/": | ||
a, b = stack.pop(), stack.pop() | ||
stack.append(int(b / a)) | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,9 +3,9 @@ def maxProduct(self, nums: List[int]) -> int: | |
# O(n)/O(1) : Time/Memory | ||
res = max(nums) | ||
curMin, curMax = 1, 1 | ||
|
||
for n in nums: | ||
|
||
Comment on lines
-6
to
+8
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Found the following improvement in Function |
||
tmp = curMax * n | ||
curMax = max(n * curMax, n * curMin, n) | ||
curMin = min(tmp, n * curMin, n) | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,7 +9,7 @@ def minCostConnectPoints(self, points: List[List[int]]) -> int: | |
dist = abs(x1 - x2) + abs(y1 - y2) | ||
adj[i].append([dist, j]) | ||
adj[j].append([dist, i]) | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Found the following improvement in Function |
||
# Prim's | ||
res = 0 | ||
visit = set() | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,7 +9,7 @@ def minInterval(self, intervals: List[List[int]], queries: List[int]) -> List[in | |
l, r = intervals[i] | ||
heapq.heappush(minHeap, (r - l + 1, r)) | ||
i += 1 | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Found the following improvement in Function |
||
while minHeap and minHeap[0][1] < q: | ||
heapq.heappop(minHeap) | ||
res[q] = minHeap[0][0] if minHeap else -1 | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,17 +8,16 @@ class Solution: | |
def rightSideView(self, root: TreeNode) -> List[int]: | ||
res = [] | ||
q = collections.deque([root]) | ||
|
||
while q: | ||
rightSide = None | ||
qLen = len(q) | ||
|
||
for i in range(qLen): | ||
node = q.popleft() | ||
if node: | ||
|
||
for _ in range(qLen): | ||
if node := q.popleft(): | ||
rightSide = node | ||
q.append(node.left) | ||
q.append(node.right) | ||
if rightSide: | ||
res.append(rightSide.val) | ||
res.append(rightSide.val) | ||
Comment on lines
-11
to
+22
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
return res |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,18 +7,18 @@ class Solution: | |
def addTwoNumbers(self, l1: ListNode, l2: ListNode) -> ListNode: | ||
dummy = ListNode() | ||
cur = dummy | ||
|
||
carry = 0 | ||
while l1 or l2 or carry: | ||
v1 = l1.val if l1 else 0 | ||
v2 = l2.val if l2 else 0 | ||
|
||
# new digit | ||
val = v1 + v2 + carry | ||
carry = val // 10 | ||
val = val % 10 | ||
cur.next = ListNode(val) | ||
|
||
Comment on lines
-10
to
+21
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Found the following improvement in Function |
||
# update ptrs | ||
cur = cur.next | ||
l1 = l1.next if l1 else None | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,13 +2,13 @@ class Solution: | |
def isValid(self, s: str) -> bool: | ||
Map = { ")":"(", "]":"[", "}":"{" } | ||
stack = [] | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Found the following improvement in Function |
||
for c in s: | ||
if c not in Map: | ||
stack.append(c) | ||
continue | ||
if not stack or stack[-1] != Map[c]: | ||
return False | ||
stack.pop() | ||
|
||
return not stack |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,13 @@ | ||
class Solution: | ||
def isHappy(self, n: int) -> bool: | ||
slow, fast = n, self.sumSquareDigits(n) | ||
|
||
while slow != fast: | ||
fast = self.sumSquareDigits(fast) | ||
fast = self.sumSquareDigits(fast) | ||
slow = self.sumSquareDigits(slow) | ||
|
||
return True if fast == 1 else False | ||
return fast == 1 | ||
Comment on lines
-4
to
+10
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
|
||
def sumSquareDigits(self, n): | ||
output = 0 | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -19,7 +19,7 @@ def insert(self, word: str) -> None: | |
curr = self.root | ||
for c in word: | ||
i = ord(c)-ord("a") | ||
if curr.children[i] == None: | ||
if curr.children[i] is None: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
curr.children[i] = TrieNode() | ||
curr = curr.children[i] | ||
curr.end = True | ||
|
@@ -31,7 +31,7 @@ def search(self, word: str) -> bool: | |
curr = self.root | ||
for c in word: | ||
i = ord(c)-ord("a") | ||
if curr.children[i] == None: | ||
if curr.children[i] is None: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
return False | ||
curr = curr.children[i] | ||
return curr.end | ||
|
@@ -43,7 +43,7 @@ def startsWith(self, prefix: str) -> bool: | |
curr = self.root | ||
for c in prefix: | ||
i = ord(c)-ord("a") | ||
if curr.children[i] == None: | ||
if curr.children[i] is None: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
return False | ||
curr = curr.children[i] | ||
return True |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Found the following improvement in Function
Solution.twoSum
: