-
-
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
Conversation
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.
Pull request overview
This PR submits solutions for Week 8 algorithm problems, demonstrating multiple approaches to common coding challenges with complexity analysis comments.
- Solutions for 7 LeetCode problems with detailed complexity analysis
- Multiple solution approaches showing iterative improvement (first attempt vs. optimized)
- Comprehensive inline comments explaining algorithmic choices and edge cases
Reviewed changes
Copilot reviewed 7 out of 8 changed files in this pull request and generated 8 comments.
Show a summary per file
| File | Description |
|---|---|
| valid-parentheses/imgolden77.py | Two implementations of parentheses validation using stack - shows progression from verbose to pythonic code with dictionary mapping |
| reverse-linked-list/imgolden77.py | Iterative linked list reversal with pointer manipulation |
| reverse-bits/imgolden77.py | Bit manipulation solution to reverse 32-bit integer |
| palindromic-substrings/imgolden77.py | Expand-around-center approach to count palindromic substrings |
| longest-substring-without-repeating-characters/imgolden77.py | Two approaches: list-based O(n²) and optimized dictionary-based O(n) sliding window |
| longest-repeating-character-replacement/imgolden77.py | Sliding window solution with character frequency tracking |
| clone-graph/imgolden77.py | DFS-based graph cloning with hashmap to track old-to-new node mappings |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| 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) | ||
|
|
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) |
| 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: |
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 slice operation 'h[h.index(i)+1:]' combined with h.index(i) results in O(n) time complexity for each iteration, making the overall algorithm O(n^2) despite using a more sophisticated approach than simple list membership. The second solution with the dictionary avoids this issue entirely.
| 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: |
| if prev: | ||
| post = curr.next | ||
| curr.next = prev | ||
| prev = curr | ||
| curr = post | ||
| else: | ||
| post = curr.next | ||
| prev = curr | ||
| curr.next = None | ||
| curr = post |
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 if-else block contains duplicated logic. The operations inside both branches are nearly identical (post = curr.next, prev = curr, curr = post), with the only difference being curr.next assignment. This duplication can be eliminated by removing the conditional check for prev and always performing the same operations, since setting curr.next = prev works correctly even when prev is None (which is the desired behavior for the first node).
| 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 |
| res = res + bit | ||
| res = res << 1 | ||
| n = n >> 1 | ||
| res = res >> 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.
There's a bug in the bit reversal logic. The code shifts left 32 times (once per iteration) and then shifts right once at the end, resulting in one extra left shift. This means the result is shifted left by 1 position more than it should be. The loop should either not shift on the last iteration, or the final right shift should be removed and the shift should happen after adding the bit rather than before.
| res = res + bit | |
| res = res << 1 | |
| n = n >> 1 | |
| res = res >> 1 | |
| res = (res << 1) + bit | |
| n = n >> 1 |
|
|
||
| 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: |
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 new list from dictionary values on every iteration is inefficient and makes this O(N*26) = O(N) in practice, but could be optimized. Consider maintaining a max_count variable that tracks the maximum frequency seen so far, which only needs to be updated when a character count increases. This avoids the repeated list conversion and max operation.
| 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: |
| seen[char] = seen.get(char, 0) + 1 | ||
| # what I wrote initially: | ||
| # if char not in seen: | ||
| # seen[char] = 1 | ||
| # else: | ||
| # seen[char] += 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 comment appears to contain commented-out code.
| seen[char] = seen.get(char, 0) + 1 | |
| # what I wrote initially: | |
| # if char not in seen: | |
| # seen[char] = 1 | |
| # else: | |
| # seen[char] += 1 | |
| # increment the count for the current character in the window | |
| seen[char] = seen.get(char, 0) + 1 |
| @@ -0,0 +1,57 @@ | |||
| # First try O(n) time complexity | |||
| 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.
This assignment to 'Solution' is unnecessary as it is redefined before this value is used.
| class Solution: | |
| class SolutionV1: |
| 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) | ||
|
|
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) |
답안 제출 문제
작성자 체크 리스트
In Review로 설정해주세요.검토자 체크 리스트
Important
본인 답안 제출 뿐만 아니라 다른 분 PR 하나 이상을 반드시 검토를 해주셔야 합니다!