Skip to content
Open
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
24 changes: 24 additions & 0 deletions climbing-stairs/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.

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

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.

한 번 그렇게 수정해보도록 하겠습니다!! 감사합니다!!

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
Contributor

Choose a reason for hiding this comment

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

이런 DP를 쓰는 문제의 경우 Top-down이 생각해내긴 훨씬 쉽지만 몇몇 문제들은 시간제한이 애매하게 걸려 있어서
재귀 호출할때 오버헤드가 생기는것 때문에 TLE가 될수도 있기 때문에
Bottom-up으로 변환하는것도 연습해보시면 좋을거에요!

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
  • 설명: 피보나치 변형으로 중복 부분 문제를 해결하기 위해 메모이제이션을 사용한 동적 계획법 패턴이며, 해의 중복 계산을 해시맵으로 저장합니다.

📊 시간/공간 복잡도 분석

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

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

피드백: 중복 재귀를 메모이제이션으로 제거하여 각 n에 대해 한 번만 계산하도록 구현했습니다.

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

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

피드백: 하나의 배열과 상수 공간으로 각 위치의 곱을 계산합니다. 제약 조건에 따라 0의 위치 처리도 포함합니다.

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

풀이 3: Solution.isAnagram — Time: O(n + m) / Space: O(k)
복잡도
Time O(n + m)
Space O(k)

피드백: 두 맵의 빈도수를 비교해 anagram 여부를 판단합니다.

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

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

Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import java.util.*;

class Solution {
// TC: O(2의 N승)
// SC: O(N)
public static Map<Integer, Integer> memo = new HashMap<>();

public int climbStairs(int n) {
// 1과 2로만 움직일 수 있을 때, 도달할 수 있는 모든 방법의 수에 대해 구하시오
// DPS (QUEUE) , BPS (STACK)
return dfs(n);
}

// dfs(5) -> dfs(3) + dfs(4) -> dfs(2) + dfs(1) + dfs(3) + dfs(2)
public static int dfs(int n){
if ( n == 1) return 1;
if ( n == 2) return 2;
if ( memo.containsKey(n) ) return memo.get(n);

int result = dfs(n-1) + dfs(n-2);
memo.put(n, result);
return result;
}
}
26 changes: 26 additions & 0 deletions product-of-array-except-self/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.

🏷️ 알고리즘 패턴 분석

  • 패턴: Hash Map / Hash Set, Greedy, Divide and Conquer
  • 설명: 배열의 곱을 이용해 각 원소를 제외한 곱을 구하는 방식은 부분 문제의 최적화로 볼 수 있으며, 0의 개수에 따른 분기 로직은 문제를 단순화하는 탐욕적 접근처럼 보일 수 있습니다. 또한 전체 곱과 제수로 나누어 구하는 아이디어는 문제를 분해해 해결하는 패턴으로 해석될 여지가 있습니다.

Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
class Solution {
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;
}
}
20 changes: 20 additions & 0 deletions valid-anagram/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.

🏷️ 알고리즘 패턴 분석

  • 패턴: Hash Map / Hash Set
  • 설명: 두 문자열의 문자 빈도를 해시맵으로 세어 서로 동일한지 비교하는 방식으로 아나그램 여부를 판단한다. 각 문자 등장 횟수를 맵에 저장하고, 두 맵의 값을 비교하는 것이 핵심이다.

Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import java.util.*;

class Solution {
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);
}
}
Loading