-
-
Notifications
You must be signed in to change notification settings - Fork 305
[imgolden77] WEEK 8 Solutions #2237
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
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,33 @@ | ||
| """ | ||
| # Definition for a Node. | ||
| class Node: | ||
| def __init__(self, val = 0, neighbors = None): | ||
| self.val = val | ||
| self.neighbors = neighbors if neighbors is not None else [] | ||
| """ | ||
|
|
||
| from typing import Optional | ||
| class Solution: | ||
| def cloneGraph(self, node: Optional['Node']) -> Optional['Node']: | ||
| if not node: | ||
| return None | ||
| old_to_new = {} | ||
|
|
||
| def dfs(curr_node): | ||
| if curr_node in old_to_new: | ||
| return old_to_new[curr_node] | ||
|
|
||
| copy = Node(curr_node.val) | ||
| old_to_new[curr_node] = copy | ||
|
|
||
| for neighbors in curr_node.neighbors: | ||
| copy.neighbors.append(dfs(neighbors)) | ||
|
|
||
| return copy | ||
|
|
||
| return dfs(node) | ||
|
|
||
| # O(N+E) time complexity where N is the number of nodes and E is the number of edges in the graph. | ||
| # O(N) space complexity for the hashmap and the recursion stack in the worst case. | ||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change | ||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,27 @@ | ||||||||||||||||
| class Solution: | ||||||||||||||||
| def characterReplacement(self, s: str, k: int) -> int: | ||||||||||||||||
| start = 0 | ||||||||||||||||
| max_len = 0 | ||||||||||||||||
| seen = {} | ||||||||||||||||
|
|
||||||||||||||||
| for i, char in enumerate(s): | ||||||||||||||||
|
|
||||||||||||||||
| seen[char] = seen.get(char, 0) + 1 | ||||||||||||||||
| # what I wrote initially: | ||||||||||||||||
| # if char not in seen: | ||||||||||||||||
| # seen[char] = 1 | ||||||||||||||||
| # else: | ||||||||||||||||
| # seen[char] += 1 | ||||||||||||||||
|
Comment on lines
+9
to
+14
|
||||||||||||||||
| seen[char] = seen.get(char, 0) + 1 | |
| # what I wrote initially: | |
| # if char not in seen: | |
| # seen[char] = 1 | |
| # else: | |
| # seen[char] += 1 | |
| seen[char] = seen.get(char, 0) + 1 # update frequency of current character |
Copilot
AI
Jan 1, 2026
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.
Creating a list from dictionary values with 'list(seen.values())' and then calling 'max()' on each iteration is inefficient. Consider tracking the maximum frequency as you update the dictionary, storing it in a variable like 'max_freq' and updating it when you increment character counts.
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,33 @@ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| class Solution: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| def lengthOfLongestSubstring(self, s: str) -> int: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| h = [] | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| max_s = 0 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| for i in s: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if i not in h: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| h.append(i) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| else: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| h=h[h.index(i)+1:] | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| h.append(i) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| #I reset h=[] but it had problemswith the cases like "dvdf" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| #So I modified it to h=h[h.index(i)+1:] | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| max_s = max(max_s, len(h)) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return max_s | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| #O(N^2) time complexity due to "if i not in h" and O(min(m,n)) space complexity where m is the size of the charset and n is the size of the string s. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+3
to
+17
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| h = [] | |
| max_s = 0 | |
| for i in s: | |
| if i not in h: | |
| h.append(i) | |
| else: | |
| h=h[h.index(i)+1:] | |
| h.append(i) | |
| #I reset h=[] but it had problemswith the cases like "dvdf" | |
| #So I modified it to h=h[h.index(i)+1:] | |
| max_s = max(max_s, len(h)) | |
| return max_s | |
| #O(N^2) time complexity due to "if i not in h" and O(min(m,n)) space complexity where m is the size of the charset and n is the size of the string s. | |
| char_window = [] | |
| max_s = 0 | |
| for i in s: | |
| if i not in char_window: | |
| char_window.append(i) | |
| else: | |
| char_window = char_window[char_window.index(i) + 1:] | |
| char_window.append(i) | |
| #I reset char_window=[] but it had problemswith the cases like "dvdf" | |
| #So I modified it to char_window=char_window[char_window.index(i)+1:] | |
| max_s = max(max_s, len(char_window)) | |
| return max_s | |
| #O(N^2) time complexity due to "if i not in char_window" and O(min(m,n)) space complexity where m is the size of the charset and n is the size of the string s. |
Copilot
AI
Jan 1, 2026
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.
This assignment to 'Solution' is unnecessary as it is redefined before this value is used.
| h = [] | |
| max_s = 0 | |
| for i in s: | |
| if i not in h: | |
| h.append(i) | |
| else: | |
| h=h[h.index(i)+1:] | |
| h.append(i) | |
| #I reset h=[] but it had problemswith the cases like "dvdf" | |
| #So I modified it to h=h[h.index(i)+1:] | |
| max_s = max(max_s, len(h)) | |
| return max_s | |
| #O(N^2) time complexity due to "if i not in h" and O(min(m,n)) space complexity where m is the size of the charset and n is the size of the string s. | |
| class Solution: | |
| def lengthOfLongestSubstring(self, s: str) -> int: | |
| seen = {} | |
| start = 0 | |
| max_len = 0 | |
| for i, char in enumerate(s): | |
| if char in seen and seen[char]>=start: | |
| start= seen[char]+1 | |
| seen[char]=i | |
| max_len = max(max_len, i - start +1) | |
| seen = {} | |
| start = 0 | |
| max_len = 0 | |
| for i, char in enumerate(s): | |
| if char in seen and seen[char] >= start: | |
| start = seen[char] + 1 | |
| seen[char] = i | |
| max_len = max(max_len, i - start + 1) |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,25 @@ | ||
| class Solution: | ||
| def countSubstrings(self, s: str) -> int: | ||
| total_count = 0 | ||
|
|
||
| def expand(left, right): | ||
| local_count = 0 | ||
| while left>=0 and right< len(s) and s[left] == s[right]: | ||
| # while s[left] == s[right] and left>=0 and right< len(s) : | ||
| # The above line would cause index error if left or right go out of bounds | ||
| local_count += 1 | ||
| left -= 1 | ||
| right += 1 | ||
| return local_count | ||
|
|
||
| for i in range(len(s)): | ||
| total_count += expand(i, i) | ||
| total_count += expand(i, i+1) | ||
|
|
||
| return total_count | ||
|
|
||
| #Complexity Analysis | ||
| #Time complexity: O(n^2) where n is the length of the input string s. | ||
| #Space complexity: O(1) as we are using only a constant amount of extra space | ||
| # Manacher's Algorithm can achieve O(n) time complexity for this problem. | ||
|
|
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| class Solution: | ||
| def reverseBits(self, n: int) -> int: | ||
| if n == 0: | ||
| return 0 | ||
| res = 0 | ||
| for i in range(32): | ||
| bit = n & 1 | ||
| res = res + bit | ||
| res = res << 1 | ||
| n = n >> 1 | ||
| res = res >> 1 | ||
|
|
||
| return res | ||
|
|
||
| # Complexity O(1) - Time: 32 iterations, Space: O(1) no extra space used | ||
|
|
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,21 @@ | ||||||||||||||||||||||||||||||
| class Solution: | ||||||||||||||||||||||||||||||
| def reverseList(self, head: Optional[ListNode]) -> Optional[ListNode]: | ||||||||||||||||||||||||||||||
| curr = head | ||||||||||||||||||||||||||||||
| prev = None | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| while curr is not None: # once was "while curr.next is not None:" but that showed AttributeError: 'NoneType' object has no attribute 'next' | ||||||||||||||||||||||||||||||
| if prev: | ||||||||||||||||||||||||||||||
| post = curr.next | ||||||||||||||||||||||||||||||
| curr.next = prev | ||||||||||||||||||||||||||||||
| prev = curr | ||||||||||||||||||||||||||||||
| curr = post | ||||||||||||||||||||||||||||||
| else: | ||||||||||||||||||||||||||||||
| post = curr.next | ||||||||||||||||||||||||||||||
| prev = curr | ||||||||||||||||||||||||||||||
| curr.next = None | ||||||||||||||||||||||||||||||
| curr = post | ||||||||||||||||||||||||||||||
|
Comment on lines
+7
to
+16
|
||||||||||||||||||||||||||||||
| if prev: | |
| post = curr.next | |
| curr.next = prev | |
| prev = curr | |
| curr = post | |
| else: | |
| post = curr.next | |
| prev = curr | |
| curr.next = None | |
| curr = post | |
| post = curr.next | |
| curr.next = prev | |
| prev = curr | |
| curr = post |
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,59 @@ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| # First try O(n) time complexity | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| class Solution: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| def isValid(self, s: str) -> bool: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| stack = [] | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| former= ['(', '{', '['] | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| for i in s: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if i in former: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+5
to
+8
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| former= ['(', '{', '['] | |
| for i in s: | |
| if i in former: | |
| opening_brackets = ['(', '{', '['] | |
| for i in s: | |
| if i in opening_brackets: |
Copilot
AI
Jan 1, 2026
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.
Inconsistent spacing around the equality operator. Should be 'stack == []' to match Python style conventions (PEP 8).
Copilot
AI
Jan 1, 2026
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.
Inconsistent spacing around the equality operator. Should be 'stack[-1] == ' to match Python style conventions (PEP 8).
Copilot
AI
Jan 1, 2026
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.
Inconsistent spacing around the equality operator. Should be 'stack == []' to match Python style conventions (PEP 8).
Copilot
AI
Jan 1, 2026
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.
Inconsistent spacing around the equality operator. Should be 'stack[-1] == ' to match Python style conventions (PEP 8).
Copilot
AI
Jan 1, 2026
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.
Inconsistent spacing around the equality operator. Should be 'stack[-1] == ' to match Python style conventions (PEP 8).
Copilot
AI
Jan 1, 2026
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.
This assignment to 'Solution' is unnecessary as it is redefined before this value is used.
| class Solution: | |
| def isValid(self, s: str) -> bool: | |
| stack = [] | |
| former= ['(', '{', '['] | |
| for i in s: | |
| if i in former: | |
| stack.append(i) | |
| elif i == ')': | |
| if stack== []: | |
| return False | |
| elif stack[-1]== '(': | |
| stack.pop() | |
| else: | |
| return False | |
| elif i == '}': | |
| if stack== []: | |
| return False | |
| elif stack[-1]== '{': | |
| stack.pop() | |
| else: | |
| return False | |
| elif i == ']': | |
| if stack == []: | |
| return False | |
| elif stack[-1]== '[': | |
| stack.pop() | |
| else: | |
| return False | |
| if stack == []: | |
| return True | |
| else: | |
| return False | |
| # (obsolete implementation removed; see the Solution class below) |
Copilot
AI
Jan 1, 2026
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.
Inconsistent spacing around the inequality operator. Should be 'stack[-1] != par_map[i]' to match Python style conventions (PEP 8).
| if not stack or stack[-1] !=par_map[i]: | |
| if not stack or stack[-1] != par_map[i]: |
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.
The variable name 'neighbors' in the loop is misleading because it represents a single neighbor node, not multiple neighbors. It should be singular, like 'neighbor', to accurately reflect what it represents.