Skip to content
Merged
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
25 changes: 25 additions & 0 deletions longest-repeating-character-replacement/ICE0208.java

Copy link
Copy Markdown
Contributor

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
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);
        }
        return maxLength;
    }
}
  • 패턴: Sliding Window, Greedy
  • 설명: 길이 k 이내로 문자를 바꿔 최장 같은 문자 부분 문자열 길이를 구하는 방식으로, 창 크기를 조정하며 현재 윈도우에서의 최대 빈도수를 유지하는 Sliding Window 패턴과 조건을 만족하는 최장 길이를 구하기 위한 탐욕적 접근(Greedy)의 조합으로 판단됩니다.

📊 시간/공간 복잡도 분석

ℹ️ 이 파일에는 2가지 풀이가 포함되어 있어 각각 분석합니다.

풀이 1: Solution.characterReplacement — Time: O(n) / Space: O(1)
복잡도
Time O(n)
Space O(1)

피드백: 고정된 알파벳 크기 26으로 freq를 관리하고, 윈도우를 한 방향으로 확장하면서 필요 시 좌측 포인터를 이동시킨다.

개선 제안: 현재 구현이 적절해 보입니다.

풀이 2: Solution.reverseBits — Time: O(32) / Space: O(1)
복잡도
Time O(32)
Space O(1)

피드백: 문자열 변환과 역순 문자열로의 변환을 통해 비트를 반전시키는 직관적 방법이다.

개선 제안: 현재 구현이 적절해 보입니다.

💡 풀이에 시간/공간 복잡도를 주석으로 남겨보세요!

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

심플하게 풀어주셔서 이해가 잘 되네요

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The 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

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

right - left + 1 부분을 변수로 선언하는 건 어떨까요?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

코드 가독성에 도움이 될 수 있겠네요. 감사합니다👍

}
return maxLength;
}
}
29 changes: 29 additions & 0 deletions reverse-bits/ICE0208.java

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🏷️ 알고리즘 패턴 분석

reverse-bits/ICE0208.java
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);
    }
}
  • 패턴: Bit Manipulation, Divide and Conquer
  • 설명: 주어진 코드는 비트를 문자열로 다루어 32비트 이진 표현을 뒤집은 후 다시 정수로 해석한다. 비트 단위 변환과 역순 처리로 비트 조작 패턴에 해당하며, 문자열 기반으로 구현되지만 핵심은 비트 조작 아이디어를 활용한다.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🏷️ 알고리즘 패턴 분석

reverse-bits/ICE0208.java
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);
    }
}
  • 패턴: Bit Manipulation, Two Pointers
  • 설명: 첫 풀이에서 비트를 왼쪽으로 시프트하고 마지막 비트를 추출해 반대로 뒤집는 방식은 비트 조작(Bit Manipulation)이며, 비트를 순차적으로 다루어 반전 위치를 맞추는 형태로 볼 수 있습니다. 두 번째 풀이도 비트를 문자열로 다루는 비트 조작의 변형으로 해석할 수 있습니다.

📊 시간/공간 복잡도 분석

ℹ️ 이 파일에는 2가지 풀이가 포함되어 있어 각각 분석합니다.

풀이 1: Solution2.reverseBits — Time: O(32) / Space: O(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);
}
}
Loading