diff --git a/climbing-stairs/essaysir.java b/climbing-stairs/essaysir.java new file mode 100644 index 0000000000..ec04f09bbd --- /dev/null +++ b/climbing-stairs/essaysir.java @@ -0,0 +1,24 @@ +import java.util.*; + +class Solution { + // TC: O(2의 N승) + // SC: O(N) + public static Map 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; + } +} diff --git a/product-of-array-except-self/essaysir.java b/product-of-array-except-self/essaysir.java new file mode 100644 index 0000000000..cdd4e65b3f --- /dev/null +++ b/product-of-array-except-self/essaysir.java @@ -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; + } +} diff --git a/valid-anagram/essaysir.java b/valid-anagram/essaysir.java new file mode 100644 index 0000000000..07539194ad --- /dev/null +++ b/valid-anagram/essaysir.java @@ -0,0 +1,20 @@ +import java.util.*; + +class Solution { + public boolean isAnagram(String s, String t) { + // 둘이 anagram 이면 인지 아닌지 확인 해라 + // 아나 그램이 다시 만들 수 있는 가 == 들어있는 알파벳의 갯수가 동일한 가 + Map prevMap = new HashMap<>(); + Map 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); + } +}