diff --git a/3sum/essaysir.java b/3sum/essaysir.java new file mode 100644 index 0000000000..5dcc1475bf --- /dev/null +++ b/3sum/essaysir.java @@ -0,0 +1,35 @@ +import java.util.*; + +class Solution { + public List> threeSum(int[] nums) { + int n = nums.length; + Arrays.sort(nums); + + Set> result = new LinkedHashSet<>(); + + // N C 3 조합 전부 나열 + List> possible = new ArrayList<>(); + for (int i = 0; i < n; i++) { + for (int j = i + 1; j < n; j++) { + for (int k = j + 1; k < n; k++) { + possible.add(Arrays.asList(nums[i], nums[j], nums[k])); + } + } + } + + // 합이 0인 것만 걸러서 Set에 넣기 + for (int i = 0; i < possible.size(); i++) { + List array = possible.get(i); + int sum = 0; + for (int cur : array) { + sum += cur; + } + if (sum == 0) { + result.add(array); // Set이라 중복은 알아서 걸러짐 + } + } + + // 3) 리턴할 때 List로 변환 + return new ArrayList<>(result); + } +} 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); + } +} diff --git a/validate-binary-search-tree/essaysir.java b/validate-binary-search-tree/essaysir.java new file mode 100644 index 0000000000..6a4b2729f6 --- /dev/null +++ b/validate-binary-search-tree/essaysir.java @@ -0,0 +1,27 @@ +/** + * Definition for a binary tree node. + * public class TreeNode { + * int val; + * TreeNode left; + * TreeNode right; + * TreeNode() {} + * TreeNode(int val) { this.val = val; } + * TreeNode(int val, TreeNode left, TreeNode right) { + * this.val = val; + * this.left = left; + * this.right = right; + * } + * } + */ +class Solution { + public boolean isValidBST(TreeNode root) { + return validate(root, Long.MIN_VALUE, Long.MAX_VALUE); + } + + private boolean validate(TreeNode node, long low, long high) { + if (node == null) return true; + if (node.val <= low || node.val >= high) return false; + return validate(node.left, low, node.val) + && validate(node.right, node.val, high); + } +}