-
-
Notifications
You must be signed in to change notification settings - Fork 195
[sungjinwi] Week 08 solution #1511
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
+80
−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,51 @@ | ||
/* | ||
s는 영어 대문자만 포함 : 26의 크기를 가진 vector counts에 빈도 저장 | ||
left, right 통해 슬라이딩 윈도우 기법 적용 | ||
1. right 위치의 문자 빈도 수를 증가시키고 maxCnt를 업데이트한다 | ||
- maxCnt는 현재까지 윈도우를 움직이면서 윈도우 내에 한가지 문자의 최대 빈도수 | ||
2. (윈도우의 크기 - maxCnt) > k일 때 최대 대체 가능한 문자의 수를 넘어섰으므로 윈도우의 크기를 줄임 | ||
: s[left]의 빈도 감소 및 left를 전진 | ||
3. ans는 max(현재 윈도우의 크기, 기존 최대 크기 ans) 로 업데이트 | ||
- ans의 증가는 maxCnt가 증가 | 최대빈도문자와 다른 문자가 k개 이하일 때 -> (maxCnt ~ maxCnt + k)로 업데이트 | ||
이 때는 while 루프를 수행하지 않음 | ||
- while (right - left + 1 - maxCnt > k) -> 실제 이 조건을 만족하는 윈도우에서는 대체해야할 문자가 k보다 많이 존재할 수도 있다 | ||
다만 윈도우의 크기를 maxCnt + k개로 제한하기 때문에 실제 답보다 더 크게 업데이트 될 수 없다 | ||
매번 윈도우 내의 최대빈도문자를 계산하지 않아도 되기 때문에 살짝 최적화 O(26n) -> O(n) | ||
s의 길이 : N | ||
TC : O(N) | ||
SC : O(1) | ||
상수(26) 크기의 배열 | ||
*/ | ||
|
||
#include <string> | ||
#include <vector> | ||
using namespace std; | ||
|
||
class Solution { | ||
public: | ||
int characterReplacement(string s, int k) { | ||
vector<int> counts(26, 0); | ||
int left = 0, right = 0, maxCnt = 0, ans = 0; | ||
|
||
for(; right < s.size(); right++) { | ||
maxCnt = max(maxCnt, ++counts[s[right] - 'A']); | ||
|
||
while (right - left + 1 - maxCnt > k) { | ||
--counts[s[left] - 'A']; | ||
left++; | ||
} | ||
ans = max(ans, right - left + 1); | ||
} | ||
return ans; | ||
} | ||
}; | ||
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,29 @@ | ||
/* | ||
풀이 : | ||
n & 1 -> n의 오른쪽 첫번째 비트 -> ans에 더한다 | ||
n >> 1 & 1 -> n의 오른쪽 두번째 비트 -> ans를 << 1 해준뒤 더한다 | ||
... | ||
|
||
n의 오른쪽 끝자리부터 ans에 더해져서 << 연산에 의해 좌측으로 가기 때문에 비트를 뒤집을 수 있다 | ||
|
||
TC : O(1) | ||
32번 | ||
|
||
SC : O(1) | ||
s*/ | ||
|
||
#include <stdint.h> | ||
|
||
class Solution { | ||
public: | ||
uint32_t reverseBits(uint32_t n) { | ||
uint32_t ans = 0; | ||
|
||
for (int i = 0; i < 32; i++) { | ||
ans <<= 1; | ||
ans += n & 1; | ||
n >>= 1; | ||
} | ||
return ans; | ||
} | ||
}; |
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.
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.
슬라이딩 윈도우 내 maxCnt가 고정이라, left 증가 후에도 갱신되지 않는 점은 약간의 정밀도 손해가 있을 수 있어요.
윈도우가 수축된 이후 실제 최대 빈도 문자를 다시 계산해보는 것도 고려해볼 수 있을 것 같아요.
물론 시간복잡도 상 이점과의 균형을 고려한 선택이라면 현재 방식도 타당합니다 :)