Skip to content

Conversation

@imgolden77
Copy link
Contributor

답안 제출 문제

작성자 체크 리스트

  • Projects의 오른쪽 버튼(▼)을 눌러 확장한 뒤, Week를 현재 주차로 설정해주세요.
  • 문제를 모두 푸시면 프로젝트에서 StatusIn Review로 설정해주세요.
  • 코드 검토자 1분 이상으로부터 승인을 받으셨다면 PR을 병합해주세요.

검토자 체크 리스트

Important

본인 답안 제출 뿐만 아니라 다른 분 PR 하나 이상을 반드시 검토를 해주셔야 합니다!

  • 바로 이전에 올라온 PR에 본인을 코드 리뷰어로 추가해주세요.
  • 본인이 검토해야하는 PR의 답안 코드에 피드백을 주세요.
  • 토요일 전까지 PR을 병합할 수 있도록 승인해주세요.

Copy link

Copilot AI left a 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.

Comment on lines +3 to +29
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)

Copy link

Copilot AI Jan 1, 2026

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.

Suggested change
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 uses AI. Check for mistakes.
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:
Copy link

Copilot AI Jan 1, 2026

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.

Suggested change
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 uses AI. Check for mistakes.
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
Copy link

Copilot AI Jan 1, 2026

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).

Suggested change
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

Copilot uses AI. Check for mistakes.
Comment on lines +8 to +11
res = res + bit
res = res << 1
n = n >> 1
res = res >> 1
Copy link

Copilot AI Jan 1, 2026

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.

Suggested change
res = res + bit
res = res << 1
n = n >> 1
res = res >> 1
res = (res << 1) + bit
n = n >> 1

Copilot uses AI. Check for mistakes.
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:
Copy link

Copilot AI Jan 1, 2026

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.

Suggested change
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:

Copilot uses AI. Check for mistakes.
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
Copy link

Copilot AI Jan 1, 2026

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.

Suggested change
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

Copilot uses AI. Check for mistakes.
@@ -0,0 +1,57 @@
# First try O(n) time complexity
class Solution:
Copy link

Copilot AI Jan 1, 2026

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.

Suggested change
class Solution:
class SolutionV1:

Copilot uses AI. Check for mistakes.
Comment on lines +3 to +29
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)

Copy link

Copilot AI Jan 1, 2026

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.

Suggested change
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 uses AI. Check for mistakes.
@imgolden77 imgolden77 closed this Jan 1, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

Status: Solving

Development

Successfully merging this pull request may close these issues.

1 participant