[njngwn] WEEK 08 Solutions - #2823
Merged
Merged
Conversation
Contributor
📊 njngwn 님의 학습 현황이번 주 제출 문제
누적 학습 요약
문제 풀이 현황
🤖 이 댓글은 GitHub App을 통해 자동으로 작성되었습니다. 🔢 API 사용량 (gpt-5-nano)
|
Contributor
There was a problem hiding this comment.
🏷️ 알고리즘 패턴 분석
longest-repeating-character-replacement/njngwn.py
class Solution:
# Time Complexity: O(n), n: len(s)
# Space Complexity: O(1)
def characterReplacement(self, s: str, k: int) -> int:
count = [0] * 26
max_len, max_cnt = 0, 0
left = 0
for right in range(len(s)): # window expands
ch = ord(s[right]) - ord('A')
count[ch] += 1
max_cnt = max(max_cnt, count[ch])
# len(substring) - len(most frequent character) > k => window needs to schrink
if (right - left + 1) - max_cnt > k:
count[ord(s[left]) - ord('A')] -= 1
left += 1
max_len = max(max_len, right - left + 1)
return max_len- 패턴: Sliding Window, Greedy
- 설명: 고정된 윈도우 크기로 문자 빈도수를 유지하며, 윈도우를 확장/축소하는 방식으로 부분 문자열의 조건을 만족시키는지 확인한다. 최대로 길이를 갱신하는 방식은 부분 문자열의 길이를 최대화하는 그리디적 아이디어와 연계된다.
📊 시간/공간 복잡도 분석
| 유저 분석 | 실제 분석 | 결과 | |
|---|---|---|---|
| Time | O(n) | O(n) | ✅ |
| Space | O(1) | O(1) | ✅ |
피드백: 알파벳 26개 카운트 배열을 사용하여 현재 윈도우의 각 문자 출현을 추적한다. 최대 빈도수를 갱신하며 윈도우를 확장/축소한다.
개선 제안: 현재 구현이 적절해 보입니다.
Contributor
There was a problem hiding this comment.
🏷️ 알고리즘 패턴 분석
reverse-bits/njngwn.py
class Solution:
# Time Complexity: O(1)
# Space Complexity: O(1)
def reverseBits(self, n: int) -> int:
stack = []
for i in range(32):
stack.append(n % 2)
n //= 2
res, multiples = 0, 1
while stack:
res += (stack.pop() * multiples)
multiples *= 2
return res- 패턴: Stack
- 설명: 주어진 코드는 비트를 스택에 차례로 넣고(pop) 다시 꺼내며 순서를 뒤집어 결과를 구성한다. 비트 역순을 얻기 위해 스택 활용 패턴이 적용되므로 Stack 패턴에 해당한다.
📊 시간/공간 복잡도 분석
| 유저 분석 | 실제 분석 | 결과 | |
|---|---|---|---|
| Time | O(1) | O(32) | ❌ |
| Space | O(1) | O(32) | ❌ |
피드백: 비트를 스택에 저장한 뒤 역순으로 합치는 간단한 구현이다. 상수 크기의 고정된 루프를 사용한다.
개선 제안: 코드의 의도를 더 명확히 하기 위해 비트 연산 기반의 풀이로도 대체하면 자주 요구되는 최적화가 가능하다.
Contributor
There was a problem hiding this comment.
🏷️ 알고리즘 패턴 분석
palindromic-substrings/njngwn.py
class Solution:
# Time Complexity: O(n^2), n: len(s)
# Space Complexity: O(1)
def countSubstrings(self, s: str) -> int:
cnt = 0
for i in range(len(s)):
# odd number
start, end = i, i
while start >= 0 and end < len(s) and s[start] == s[end]:
start -= 1
end += 1
cnt += 1
# even number
start, end = i, i + 1
while start >= 0 and end < len(s) and s[start] == s[end]:
start -= 1
end += 1
cnt += 1
return cnt- 패턴: Two Pointers, Monotonic Stack, Dynamic Programming
- 설명: 가장자어 확장으로 팰린드롬을 확장해가며 더해가는 방식으로 두 개의 포인터(start, end)를 가운데에서 양쪽으로 이동시키는 패턴이 핵심입니다. 이를 통해 모든 중심에서 팰린드롬을 탐색하는 'Two Pointers' 응용이고, 부분 문자열의 개수를 세는 데 사용됩니다.
📊 시간/공간 복잡도 분석
| 유저 분석 | 실제 분석 | 결과 | |
|---|---|---|---|
| Time | O(n^2) | O(n^2) | ✅ |
| Space | O(1) | O(1) | ✅ |
피드백: 각 위치를 중심으로 확장하며 대칭 여부를 확인하므로 최악의 경우 전체 문자열 길이에 대해 두 번의 확장을 수행합니다.
개선 제안: 현재 구현이 일반적인 확장 방식으로 충분히 효율적입니다.
parkhojeong
approved these changes
Aug 17, 2026
parkhojeong
left a comment
Contributor
There was a problem hiding this comment.
수고하셨습니다. 커멘트 간단히 남겨보았어요.
주차가 지나서 바로 머지하도록 하겠습니다.
Comment on lines
+15
to
+17
| if (right - left + 1) - max_cnt > k: | ||
| count[ord(s[left]) - ord('A')] -= 1 | ||
| left += 1 |
Comment on lines
+10
to
+20
| while start >= 0 and end < len(s) and s[start] == s[end]: | ||
| start -= 1 | ||
| end += 1 | ||
| cnt += 1 | ||
|
|
||
| # even number | ||
| start, end = i, i + 1 | ||
| while start >= 0 and end < len(s) and s[start] == s[end]: | ||
| start -= 1 | ||
| end += 1 | ||
| cnt += 1 |
Contributor
There was a problem hiding this comment.
초기값만 다르고 로직은 동일해서 헬퍼함수 하나를 두면 의도가 더 명확해질 거 같네요
Comment on lines
+5
to
+8
| stack = [] | ||
| for i in range(32): | ||
| stack.append(n % 2) | ||
| n //= 2 |
Contributor
There was a problem hiding this comment.
스택 없이 비트 연산으로 구현해보셔도 좋을 거 같습니다.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
답안 제출 문제
작성자 체크 리스트
In Review로 설정해주세요.검토자 체크 리스트
Important
본인 답안 제출 뿐만 아니라 다른 분 PR 하나 이상을 반드시 검토를 해주셔야 합니다!