Skip to content

[hoonjichoi1] WEEK 03 solutions#2716

Open
Hoonjichoi1 wants to merge 4 commits into
DaleStudy:mainfrom
Hoonjichoi1:main
Open

[hoonjichoi1] WEEK 03 solutions#2716
Hoonjichoi1 wants to merge 4 commits into
DaleStudy:mainfrom
Hoonjichoi1:main

Conversation

@Hoonjichoi1

@Hoonjichoi1 Hoonjichoi1 commented Jul 6, 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.

🏷️ 알고리즘 패턴 분석

  • 패턴: Backtracking, Depth-First Search, Greedy
  • 설명: dfs 재귀로 모든 가능한 조합을 탐색하며 합이 타깃과 같아지면 결과에 추가하는 방식으로 문제를 해결한다. 각 재귀에서 선택/포기 결정이 많아 백트래킹의 전형적 구조를 띄고 있고, 깊이 우선 탐색의 탐색 흐름을 따른다.

📊 시간/공간 복잡도 분석

복잡도
Time O(k * t)
Space O(t)

피드백: 백트래킹으로 모든 가능한 조합을 탐색하고 중복 없이 시작 위치를 고정하여 재귀를 진행합니다.

개선 제안: 현재 구현은 시간 복잡도가 후보 수와 목표합에 따라 크게 달라질 수 있습니다. 정렬 후 가지치기나 중복 제거를 추가하면 성능이 개선될 수 있습니다.

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

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.

🏷️ 알고리즘 패턴 분석

  • 패턴: Dynamic Programming, Divide and Conquer
  • 설명: 주어진 코드는 앞에서의 결정(dp[i] 누적)으로 전체 해를 합산하는 전형적인 DP 패턴이며, 각 위치의 해를 두 가지 경우(dp[i+1], dp[i+2])로 합산합니다. 부분문제의 중복 해결이 핵심입니다. 또한 문제의 재귀적 분할 형태가 바탕이므로 Divide and Conquer 성격도 보일 수 있습니다.

📊 시간/공간 복잡도 분석

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

피드백: 두 자리 수 해석 여부를 확인하며 순차적으로 DP 배열을 채웁니다. 0 처리와 경계 조건에 주의가 필요합니다.

개선 제안: 상수 공간으로 최적화 가능(두 변수만 사용하는 최적화). 예외 처리도 간단히 추가하면 안정적입니다.

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

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.

🏷️ 알고리즘 패턴 분석

  • 패턴: Bit Manipulation
  • 설명: 주어진 코드는 정수를 이진수로 보고 1의 개수를 세는 방식으로 비트 수를 계산합니다. 비트 연산 대신 나눗셈 모듈로를 활용해 각 자리의 1을 셈으로써 비트 패턴을 추적합니다.

📊 시간/공간 복잡도 분석

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

피드백: 입력 n의 이진 표현 길이만큼 반복하며 각 비트를 확인합니다.

개선 제안: n이 0일 때의 예외 처리와 루프 조건을 명확히 하면 더 안전합니다.

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

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.

🏷️ 알고리즘 패턴 분석

  • 패턴: Two Pointers, Hash Map / Hash Set
  • 설명: 문자열을 정제한 뒤 좌우 포인터를 동시에 이동시키며 대칭 여부를 확인하는 두 포인터 방식이 핵심 패턴입니다. 추가적으로 문자열 비교를 위해 문자 배열화하는 과정은 두 포인터의 필요 조건을 마련합니다.

📊 시간/공간 복잡도 분석

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

피드백: 비알파벳/숫자 문자 제거와 정규화를 거쳐 양끝 비교를 수행합니다.

개선 제안: 대체로 정규식을 사용한 전처리 대신 두 포인터로 직접 검사하는 방식도 고려해볼 만합니다.

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

@essaysir essaysir self-requested a review July 11, 2026 01:10

@parkhojeong parkhojeong left a comment

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.

수고하셨습니다. 마이너한 커멘트 남겼어요~

Comment on lines +12 to +22
for (int i = n-1; i > -1; i--) {
if (s.charAt(i) != '0') {
dp[i] += dp[i+1];
}
if (s.charAt(i) != '0' && i+1 < n) {
int twoDigit = Integer.valueOf(s.substring(i, i+2));
if (twoDigit > 0 && twoDigit < 27) {
dp[i] += dp[i+2];
}
}
}

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.

twoDigit >= 10 인 케이스만 존재해서 > 0 보다는 >= 10 으로 좁혀주는게 좋을 거 같습니다!

Suggested change
for (int i = n-1; i > -1; i--) {
if (s.charAt(i) != '0') {
dp[i] += dp[i+1];
}
if (s.charAt(i) != '0' && i+1 < n) {
int twoDigit = Integer.valueOf(s.substring(i, i+2));
if (twoDigit > 0 && twoDigit < 27) {
dp[i] += dp[i+2];
}
}
}
for (int i = n-1; i > -1; i--) {
if (s.charAt(i) != '0') {
dp[i] += dp[i+1];
}
if (s.charAt(i) != '0' && i+1 < n) {
int twoDigit = Integer.valueOf(s.substring(i, i+2));
if (twoDigit >= 10 && twoDigit < 27) {
dp[i] += dp[i+2];
}
}
}

Comment on lines +2 to +17
public int hammingWeight(int n) {

if (n == 1) {
return 1;
}

int curr = n, result = 1;
while (curr > 1) {
if (curr % 2 == 1) {
result++;
}
curr = curr / 2;
}

return result;
}

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.

루프 조건을 조금 바꿔주면 n == 1 처리를 별도로 해주지 않아도 될 거 같습니다!

Suggested change
public int hammingWeight(int n) {
if (n == 1) {
return 1;
}
int curr = n, result = 1;
while (curr > 1) {
if (curr % 2 == 1) {
result++;
}
curr = curr / 2;
}
return result;
}
public int hammingWeight(int n) {
int curr = n, result = 0;
while (curr > 0) {
if (curr % 2 == 1) {
result++;
}
curr = curr / 2;
}
return result;
}

public boolean isPalindrome(String s) {

// removing all non-alphanumeric characters and convert them to lower case
String conveted = s.replaceAll("[^a-zA-Z0-9]", "").toLowerCase();

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.

오타가 있네요

Suggested change
String conveted = s.replaceAll("[^a-zA-Z0-9]", "").toLowerCase();
String converted = s.replaceAll("[^a-zA-Z0-9]", "").toLowerCase();

Comment on lines +4 to +5
// removing all non-alphanumeric characters and convert them to lower case
String conveted = s.replaceAll("[^a-zA-Z0-9]", "").toLowerCase();

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.

실제로 돌려보니 replaceAll 비용이 비싸서 runtime 성능이 좋지는 않게 나오는 거 같아요.
직접 순회하는 방식이나 투포인터 등 다른 방식으로도 풀어보시면 좋을 거 같습니다.

구현체도 참고차 남겨드려요!
https://github.com/openjdk/jdk/blob/master/src/java.base/share/classes/java/util/regex/Matcher.java#L1221-L1234

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

Status: Solving

Development

Successfully merging this pull request may close these issues.

3 participants