-
-
Notifications
You must be signed in to change notification settings - Fork 361
[ICE0208] WEEK 08 Solutions #2818
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
|
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. 심플하게 풀어주셔서 이해가 잘 되네요
Member
Author
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. 리뷰 감사합니다! |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,25 @@ | ||
| class Solution { | ||
| public int characterReplacement(String s, int k) { | ||
| int[] frequency = new int[26]; | ||
| int maxFrequency = 0; | ||
| int left = 0; | ||
| int maxLength = 0; | ||
|
|
||
| for (int right = 0; right < s.length(); right++) { | ||
| int index = s.charAt(right) - 'A'; | ||
| frequency[index]++; | ||
|
|
||
| // maxFrequency 갱신 | ||
| maxFrequency = Math.max(maxFrequency, frequency[index]); | ||
|
|
||
| // right - left + 1 - maxFrequency : 현재 위도우에서 maxFrequency를 제외한 개수 | ||
| while (right - left + 1 - maxFrequency > k) { | ||
| frequency[s.charAt(left) - 'A']--; | ||
| left++; | ||
| } | ||
|
|
||
| maxLength = Math.max(maxLength, right - left + 1); | ||
|
Comment on lines
+16
to
+21
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.
Member
Author
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 maxLength; | ||
| } | ||
| } | ||
|
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/ICE0208.javaclass Solution {
public int reverseBits(int n) {
String binary = String.format("%32s", Integer.toBinaryString(n))
.replace(' ', '0');
String reversed = new StringBuilder(binary)
.reverse()
.toString();
return Integer.parseUnsignedInt(reversed, 2);
}
}
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/ICE0208.javaclass Solution2 {
public int reverseBits(int n) {
int result = 0;
for (int i = 0; i < 32; i++) {
int lastBit = n & 1; // 가장 오른쪽 비트 추출
result <<= 1; // 새 비트를 넣을 자리 확보
result |= lastBit; // 추출한 비트를 오른쪽 끝에 추가
n >>>= 1; // 처리한 비트를 버림.
}
return result;
}
}
class Solution {
public int reverseBits(int n) {
String binary = String.format("%32s", Integer.toBinaryString(n))
.replace(' ', '0');
String reversed = new StringBuilder(binary)
.reverse()
.toString();
return Integer.parseUnsignedInt(reversed, 2);
}
}
📊 시간/공간 복잡도 분석
풀이 1:
|
| 복잡도 | |
|---|---|
| Time | O(32) |
| Space | O(1) |
피드백: 정수의 각 비트를 차례대로 반전시켜 누적 결과를 생성하는 방법으로, 고정된 32비트에 대해 일정한 시간과 상수 공간을 사용합니다.
개선 제안: 현재 구현이 적절해 보입니다.
풀이 2: Solution.reverseBits — Time: O(32) / Space: O(32)
| 복잡도 | |
|---|---|
| Time | O(32) |
| Space | O(32) |
피드백: 문자열 변환과 파싱으로 구현했지만 비트 연산 기반 방법보다 상수 공간을 더 많이 사용할 수 있으며, 비트 조작에 의존하는 것이 일반적입니다.
개선 제안: 고려해볼 만한 대안: 비트 연산만으로 구현하는 방법으로 리팩토링
💡 풀이에 시간/공간 복잡도를 주석으로 남겨보세요!
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,29 @@ | ||
| class Solution2 { | ||
| public int reverseBits(int n) { | ||
| int result = 0; | ||
|
|
||
| for (int i = 0; i < 32; i++) { | ||
| int lastBit = n & 1; // 가장 오른쪽 비트 추출 | ||
|
|
||
| result <<= 1; // 새 비트를 넣을 자리 확보 | ||
| result |= lastBit; // 추출한 비트를 오른쪽 끝에 추가 | ||
|
|
||
| n >>>= 1; // 처리한 비트를 버림. | ||
| } | ||
|
|
||
| return result; | ||
| } | ||
| } | ||
|
|
||
| class Solution { | ||
| public int reverseBits(int n) { | ||
| String binary = String.format("%32s", Integer.toBinaryString(n)) | ||
| .replace(' ', '0'); | ||
|
|
||
| String reversed = new StringBuilder(binary) | ||
| .reverse() | ||
| .toString(); | ||
|
|
||
| return Integer.parseUnsignedInt(reversed, 2); | ||
| } | ||
| } |
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/ICE0208.java
📊 시간/공간 복잡도 분석
풀이 1:
Solution.characterReplacement— Time: O(n) / Space: O(1)피드백: 고정된 알파벳 크기 26으로 freq를 관리하고, 윈도우를 한 방향으로 확장하면서 필요 시 좌측 포인터를 이동시킨다.
개선 제안: 현재 구현이 적절해 보입니다.
풀이 2:
Solution.reverseBits— Time: O(32) / Space: O(1)피드백: 문자열 변환과 역순 문자열로의 변환을 통해 비트를 반전시키는 직관적 방법이다.
개선 제안: 현재 구현이 적절해 보입니다.