From e83c01dd717378ec29b1a43be51736dd90ad78aa Mon Sep 17 00:00:00 2001 From: "Yongseok.choi" Date: Mon, 28 Apr 2025 07:24:45 +0900 Subject: [PATCH 1/6] =?UTF-8?q?add:=20=EC=A3=BC=EC=8B=9D=20=EC=82=AC?= =?UTF-8?q?=EA=B3=A0=ED=8C=94=EA=B8=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../YoungSeok-Choi.java | 24 +++++++++++++ group-anagrams/YoungSeok-Choi.java | 34 +++++++++++++++++++ 2 files changed, 58 insertions(+) create mode 100644 best-time-to-buy-and-sell-stock/YoungSeok-Choi.java create mode 100644 group-anagrams/YoungSeok-Choi.java diff --git a/best-time-to-buy-and-sell-stock/YoungSeok-Choi.java b/best-time-to-buy-and-sell-stock/YoungSeok-Choi.java new file mode 100644 index 000000000..2e70e2689 --- /dev/null +++ b/best-time-to-buy-and-sell-stock/YoungSeok-Choi.java @@ -0,0 +1,24 @@ +// NOTE: tc --> O(n) +class Solution { + public int maxProfit(int[] prices) { + + int curMax = 0; + int gMax = 0; + + if(prices.length == 0) return 0; + + int sell = prices[0]; + for(int i = 1; i < prices.length; i++) { + curMax = Math.max(0, prices[i] - sell); + + // NOTE: 새롭게 시작하는게 더 좋은경우 + if(curMax == 0) { + sell = prices[i]; + } + + gMax = Math.max(curMax, gMax); + } + + return gMax; + } +} diff --git a/group-anagrams/YoungSeok-Choi.java b/group-anagrams/YoungSeok-Choi.java new file mode 100644 index 000000000..3d0b85a7b --- /dev/null +++ b/group-anagrams/YoungSeok-Choi.java @@ -0,0 +1,34 @@ +import java.util.ArrayList; +import java.util.Arrays; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +// NOTE: tc -> O(n) +class Solution { + public List> groupAnagrams(String[] strs) { + + List> result = new ArrayList<>(); + Map> sMap = new HashMap<>(); + + for(int i = 0; i < strs.length; i++) { + char[] cArr = strs[i].toCharArray(); + Arrays.sort(cArr); + String sorted = new String(cArr); + + if(sMap.containsKey(sorted)) { + sMap.get(sorted).add(strs[i]); + } else { + List temp = new ArrayList<>(); + temp.add(strs[i]); + sMap.put(sorted, temp); + } + } + + for(List arr : sMap.values()) { + result.add(arr); + } + + return result; + } +} \ No newline at end of file From 16d26091aa93be827cde17eec84f656266c1a4dc Mon Sep 17 00:00:00 2001 From: "Yongseok.choi" Date: Mon, 28 Apr 2025 07:27:30 +0900 Subject: [PATCH 2/6] fix: lint --- group-anagrams/YoungSeok-Choi.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/group-anagrams/YoungSeok-Choi.java b/group-anagrams/YoungSeok-Choi.java index 3d0b85a7b..8ede468fc 100644 --- a/group-anagrams/YoungSeok-Choi.java +++ b/group-anagrams/YoungSeok-Choi.java @@ -31,4 +31,4 @@ public List> groupAnagrams(String[] strs) { return result; } -} \ No newline at end of file +} From 384c79cd29daa5cccd21a7f8dda406a2e6477610 Mon Sep 17 00:00:00 2001 From: "Yongseok.choi" Date: Mon, 28 Apr 2025 08:00:27 +0900 Subject: [PATCH 3/6] add: encode and decode --- encode-and-decode-strings/Solution.java | 45 +++++++++++++++++++++++++ 1 file changed, 45 insertions(+) create mode 100644 encode-and-decode-strings/Solution.java diff --git a/encode-and-decode-strings/Solution.java b/encode-and-decode-strings/Solution.java new file mode 100644 index 000000000..6a1925602 --- /dev/null +++ b/encode-and-decode-strings/Solution.java @@ -0,0 +1,45 @@ +import java.util.ArrayList; +import java.util.List; + +public class Solution { + /* + * @param strs: a list of strings + * @return: encodes a list of strings to a single string. + */ + public String encode(List strs) { + List temp = new ArrayList<>(); + + if(strs.size() == 0) return null; + + for(String s : strs) { + if(":".equals(s)) { + temp.add("::"); + } else { + temp.add(s); + } + } + + return String.join(":;", temp); + } + + /* + * @param str: A string + * @return: decodes a single string to a list of strings + */ + public List decode(String str) { + List temp = new ArrayList<>(); + + if(str == null) return new ArrayList<>(); + + // if(str.length() == 0) return new ArrayList<>(); + + for(String s : str.split(":;")) { + if("::".equals(s)) { + temp.add(":"); + } else { + temp.add(s); + } + } + return temp; + } +} From 513f868af977f4c1f0a79841b204955837d2d624 Mon Sep 17 00:00:00 2001 From: "Yongseok.choi" Date: Mon, 28 Apr 2025 08:03:59 +0900 Subject: [PATCH 4/6] fix: lint --- encode-and-decode-strings/{Solution.java => YoungSeok-Choi.java} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename encode-and-decode-strings/{Solution.java => YoungSeok-Choi.java} (100%) diff --git a/encode-and-decode-strings/Solution.java b/encode-and-decode-strings/YoungSeok-Choi.java similarity index 100% rename from encode-and-decode-strings/Solution.java rename to encode-and-decode-strings/YoungSeok-Choi.java From e2402bde6bb5552cda92358b1cb4b381aa7e0b5d Mon Sep 17 00:00:00 2001 From: "Yongseok.choi" Date: Thu, 1 May 2025 11:16:19 +0900 Subject: [PATCH 5/6] add: word break; --- word-break/YoungSeok-Choi.java | 80 ++++++++++++++++++++++++++++++++++ 1 file changed, 80 insertions(+) create mode 100644 word-break/YoungSeok-Choi.java diff --git a/word-break/YoungSeok-Choi.java b/word-break/YoungSeok-Choi.java new file mode 100644 index 000000000..22f98abe8 --- /dev/null +++ b/word-break/YoungSeok-Choi.java @@ -0,0 +1,80 @@ +import java.util.HashMap; +import java.util.HashSet; +import java.util.List; +import java.util.Map; +import java.util.Set; + +// DFS 완전탐색할 때 불필요한 탐색을 줄이는 방법을 항상 고민할 것. +class Solution { + public int size = 0; + public Map failedMap = new HashMap<>(); + public boolean wordBreak(String s, List wordDict) { + size = wordDict.size(); + + Set sSet = new HashSet<>(); + for (char c : s.toCharArray()) { + sSet.add(c); + } + + Set wSet = new HashSet<>(); + for (char c : String.join("", wordDict).toCharArray()) { + wSet.add(c); + } + + if(sSet.size() > wSet.size()) { + return false; + } + + return dfs(s, wordDict); + } + + public boolean dfs(String s, List wordDict) { + if(s.length() == 0) return true; + if(failedMap.containsKey(s)) return false; + + for(int i = 0; i < size; i++) { + String word = wordDict.get(i); + if(s.startsWith(word)) { + + s = s.substring(word.length()); + boolean result = dfs(s, wordDict); + + if(result) { + return true; + } else { + failedMap.put(s, true); + } + + s = word + s; + } + } + + return false; + } +} + +// 특정 문자로 시작되는 것만 판단하여 반복해 풀려고 했던 접근법. +// 전체 조합을 보아야 하는 "cars", ["cars", "ca", "rs"] 경우에 반례가 됨. +class WrongSolution { + public boolean wordBreak(String s, List wordDict) { + int size = wordDict.size(); + + while(true) { + boolean isMatched = false; + for(int i = 0; i < size; i++) { + String word = wordDict.get(i); + if(s.startsWith(word)) { + isMatched = true; + s = s.substring(word.length()); + break; + } + } + + if(!isMatched) { + break; + } + } + + return s.length() == 0; + } +} From c86940f3b73191ee8e6fa2735d35bdc52399da91 Mon Sep 17 00:00:00 2001 From: "Yongseok.choi" Date: Thu, 1 May 2025 11:47:45 +0900 Subject: [PATCH 6/6] add: implement-trie-prefix-tree --- .../YoungSeok-Choi.java | 37 +++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100644 implement-trie-prefix-tree/YoungSeok-Choi.java diff --git a/implement-trie-prefix-tree/YoungSeok-Choi.java b/implement-trie-prefix-tree/YoungSeok-Choi.java new file mode 100644 index 000000000..54e2dfcc9 --- /dev/null +++ b/implement-trie-prefix-tree/YoungSeok-Choi.java @@ -0,0 +1,37 @@ +import java.util.HashMap; +import java.util.Map; + +// Map으로 풀려버려서 당황.. +// 이진트리? 어떤식으로 풀어야 할지 자료구조 정하고 다시 풀어보기.. +class Trie { + + Map tMap; + + public Trie() { + this.tMap = new HashMap<>(); + } + + public void insert(String word) { + this.tMap.put(word, true); + } + + public boolean search(String word) { + return this.tMap.containsKey(word); + } + + public boolean startsWith(String prefix) { + for(String key : this.tMap.keySet()) { + if(key.startsWith(prefix)) return true; + } + + return false; + } +} + +/** + * Your Trie object will be instantiated and called as such: + * Trie obj = new Trie(); + * obj.insert(word); + * boolean param_2 = obj.search(word); + * boolean param_3 = obj.startsWith(prefix); + */