-
-
Notifications
You must be signed in to change notification settings - Fork 361
[njngwn] WEEK 08 Solutions #2823
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,21 @@ | ||
| 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 | ||
|
Comment on lines
+15
to
+17
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 좋은 접근인 거 같습니다 |
||
|
|
||
| max_len = max(max_len, right - left + 1) | ||
|
|
||
| return max_len | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🏷️ 알고리즘 패턴 분석palindromic-substrings/njngwn.pyclass 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
📊 시간/공간 복잡도 분석
피드백: 각 위치를 중심으로 확장하며 대칭 여부를 확인하므로 최악의 경우 전체 문자열 길이에 대해 두 번의 확장을 수행합니다. 개선 제안: 현재 구현이 일반적인 확장 방식으로 충분히 효율적입니다. |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
| 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 | ||
|
Comment on lines
+10
to
+20
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 초기값만 다르고 로직은 동일해서 헬퍼함수 하나를 두면 의도가 더 명확해질 거 같네요 |
||
|
|
||
| return cnt | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🏷️ 알고리즘 패턴 분석reverse-bits/njngwn.pyclass 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
📊 시간/공간 복잡도 분석
피드백: 비트를 스택에 저장한 뒤 역순으로 합치는 간단한 구현이다. 상수 크기의 고정된 루프를 사용한다. 개선 제안: 코드의 의도를 더 명확히 하기 위해 비트 연산 기반의 풀이로도 대체하면 자주 요구되는 최적화가 가능하다. |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| 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 | ||
|
Comment on lines
+5
to
+8
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 스택 없이 비트 연산으로 구현해보셔도 좋을 거 같습니다. |
||
|
|
||
| res, multiples = 0, 1 | ||
| while stack: | ||
| res += (stack.pop() * multiples) | ||
| multiples *= 2 | ||
|
|
||
| return res | ||
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.
🏷️ 알고리즘 패턴 분석
longest-repeating-character-replacement/njngwn.py
📊 시간/공간 복잡도 분석
피드백: 알파벳 26개 카운트 배열을 사용하여 현재 윈도우의 각 문자 출현을 추적한다. 최대 빈도수를 갱신하며 윈도우를 확장/축소한다.
개선 제안: 현재 구현이 적절해 보입니다.