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/encode-and-decode-strings/YoungSeok-Choi.java b/encode-and-decode-strings/YoungSeok-Choi.java new file mode 100644 index 000000000..6a1925602 --- /dev/null +++ b/encode-and-decode-strings/YoungSeok-Choi.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; + } +} diff --git a/group-anagrams/YoungSeok-Choi.java b/group-anagrams/YoungSeok-Choi.java new file mode 100644 index 000000000..8ede468fc --- /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; + } +} 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); + */ 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; + } +}