Skip to content

[dahyeong-yun] WEEK 08 Solutions - #2811

Merged
dahyeong-yun merged 3 commits into
DaleStudy:mainfrom
dahyeong-yun:week-08
Aug 15, 2026
Merged

[dahyeong-yun] WEEK 08 Solutions#2811
dahyeong-yun merged 3 commits into
DaleStudy:mainfrom
dahyeong-yun:week-08

Conversation

@dahyeong-yun

@dahyeong-yun dahyeong-yun commented Aug 11, 2026

Copy link
Copy Markdown
Contributor

답안 제출 문제

작성자 체크 리스트

  • Projects의 오른쪽 버튼(▼)을 눌러 확장한 뒤, Week를 현재 주차로 설정해주세요.
  • 문제를 모두 푸시면 프로젝트에서 StatusIn Review로 설정해주세요.
  • 코드 검토자 1분 이상으로부터 승인을 받으셨다면 PR을 병합해주세요.

검토자 체크 리스트

Important

본인 답안 제출 뿐만 아니라 다른 분 PR 하나 이상을 반드시 검토를 해주셔야 합니다!

  • 바로 이전에 올라온 PR에 본인을 코드 리뷰어로 추가해주세요.
  • 본인이 검토해야하는 PR의 답안 코드에 피드백을 주세요.
  • 토요일 전까지 PR을 병합할 수 있도록 승인해주세요.

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/dahyeong-yun.java
/**
 * TC : O(1)
 *   - 32번의 루프를 2번 반복하므로 O(1)
 * SC : O(1)
 *   - 32칸 고정 길이의 stack이 필요하므로 O(1)
 */

class Solution {
    public int reverseBits(int n) {
        int answer = 0;
        Deque<Integer> stack = new ArrayDeque<>();

        for(int i = 0; i<32; i++) {
            stack.add(n % 2);
            n /= 2;
        }

        int j = 0;
        while(!stack.isEmpty()) {
            int bit = stack.getLast();
            stack.removeLast();
            answer += bit * Math.pow(2, j); 
            j += 1;
        }

        return answer;       
    }
}
  • 패턴: Stack / Queue, Bit Manipulation
  • 설명: 주어진 코드는 32비트 정수를 비트를 스택에 쌓고, 다시 꺼내며 자리수에 따라 반전된 비트를 구성한다. 비트 단위 조작과 스택 사용으로 비트 반전의 과정을 다룬다.

📊 시간/공간 복잡도 분석

복잡도
Time O(1)
Space O(1)

피드백: 고정된 32비트 길이의 루프와 고정 크기 스택으로 구성되어 있어 시간과 공간이 상수로 보장된다.

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

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

@dalestudy

dalestudy Bot commented Aug 11, 2026

Copy link
Copy Markdown
Contributor

📊 dahyeong-yun 님의 학습 현황

이번 주 제출 문제

문제 난이도 유형 분석
longest-repeating-character-replacement Medium ⚠️ 유형 불일치
palindromic-substrings Medium ✅ 의도한 유형
reverse-bits Easy ⚠️ 유형 불일치

누적 학습 요약

  • 풀이한 문제: 31 / 75개
  • 이번 주 유형 일치율: 33% (3문제 중 1문제 일치)

문제 풀이 현황

카테고리 진행도 완료
Array ■■■■■■□ 8 / 10 (Medium 5, Easy 3)
Dynamic Programming ■■■■■□□ 8 / 11 (Easy 1, Medium 7)
Matrix ■■■■□□□ 2 / 4 (Medium 2)
String ■■■■□□□ 5 / 10 (Medium 2, Easy 3)
Linked List ■■□□□□□ 2 / 6 (Easy 2)
Heap ■■□□□□□ 1 / 3 (Medium 1)
Graph ■■□□□□□ 2 / 8 (Medium 2)
Binary ■□□□□□□ 1 / 5 (Easy 1)
Tree ■□□□□□□ 2 / 14 (Easy 1, Medium 1)
Interval □□□□□□□ 0 / 5 ← 아직 시작 안 함

🤖 이 댓글은 GitHub App을 통해 자동으로 작성되었습니다.

🔢 API 사용량 (gpt-5-nano)
요청 입력 토큰 출력 토큰 합계 비용
1 388 82 470 $0.000052
2 866 167 1,033 $0.000110
3 1,332 227 1,559 $0.000157
합계 2,586 476 3,062 $0.000320

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.

한번 쉬프트 연산자를 활용해보시는건 어떨까요?
방금 저도 자바로 풀어봤는데 11줄까지 코드를 줄일수 있었어요!
if ((n & 1) == 1) answer |= 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.

또한 쉬프트연산자 쓰시면 별도의 스택같은 자료형이 필요 없기때문에 성능도 좋을거에요!

@JeonJe
JeonJe self-requested a review August 12, 2026 13: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.

🏷️ 알고리즘 패턴 분석

longest-repeating-character-replacement/dahyeong-yun.java
/**
 * TC : O(n)
 *   - 문자열의 길이 n 만큼 반복하고 루프 안에서는 고정 길이를 반복하므로 O(n)
 * SC : O(1)
 *   - 26개 알파벳의 카운트를 위한 배열을 생성하므로 O(1)
 */
class Solution {
    public int characterReplacement(String s, int k) {
        int max = 0;

        int len = s.length();
        int deleteTarget = 0;
        int[] count = new int[26];
        for(int i=0; i<len; i++) {
            char c = s.charAt(i);
            count[c - 'A']++;

            
            int maxCountAlphabet = 0;
            int total = count[0];
            for(int j=1; j<26; j++) {
                total += count[j];    
                if(count[j] > count[maxCountAlphabet]) maxCountAlphabet = j;
            }

            if(total - count[maxCountAlphabet] <= k) {
                max = Math.max(max, total);
            } else {
                count[s.charAt(deleteTarget++) - 'A']--;
            }
        }

        return max;
    }
}
  • 패턴: Sliding Window, Greedy, Hash Map / Hash Set
  • 설명: 문자열 슬라이딩 윈도우로 부분 문자열 길이를 확장하며, 최대 반복 문자 수를 유지해 k만큼의 대체로 길이를 늘리는 탐욕적 전략을 사용합니다. 문자 빈도 배열을 이용해 현재 윈도우에서 필요한 교체 수를 계산합니다.

📊 시간/공간 복잡도 분석

유저 분석 실제 분석 결과
Time O(n) O(n)
Space O(1) O(1)

피드백: 26개 알파벳의 카운트를 유지하는 배열과 윈도우 좌/우 포인터를 사용해 부분 문자열의 문자 다수의 갯수를 트래킹한다.

개선 제안: 현재 구현은 최대 등장 문자 인덱스를 갱신하는 로직은 있지만 maxCountAlphabet의 값을 직접 인덱스로 비교하는 부분에서 혼란을 줄 수 있다. 더 명확하게 maxCountAlphabet 값을 문자 빈도 중 최댓값의 문자 인덱스로 관리하면 오류를 줄일 수 있다.

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.

🏷️ 알고리즘 패턴 분석

palindromic-substrings/dahyeong-yun.java
/**
 * TC : O(n^2)
 *   - 문자열 길이 n 만큼 반복하고, 각 인덱스에서 n/2 만큼의 회문을 확인하므로 n * (n/2) => O(n^2)
 * SC : O(1)
 *   - 별도 유의미한 공간을 사용하지 않음 
 */
class Solution {
    public int countSubstrings(String s) {
        int len = s.length(), count = 0;

        for(int i = 0; i<len; i++) {
            int start = i, end = i;

            // 홀수 길이 회문 카운트
            while(
                start >= 0 && end < len && s.charAt(start) == s.charAt(end)
            ) {
                count++;
                start--;
                end++;
            }

            // 짝수 길이 회문 카운트
            start = i;
            end = i+1;
            while(
                start >= 0 && end < len && s.charAt(start) == s.charAt(end)
            ) {
                count++;
                start--;
                end++;
            }
        }

        return count;
    }
}
  • 패턴: Two Pointers, Monotonic Stack, Hash Map / Hash Set
  • 설명: 문자열의 각 인덱스에서 좌우로 확장하며 팰린드롬을 센다. 중앙 기준으로 홀수/짝수 길이의 회문을 확장하는 두 포인터 방식이 핵심 패턴이다.

📊 시간/공간 복잡도 분석

유저 분석 실제 분석 결과
Time O(n^2) O(n^2)
Space O(1) O(1)

피드백: 모든 위치에서 가운데를 기준으로 좌우로 확장하며 회문을 세는 방식으로 구현되어 있습니다.

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

@dahyeong-yun dahyeong-yun moved this from Solving to In Review in 리트코드 스터디 8기 Aug 14, 2026
* - 32칸 고정 길이의 stack이 필요하므로 O(1)
*/

class Solution {

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.

저는 시프트로 비트를 하나씩 꺼내 자리에 얹는 방식으로 풀었는데, 스택에 담았다가 반대로 꺼내는 접근도 있군요. "뒤집는다"는 동작이 스택에 그대로 드러나서 의도가 잘 읽혔습니다.

두 부분이 눈에 들어 왔는데,

  1. getLast() 후 removeLast()는 pollLast() 하나로도 될 것 같습니다!
  2. 그리고 answer += bit * Math.pow(2, j) 부분인데, Math.pow가 double을 반환하다 보니 암묵적 (int) 로 변환됩니다. 이 부분을 bit << j로 쓰면 정수 연산만으로 끝나서, 20만 건 기준으로는 333ms에서 35ms로 줄어들더라고요. 복잡도는 똑같이 O(1)이라 통과에는 영향이 없지만 상수 차이가 있어서 공유드려봅니다!

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

와우.. 상세한 설명 감사해요. 고민이 거기까지 미치지 못했는데 시야가 넓어지는 느낌이네요!

count[s.charAt(deleteTarget++) - 'A']--;
}
}

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.

코드 잘 읽었습니다!
count 26칸을 더한 total이 윈도우에 들어 있는 글자 수, 즉 윈도우 길이고, count[maxCountAlphabet]이 그 안에서 가장 많이 나온 문자의 개수네요.
그래서 total - count[maxCountAlphabet] <= k가 "바꿔야 할 개수가 k 이하인가"로 표현이 되네요!

저는 위 부분을 left, right와 windowLength로 표현했는데, 이번에 카운트의 합으로 length를 표현할 수 있다는걸 배워갑니다!

@dahyeong-yun
dahyeong-yun merged commit 70ba5e8 into DaleStudy:main Aug 15, 2026
1 check passed
@github-project-automation github-project-automation Bot moved this from In Review to Completed in 리트코드 스터디 8기 Aug 15, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

Status: Completed

Development

Successfully merging this pull request may close these issues.

3 participants