-
-
Notifications
You must be signed in to change notification settings - Fork 305
[imgolden77] WEEK8 Solutions #2236
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,28 @@ | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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 | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| max_val = max(list(seen.values())) | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| window_len = i - start + 1 | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| if window_len - max_val <= k: | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+6
to
+17
|
||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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 | |
| max_val = max(list(seen.values())) | |
| window_len = i - start + 1 | |
| if window_len - max_val <= k: | |
| max_count = 0 | |
| for i, char in enumerate(s): | |
| seen[char] = seen.get(char, 0) + 1 | |
| max_count = max(max_count, seen[char]) | |
| # what I wrote initially: | |
| # if char not in seen: | |
| # seen[char] = 1 | |
| # else: | |
| # seen[char] += 1 | |
| window_len = i - start + 1 | |
| if window_len - max_count <= k: |
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,32 @@ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| class Solution: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+3
to
+19
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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: | |
| 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) | |
| return max_len | |
| #O(N) time complexity and O(min(m,n)) space complexity where m is the size of the charset and n is the size of the string 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: | |
| class Solution: |
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.
The membership check 'i not in h' on a list has O(n) time complexity, making the overall algorithm O(n^2) as noted in the comment. The second solution using a dictionary is more efficient. Consider removing the first solution or clearly marking it as a suboptimal approach if keeping it for educational purposes.
| 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) |
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 | ||||||||||||||
|
Comment on lines
+8
to
+11
|
||||||||||||||
| res = res + bit | |
| res = res << 1 | |
| n = n >> 1 | |
| res = res >> 1 | |
| res = (res << 1) + bit | |
| n = n >> 1 |
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,20 @@ | ||||||||||||||||||||||||||||||
| 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,57 @@ | ||||||
| # First try O(n) time complexity | ||||||
| class Solution: | ||||||
|
||||||
| class Solution: | |
| class SolutionV1: |
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 comment appears to contain commented-out code.