Skip to content

[essaysir] WEEK 02 Solutions#2681

Open
essaysir wants to merge 7 commits into
DaleStudy:mainfrom
essaysir:week-02
Open

[essaysir] WEEK 02 Solutions#2681
essaysir wants to merge 7 commits into
DaleStudy:mainfrom
essaysir:week-02

Conversation

@essaysir

@essaysir essaysir commented Jun 29, 2026

Copy link
Copy Markdown
Contributor

답안 제출 문제

작성자 체크 리스트

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

검토자 체크 리스트

Important

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

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

@dalestudy

dalestudy Bot commented Jun 29, 2026

Copy link
Copy Markdown
Contributor

📊 essaysir 님의 학습 현황

이번 주 제출 문제

문제 난이도 유형 분석
3sum Medium ⚠️ 유형 불일치
climbing-stairs Easy ✅ 의도한 유형
product-of-array-except-self Medium ⚠️ 유형 불일치
valid-anagram Easy ✅ 의도한 유형
validate-binary-search-tree Medium ✅ 의도한 유형

누적 학습 요약

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

문제 풀이 현황

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

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

🔢 API 사용량 (gpt-5-nano)
요청 입력 토큰 출력 토큰 합계 비용
1 754 116 870 $0.000084
2 793 119 912 $0.000087
3 1,267 111 1,378 $0.000108
4 1,267 111 1,378 $0.000108
5 2,148 262 2,410 $0.000212
6 2,148 250 2,398 $0.000207
합계 8,377 969 9,346 $0.000806

@github-actions github-actions Bot added the java label Jun 29, 2026

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.

제 생각에 dfs 함수에 적절한 cache만 추가해도 시간복잡도가 엄청나게 좋아질것 같네요 ( O(N) )!

@Yiseull Yiseull self-requested a review June 29, 2026 10:46
- memo 를 이용한 시간 복잡도 단축

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.

오 이제 적절한 시간복잡도네요!
정답이 피보나치 수열인것까지 아신다면 공간복잡도도 훨씬 줄이실수 있으실것 같아요!
고생하셨습니다.

@essaysir essaysir moved this from Solving to In Review in 리트코드 스터디 8기 Jul 4, 2026
Comment thread 3sum/essaysir.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.

🏷️ 알고리즘 패턴 분석

  • 패턴: Brute Force, Hash Map / Hash Set
  • 설명: 세 가지 수의 모든 조합을 나열하고 합이 0인 경우를 걸러내는 방식으로, 문제의 해를 모두 탐색하는 brute-force 패턴에 더해 중복 제거를 위해 Hash Set을 활용합니다.

📊 시간/공간 복잡도 분석

복잡도
Time O(n^3)
Space O(n^2)

피드백: 전체 가능한 3중 루프를 통해 경우를 모두 나열한 뒤 합이 0인 경우를 Set에 저장하므로 비효율적이다. 중복 제거는 LinkedHashSet으로 수행되나 시간은 급격히 증가한다.

개선 제안: 고려해볼 만한 대안: 투 포인터를 이용한 정렬 후 O(n^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.

🏷️ 알고리즘 패턴 분석

  • 패턴: Dynamic Programming, Hash Map / Hash Set
  • 설명: 코드가 재귀로 피보나치 형태의 중복 부분 문제를 해결하며, memoization으로 결과를 저장해 다이나믹 프로그래밍 패턴을 사용합니다. 해시맵을 이용해 이미 계산한 값을 caching합니다.

📊 시간/공간 복잡도 분석

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

피드백: 메모화를 통해 중복 계산을 제거했지만 재귀 호출로 깊이가 커지면 스택오버플로가 발생할 수 있다.

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

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

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.

🏷️ 알고리즘 패턴 분석

  • 패턴: Greedy, Division
  • 설명: 배열 원소의 곱을 각 위치에서 나누어 구하는 방식은 분할 없이 전체 곱을 한 번 계산한 뒤 각 원소로 나누는 아이디어로, 탐색이나 최적화보다는 값의 조합 규칙에 의존하는 패턴입니다. 또한 0 처리 로직이 조건 분기로 구성되어 있어 간접적으로 Greedy 성격의 선택적 판단이 포함됩니다.

📊 시간/공간 복잡도 분석

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

피드백: 0의 개수에 따른 분기 처리로 0이 다수 있을 때도 올바르게 동작한다.

개선 제안: 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.

🏷️ 알고리즘 패턴 분석

  • 패턴: Hash Map / Hash Set
  • 설명: 두 문자열의 문자 빈도수를 해시 맵으로 비교하는 방식으로 아나그램 여부를 판단한다. 각 문자 등장 횟수를 맵에 누적하고 최종적으로 두 맵의 동일성으로 결과를 도출한다.

📊 시간/공간 복잡도 분석

복잡도
Time O(n + m)
Space O(k)

피드백: 두 맵을 비교하는 단순 구현으로 직관적이지만, 배열 차원으로 최적화 가능하다.

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

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

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.

🏷️ 알고리즘 패턴 분석

  • 패턴: Binary Search, Divide and Conquer
  • 설명: BST 유효성 검사를 재귀적으로 수행하며, 각 노드에 대해 허용 범위를 좁혀가며(left<값<right) 올바른 이진 탐색 트리인지 확인한다. 분할하여 문제를 부분 문제로 해결하는 방식으로 구현된 패턴이다.

📊 시간/공간 복잡도 분석

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

피드백: 전형적인 최소-와 최대 범위 검사를 통해 문제를 해결한다. 음수/양수 경계도 안전하게 처리한다.

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

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

int result = dfs(n-1) + dfs(n-2);
memo.put(n, result);
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.

전 DP로 풀었는데 재귀로도 접근할 수 있겠네요!

단순한거지만 아래처럼 쓰면 단순해질 것 같아요!

        if ( n <= 2) return n;

Comment on lines +4 to +19
public boolean isAnagram(String s, String t) {
// 둘이 anagram 이면 인지 아닌지 확인 해라
// 아나 그램이 다시 만들 수 있는 가 == 들어있는 알파벳의 갯수가 동일한 가
Map<Character,Integer> prevMap = new HashMap<>();
Map<Character,Integer> curMap = new HashMap<>();

for ( int i = 0; i < s.length(); i++ ){
prevMap.merge(s.charAt(i), 1, Integer::sum);
}

for ( int i = 0; i < t.length(); i ++){
curMap.merge(t.charAt(i),1 ,Integer::sum);
}

return prevMap.equals(curMap);
}

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.

약간의 성능 개선인데 맨 처음 s와 t의 길이가 다른 경우, 조기 종료할 수도 있습니다!

평소 파이썬으로 풀다가 요즘 자바로 푸려고 하고 있는데 풀이 배워갑니다👍

Comment on lines +2 to +25
public int[] productExceptSelf(int[] nums) {
// 시간 복잡도가 O(N) 이어야 함.
// 배열에서 나 자신을 빼고서 모두를 곱하라
// 어떻게 해야 시간 복잡도가 O(N) 이지 ?
int n = nums.length;
int zeroCount = 0;
int maxTotal = 1; // 0이 아닌 값들의 곱
for (int x : nums) {
if (x == 0) zeroCount++;
else maxTotal *= x;
}

int[] result = new int[n];
for (int i = 0; i < n; i++) {
if (zeroCount >= 2) {
result[i] = 0; // 0이 2개 이상 → 무조건 0
} else if (zeroCount == 1) {
result[i] = (nums[i] == 0) ? maxTotal : 0; // 0 위치만 살아남음
} else {
result[i] = maxTotal / nums[i]; // 0 없음 → 그냥 나눔
}
}
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.

나중에 기회되시면 나눗셈 없이 푸는 방법도 한 번 고민해보시면 좋을 것 같습니다!
곱하기로만 푸는 방법이 참신하더라구요!

@Yiseull Yiseull 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.

리뷰를 늦게 달아서 죄송합니다🙇🏻‍♀️ 수정 없이 바로 머지하시면 될 것 같습니다!

자바 풀이 보면서 많이 얻어갑니다! 이번 한 주도 고생하셨습니다😊

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

Labels

Projects

Status: In Review

Development

Successfully merging this pull request may close these issues.

4 participants