Skip to content

[KwonNayeon] Week 8 solutions #1501

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

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 27 additions & 18 deletions longest-repeating-character-replacement/KwonNayeon.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,32 +8,41 @@
- 여기서 n은 문자열의 길이

Space Complexity: O(1)
- 추가 변수(left, right, max_length 등)는 상수 개
- 추가 변수(max_length, max_count, start, end 등) 이외의 공간 사용하지 않음

풀이방법:
1. Sliding Window로 구간을 관리
- right 포인터로 구간을 늘리다가
- 변경해야하는 문자 수가 k를 초과하면 left 포인터로 구간을 줄임
- end 포인터로 구간을 늘리다가
- 변경해야하는 문자 수가 k를 초과하면 start 포인터로 구간을 줄임

2. 각 구간에서:
- 가장 많이 등장한 문자로 나머지를 변경
- 나머지 문자를 가장 많이 등장한 문자로 변경
- (구간 길이 - 가장 많이 등장한 문자 수)가 k 이하여야 함
"""
class Solution:
def characterReplacement(self, s: str, k: int) -> int:
counter = {}
left = 0
max_length = 0

for right in range(len(s)):
counter[s[right]] = counter.get(s[right], 0) + 1

curr_length = right - left + 1
from collections import defaultdict

if curr_length - max(counter.values()) > k:
counter[s[left]] -= 1
left += 1

max_length = max(max_length, right - left + 1)

max_length = 0
max_count = 0
start = 0
char_count = defaultdict(int)

for end in range(len(s)):
# 현재 문자의 등장 횟수 증가
char_count[s[end]] += 1

# 윈도우 내 가장 많이 나타난 문자의 등장 횟수 업데이트
max_count = max(max_count, char_count[s[end]])

# 윈도우의 크기 - 가장 많이 나타난 문자의 등장 횟수 = 변경해야 할 문자의 수
# 이 때 변경하는 문자의 종류는 상관없음
# 이 값이 k보다 클 때, 윈도우의 크기를 줄임
if (end - start + 1) - max_count > k:
char_count[s[start]] -= 1
start += 1

# 최대 길이 업데이트
max_length = max(max_length, end - start + 1)

return max_length
22 changes: 19 additions & 3 deletions reverse-bits/KwonNayeon.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
2. 문자열 슬라이싱 [::-1]으로 비트를 뒤집음
3. int(reversed_binary, 2)로 뒤집은 이진수 문자열을 다시 정수로 변환함
"""

class Solution:
def reverseBits(self, n: int) -> int:

Expand All @@ -24,12 +23,29 @@ def reverseBits(self, n: int) -> int:
reversed_binary = binary[::-1]

return int(reversed_binary, 2)

# 코드를 간결하게 정리한 버전
class Solution:
def reverseBits(self, n: int) -> int:

return int(format(n, '032b')[::-1], 2)
"""
<Solution 2>

Time Complexity:
Time Complexity: O(1)
- 각 반복에서 비트 연산은 상수 시간이 걸림

Space Complexity:
Space Complexity: O(1)
- 사용되는 변수는 result와 입력값 n밖에 없음

풀이 방법:
- ...
"""
class Solution:
def reverseBits(self, n: int) -> int:
result = 0
for i in range(32):
result <<= 1 # 결과를 왼쪽으로 한 칸 밀고
result |= n & 1 # n의 마지막 비트를 결과에 추가
n >>= 1 # n을 오른쪽으로 한 칸 밀어 다음 비트로 이동
return result