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.

🏷️ 알고리즘 패턴 분석

  • 패턴: Dynamic Programming, Hash Map / Hash Set
  • 설명: 피보나치 형태의 중복된 재귀를 메모이제이션으로 해결하는 패턴으로 DP를 이용하고, 중간 결과를 HashMap에 저장하여 재계산을 줄인다.

📊 시간/공간 복잡도 분석

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

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

피드백: 메모이제이션(Map)으로 중복 계산을 제거하여 선형 시간 복잡도와 선형 공간 복잡도를 가진다.

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

풀이 2: Solution.isAnagram — Time: O(n + m) / Space: O(k)
복잡도
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.

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

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으로 변환하는것도 연습해보시면 좋을거에요!

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;
}
}
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