-
-
Notifications
You must be signed in to change notification settings - Fork 361
[freemjstudio] WEEK 08 Solutions #2821
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
Merged
+58
−0
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,44 @@ | ||
| from collections import Counter | ||
|
|
||
| # first solution -> O(N**2), time limit exceeded | ||
| class Solution: | ||
| def characterReplacement(self, s: str, k: int) -> int: | ||
| max_length = 0 | ||
|
|
||
| # window : length of the substring | ||
| for window in range(1, len(s)+1): | ||
| for i in range(len(s) - window + 1): | ||
| substring = s[i:i+window] | ||
|
|
||
| counter = Counter(substring) | ||
| max_count = max(counter.values()) | ||
| if window - max_count <= k: | ||
| max_length = max(max_length, window) | ||
|
|
||
| return max_length | ||
|
|
||
| # second solution | ||
| from collections import Counter | ||
|
|
||
| class Solution: | ||
| def characterReplacement(self, s: str, k: int) -> int: | ||
| max_length = 0 | ||
| counter = Counter() | ||
| left = 0 | ||
|
|
||
| for right in range(len(s)): | ||
| counter[s[right]] += 1 | ||
| max_count = max(counter.values()) | ||
|
|
||
| window = right - left + 1 | ||
| # window 사이즈가 조건에 맞도록 줄이기 | ||
| while window - max_count > k: | ||
| counter[s[left]] -= 1 | ||
| left += 1 | ||
| max_count = max(counter.values()) | ||
| window = right - left + 1 # update the window size because left has been moved | ||
|
|
||
| # 조건을 만족하는 window size 로 max length 를 갱신한다. | ||
| max_length = max(window, max_length) | ||
|
|
||
| return max_length |
|
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/freemjstudio.pyclass Solution:
def reverseBits(self, n: int) -> int:
# 1. convert integer into binary
binary = bin(n)[2:]
# 2. convert into 32bits
fill_zero = 32 - len(binary)
binary = "0" * fill_zero + binary
# 3. reverse the binary
reversed_binary = binary[::-1]
# 4. convert binary into integer
return int(reversed_binary,2)
📊 시간/공간 복잡도 분석
피드백: 정수의 이진 표현을 문자열로 다루고 왼쪽 패딩과 반전을 통해 역순 이진수를 얻는다. 개선 제안: 현재 구현은 직관적이지만, 비트 연산만으로 32비트 반전을 수행하면 더 빠르고 메모리 효율적이다. 예: 비트 마스크와 쉬프트를 이용한 역순 연산으로 O(1) 시간 복잡도를 유지할 수 있다.
|
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
| class Solution: | ||
| def reverseBits(self, n: int) -> int: | ||
| # 1. convert integer into binary | ||
| binary = bin(n)[2:] | ||
|
|
||
| # 2. convert into 32bits | ||
| fill_zero = 32 - len(binary) | ||
| binary = "0" * fill_zero + binary | ||
|
|
||
| # 3. reverse the binary | ||
| reversed_binary = binary[::-1] | ||
|
|
||
| # 4. convert binary into integer | ||
| return int(reversed_binary,2) | ||
|
freemjstudio marked this conversation as resolved.
|
||
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.