From cf5cf45405f9bc6542872113be6a4c8a69c61d17 Mon Sep 17 00:00:00 2001 From: "Yongseok.choi" Date: Mon, 7 Apr 2025 22:33:23 +0900 Subject: [PATCH 1/4] add: weekly two ps start --- climbing-stairs/YoungSeok.java | 24 +++++++++++++ valid-anagram/YoungSeok-Choi.java | 56 +++++++++++++++++++++++++++++++ 2 files changed, 80 insertions(+) create mode 100644 climbing-stairs/YoungSeok.java create mode 100644 valid-anagram/YoungSeok-Choi.java diff --git a/climbing-stairs/YoungSeok.java b/climbing-stairs/YoungSeok.java new file mode 100644 index 000000000..6af851e7b --- /dev/null +++ b/climbing-stairs/YoungSeok.java @@ -0,0 +1,24 @@ + +// 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]; + } +} \ No newline at end of file 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; + } +} From eab191bb645b5e15813764499c81c94577f99937 Mon Sep 17 00:00:00 2001 From: "Yongseok.choi" Date: Mon, 7 Apr 2025 23:00:33 +0900 Subject: [PATCH 2/4] add: ps and fix lint --- .../{YoungSeok.java => YoungSeok-Choi.java} | 5 +-- top-k-frequent-elements/YoungSeok-Choi.java | 32 +++++++++++++++++++ 2 files changed, 33 insertions(+), 4 deletions(-) rename climbing-stairs/{YoungSeok.java => YoungSeok-Choi.java} (99%) create mode 100644 top-k-frequent-elements/YoungSeok-Choi.java diff --git a/climbing-stairs/YoungSeok.java b/climbing-stairs/YoungSeok-Choi.java similarity index 99% rename from climbing-stairs/YoungSeok.java rename to climbing-stairs/YoungSeok-Choi.java index 6af851e7b..819f96ba4 100644 --- a/climbing-stairs/YoungSeok.java +++ b/climbing-stairs/YoungSeok-Choi.java @@ -1,8 +1,5 @@ - // NOTE: 백준 N과 M문제와 거의 동일한 문제 // memo(n) = memo(n-1)의 경우에 수에서 1칸을 올라가는 경우 + memo(n-2) 의 경우의 수에서 2 칸을 올라가는 경우의 점화식으로 표현됨. - - // 시간 복잡도: O(n) class Solution { public int climbStairs(int n) { @@ -21,4 +18,4 @@ public int climbStairs(int n) { return memo[n]; } -} \ No newline at end of file +} 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; + } +} From a2f51e1b0bb5196079e0ca5e3b087da0e6a51126 Mon Sep 17 00:00:00 2001 From: "Yongseok.choi" Date: Fri, 11 Apr 2025 07:48:57 +0900 Subject: [PATCH 3/4] =?UTF-8?q?add:=20=EB=82=98=EB=A8=B8=EC=A7=80=20?= =?UTF-8?q?=EB=AC=B8=EC=A0=9C=ED=92=80=EC=9D=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- 3sum/YoungSeok-Choi.java | 65 ++++++++++++++ .../YoungSeok-Choi.java | 88 +++++++++++++++++++ 2 files changed, 153 insertions(+) create mode 100644 3sum/YoungSeok-Choi.java create mode 100644 validate-binary-search-tree/YoungSeok-Choi.java diff --git a/3sum/YoungSeok-Choi.java b/3sum/YoungSeok-Choi.java new file mode 100644 index 000000000..f3a5ea201 --- /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); + } +} \ No newline at end of file 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; + } +} From 561667219084e4f190a1f77ad2fd7b951d677961 Mon Sep 17 00:00:00 2001 From: "Yongseok.choi" Date: Fri, 11 Apr 2025 07:50:18 +0900 Subject: [PATCH 4/4] fix: lint --- 3sum/YoungSeok-Choi.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/3sum/YoungSeok-Choi.java b/3sum/YoungSeok-Choi.java index f3a5ea201..cd372b1a7 100644 --- a/3sum/YoungSeok-Choi.java +++ b/3sum/YoungSeok-Choi.java @@ -62,4 +62,4 @@ public List> threeSum(int[] nums) { return new ArrayList<>(res); } -} \ No newline at end of file +}