-
-
Notifications
You must be signed in to change notification settings - Fork 248
[Sam] Week 2 solutions #49
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
Changes from all commits
8da6d78
7c3ef34
54a518a
c687c5b
db93667
24767f3
d64fd2e
a269aff
955d3d2
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 |
---|---|---|
@@ -0,0 +1,17 @@ | ||
# Time complexity : O(n) | ||
# Space complexity : O(n) | ||
class Solution: | ||
def invertTree(self, root: Optional[TreeNode]) -> Optional[TreeNode]: | ||
stack = [root] | ||
|
||
while stack: | ||
node = stack.pop() | ||
|
||
# Not accessing child if node is Null | ||
if not node: | ||
continue | ||
|
||
node.left, node.right = node.right, node.left | ||
stack += [node.left, node.right] | ||
|
||
return root |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
# Time complexity : O(n) | ||
# Space complexity : O(1) | ||
class Solution: | ||
def hasCycle(self, head: Optional[ListNode]) -> bool: | ||
slow = head | ||
fast = head | ||
|
||
# check fast and fast.next are not the last node to ensure we can access fast.next.next | ||
while fast and fast.next: | ||
slow = slow.next | ||
fast = fast.next.next | ||
if slow == fast: | ||
return True | ||
|
||
return False |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
# Time complexity : O(m*n) | ||
# Space complexity : O(1) | ||
class Solution: | ||
def mergeTwoLists( | ||
self, list1: Optional[ListNode], list2: Optional[ListNode] | ||
) -> Optional[ListNode]: | ||
# Create a dummy node to simplify the logic | ||
dummy = ListNode(None) | ||
# Pointer to the current node in the merged list | ||
node = dummy | ||
|
||
# Loop until both lists have nodes | ||
while list1 and list2: | ||
# Choose the smaller value between list1 and list2 | ||
if list1.val < list2.val: | ||
# Append list1 node to the merged list | ||
node.next = list1 | ||
# Move to the next node in list1 | ||
list1 = list1.next | ||
else: | ||
# Append list2 node to the merged list | ||
node.next = list2 | ||
# Move to the next node in list2 | ||
list2 = list2.next | ||
# Move to the next node in the merged list | ||
node = node.next | ||
|
||
# Append the remaining nodes from list1 or list2 | ||
node.next = list1 or list2 | ||
|
||
# Return the merged list (skip the dummy node) | ||
return dummy.next |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
# Time complexity : O(n) | ||
# Space complexity : O(n) | ||
class Solution: | ||
def reverseList(self, head: Optional[ListNode]) -> Optional[ListNode]: | ||
# Return None when head is None | ||
if not head: | ||
return None | ||
|
||
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. Sam ๋์ ์ฌ๊ท๋ณด๋ค๋ ์คํ์ฐ์๋ ๊ฒ์ ์ ํธํ์๋ ๋ฏ? 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. ๋น์์ ์ฌ๊ท ๋ณด๋ค๋ ์คํ์ด ๋ ์ดํด๊ฐ ํธํ๋๋ฐ ์ด์ ์กฐ๊ธ์ฉ ์ฌ๊ท์ ์ฌ๊ณ ์ ์ต์ํด์ง๋ ๊ฒ ๊ฐ์ต๋๋ค 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. ์ฐธ๊ณ ๋ก ์ ๋ ๊ฐ์ธ ์ ํธ๋ฅผ ์กด์คํด์ ใ ใ ๋ฌผ๋ก ๋ ๋ค ์์ ์์ฌ๋ก ์ธ ์ ์๋ค๋ฉด ๋ ์ข๊ฒ ์ง๋ง์ ๐ธ |
||
node = head | ||
|
||
while node: | ||
stack.append(node) | ||
node = node.next | ||
|
||
head = stack.pop() | ||
node = head | ||
|
||
# End the loop when stack is empty | ||
while stack: | ||
node.next = stack.pop() | ||
node = node.next | ||
|
||
# Set the last node's next to None to indicate the end of the reversed list | ||
node.next = None | ||
|
||
return head |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
# Time complexity : O(n) | ||
# Space complexity : O(n) | ||
class Solution: | ||
def isValid(self, s: str) -> bool: | ||
parentheses = {"[": "]", "{": "}", "(": ")"} | ||
stack = [] | ||
|
||
# if ch is opening bracket | ||
for ch in s: | ||
if ch in parentheses: | ||
stack.append(ch) | ||
# if ch is closing bracket | ||
else: | ||
# if closing bracket comes without opening bracket previously | ||
if not stack: | ||
return False | ||
# if closing bracket doesnt match with the latest type of opening bracket | ||
if ch != parentheses[stack.pop()]: | ||
return False | ||
|
||
# True if stack is empty after going through the process above | ||
return not stack |
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.
ํ์ด์ฌ์ ์ด๊ฒ ํ ์ค๋ก ๋์ ๋ ํธํด์ ใ ใ