diff --git a/3sum/JEONGBEOMKO.java b/3sum/JEONGBEOMKO.java new file mode 100644 index 000000000..7c10aed5c --- /dev/null +++ b/3sum/JEONGBEOMKO.java @@ -0,0 +1,36 @@ +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; + +class Solution { + public List> threeSum(int[] nums) { + /* + time complexity: O(n^2) + space complexity: O(1) + */ + Arrays.sort(nums); + List> result = new ArrayList<>(); + + for (int i = 0; i < nums.length - 2; i++) { + if (i > 0 && nums[i] == nums[i - 1]) continue; + + int left = i + 1, right = nums.length - 1; + + while (left < right) { + int sum = nums[i] + nums[left] + nums[right]; + + if (sum < 0) { + left++; + } else if (sum > 0) { + right--; + } else { + result.add(Arrays.asList(nums[i], nums[left], nums[right])); + while (left < right && nums[left] == nums[left + 1]) left++; + while (left < right && nums[right] == nums[right - 1]) right--; + left++; right--; + } + } + } + return result; + } +} diff --git a/climbing-stairs/JEONGBEOMKO.java b/climbing-stairs/JEONGBEOMKO.java new file mode 100644 index 000000000..0fc7cdba7 --- /dev/null +++ b/climbing-stairs/JEONGBEOMKO.java @@ -0,0 +1,17 @@ +class Solution { + public int climbStairs(int n) { + + if (n == 1 || n == 2) { + return n; + } + + int[] cases = new int[n + 1]; + cases[1] = 1; + cases[2] = 2; + for (int i = 3; i <= n; i++) { + cases[i] = cases[i - 1] + cases[i - 2]; + } + + return cases[n]; + } +} diff --git a/product-of-array-except-self/JEONGBEOMKO.java b/product-of-array-except-self/JEONGBEOMKO.java new file mode 100644 index 000000000..0f9b179c1 --- /dev/null +++ b/product-of-array-except-self/JEONGBEOMKO.java @@ -0,0 +1,27 @@ +/* [time-complexity]: O(n) + [space-complexity]: O(1) + */ +public class JEONGBEOMKO { + + class Solution { + public int[] productExceptSelf(int[] nums) { + int n = nums.length; + int[] result = new int[n]; + + // 왼쪽 곱 계산 + result[0] = 1; + for (int i = 1; i < n; i++) { + result[i] = result[i - 1] * nums[i - 1]; + } + + // 오른쪽 곱과 동시에 결과 계산 + int rightProduct = 1; + for (int i = n - 1; i >= 0; i--) { + result[i] = result[i] * rightProduct; + rightProduct *= nums[i]; + } + + return result; + } + } +} diff --git a/valid-anagram/JEONGBEOMKO.java b/valid-anagram/JEONGBEOMKO.java new file mode 100644 index 000000000..ee54000e4 --- /dev/null +++ b/valid-anagram/JEONGBEOMKO.java @@ -0,0 +1,30 @@ +import java.util.HashMap; +import java.util.Map; + +public class Solution { + /* + time complexity: O(n) + space complexity: O(1) + */ + public boolean isAnagram(String s, String t) { + if (s.length() != t.length()) return false; + + Map sFrequency = new HashMap<>(); + + for (int i = 0; i < s.length(); i++) { + sFrequency.put(s.charAt(i), sFrequency.getOrDefault(s.charAt(i), 0) + 1); + } + + for (int i = 0; i < t.length(); i++) { + if (sFrequency.getOrDefault(t.charAt(i), 0) != 0) { + sFrequency.put(t.charAt(i), sFrequency.get(t.charAt(i)) - 1); + } + } + + for (int count : sFrequency.values()) { + if (count != 0) return false; + } + + return true; + } +} diff --git a/validate-binary-search-tree/JEONGBEOMKO.java b/validate-binary-search-tree/JEONGBEOMKO.java new file mode 100644 index 000000000..a0d76157d --- /dev/null +++ b/validate-binary-search-tree/JEONGBEOMKO.java @@ -0,0 +1,34 @@ +/** + * 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; + * } + * } + */ +/** + * time complexity: O(n) + * space complexity: O(h) + */ +class Solution { + public boolean isValidBST(TreeNode root) { + return isValidRange(root, Long.MIN_VALUE, Long.MAX_VALUE); + } + + private boolean isValidRange(TreeNode root, long min, long max) { + if (root == null) return true; + + if (root.val <= min || root.val >= max) return false; + + return isValidRange(root.left, min, root.val) + && isValidRange(root.right, root.val, max); + } + +}