diff --git a/3sum/YoungSeok-Choi.java b/3sum/YoungSeok-Choi.java new file mode 100644 index 000000000..cd372b1a7 --- /dev/null +++ b/3sum/YoungSeok-Choi.java @@ -0,0 +1,65 @@ +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Collections; +import java.util.HashSet; +import java.util.List; +import java.util.Set; + +// NOTE: 실행시간 1.5초.. +// 시간 복잡도: O(n^2) +class Solution { + public List> threeSum(int[] nums) { + + Arrays.sort(nums); + Set> result = new HashSet<>(); + + for(int i = 0; i < nums.length - 2; i++) { + int startIdx = i + 1; + int endIdx = nums.length - 1; + while(startIdx < endIdx) { + int sum = nums[i] + nums[startIdx] + nums[endIdx]; + if(sum == 0) { + result.add(Arrays.asList(nums[i], nums[startIdx], nums[endIdx])); + } + + if(sum > 0) { + endIdx--; + } else { + startIdx++; + } + } + } + + return new ArrayList<>(result); + } +} + + + +// 309 / 314 testcases passed Time Limit Exceeded +// NOTE: 시간복잡도 O(n^3) +class WrongSolution { + public List> threeSum(int[] nums) { + Set> res = new HashSet<>(); + + List temp = new ArrayList<>(); + + temp.remove(3); + + for(int i = 0; i < nums.length; i++) { + for(int j = 0; j < i; j++) { + for(int k = 0; k < j; k++) { + + if((nums[i] + nums[j] + nums[k]) == 0) { + List solution = Arrays.asList(nums[i], nums[j], nums[k]); + Collections.sort(solution); + res.add(solution); + } + } + } + } + + + return new ArrayList<>(res); + } +} diff --git a/climbing-stairs/YoungSeok-Choi.java b/climbing-stairs/YoungSeok-Choi.java new file mode 100644 index 000000000..819f96ba4 --- /dev/null +++ b/climbing-stairs/YoungSeok-Choi.java @@ -0,0 +1,21 @@ +// NOTE: 백준 N과 M문제와 거의 동일한 문제 +// memo(n) = memo(n-1)의 경우에 수에서 1칸을 올라가는 경우 + memo(n-2) 의 경우의 수에서 2 칸을 올라가는 경우의 점화식으로 표현됨. +// 시간 복잡도: O(n) +class Solution { + public int climbStairs(int n) { + int[] memo = new int[n + 1]; + + + if(n < 3) { + return n; + } + + memo[1] = 1; + memo[2] = 2; + for(int i = 3; i < memo.length; i++) { + memo[i] = memo[i - 1] + memo[i - 2]; + } + + return memo[n]; + } +} diff --git a/top-k-frequent-elements/YoungSeok-Choi.java b/top-k-frequent-elements/YoungSeok-Choi.java new file mode 100644 index 000000000..6fd7e10bc --- /dev/null +++ b/top-k-frequent-elements/YoungSeok-Choi.java @@ -0,0 +1,32 @@ +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + + +// O(nlogn) 시간복잡도. + +class Solution { + public int[] topKFrequent(int[] nums, int k) { + Map freqMap = new HashMap<>(); + int[] res = new int [k]; + + for(int i = 0; i < nums.length; i++) { + if(freqMap.containsKey(nums[i])) { + freqMap.put(nums[i], freqMap.get(nums[i]) + 1); + } else { + freqMap.put(nums[i], 1); + } + } + + List> entList = new ArrayList<>(freqMap.entrySet()); + + entList.sort((a, b) -> b.getValue().compareTo(a.getValue())); + + for(int i = 0; i < res.length; i++) { + res[i] = entList.get(i).getKey(); + } + + return res; + } +} diff --git a/valid-anagram/YoungSeok-Choi.java b/valid-anagram/YoungSeok-Choi.java new file mode 100644 index 000000000..5ea85e1f1 --- /dev/null +++ b/valid-anagram/YoungSeok-Choi.java @@ -0,0 +1,56 @@ +import java.util.HashMap; +import java.util.Map; + + +// NOTE: anagram 자체를 정확하게 이해하지 못해서 몇 번 틀렸던 문제. +// 두 문자열의 길이가 다르면 anagram이 아니다. +// 같은 알파뱃이 같은 개수가 있는지 확인을 하는게 문제의 핵심. + +// NOTE: 시간복잡도: O(n+m) +class Solution { + public boolean isAnagram(String s, String t) { + Map sMap = new HashMap<>(); + Map tMap = new HashMap<>(); + + int tLen = t.toCharArray().length; + int sLen = s.toCharArray().length; + + if(tLen != sLen) { + return false; + } + + for(char c: s.toCharArray()) { + if(sMap.containsKey(c)) { + int cnt = sMap.get(c); + sMap.put(c, cnt + 1); + } else { + sMap.put(c, 1); + } + } + + for(char c: t.toCharArray()) { + if(tMap.containsKey(c)) { + int cnt = tMap.get(c); + tMap.put(c, cnt + 1); + } else { + tMap.put(c, 1); + } + } + + + for(Character c: sMap.keySet()) { + if(!tMap.containsKey(c)) { + return false; + } + + int sMapCnt = sMap.get(c); + int tMapCnt = tMap.get(c); + + if(sMapCnt != tMapCnt) { + return false; + } + } + + return true; + } +} diff --git a/validate-binary-search-tree/YoungSeok-Choi.java b/validate-binary-search-tree/YoungSeok-Choi.java new file mode 100644 index 000000000..22106e1e4 --- /dev/null +++ b/validate-binary-search-tree/YoungSeok-Choi.java @@ -0,0 +1,88 @@ +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; + +/** + * 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; + * } + * } + */ +// NOTE: 답지를 보고 풀었던 문제.. 정확하게 이해하는 과정이 필요하다. +// TODO: 별도 블로그로 풀이과정을 정리할 것. +class Solution { + public boolean isValidBST(TreeNode root) { + return dfs(root, Long.MIN_VALUE, Long.MAX_VALUE); + } + + public boolean dfs(TreeNode cur, long min, long max) { + if(cur == null) return true; + + if(cur.val <= min || cur.val >= max) return false; + + return dfs(cur.left, min, cur.val) && dfs(cur.right, cur.val, max); + } +} + +class WrongSolution { + public boolean isValidBST(TreeNode root) { + return dfs(root, new ArrayList<>(Arrays.asList(root.val))); + } + + public boolean dfs(TreeNode cur, List path) { + + if(cur.left != null) { + int lVal = cur.left.val; + for(int i = 0; i < path.size(); i++) { + if(i == path.size() - 1) { + + if(lVal >= path.get(i)) { + return false; + } + + } else { + if(lVal < path.get(i)) { + return false; + } + } + } + + path.add(cur.left.val); + if(!dfs(cur.left, path)) return false; + path.remove(Integer.valueOf(cur.left.val)); + } + + if(cur.right != null) { + int rVal = cur.right.val; + for(int i = 0; i < path.size(); i++) { + if(i == path.size() - 1) { + + if(rVal <= path.get(i)) { + return false; + } + + + } else { + if(rVal > path.get(i)) { + return false; + } + } + } + + path.add(cur.right.val); + if(!dfs(cur.right, path)) return false; + path.remove(Integer.valueOf(cur.right.val)); + } + + return true; + } +}