diff --git a/best-time-to-buy-and-sell-stock/sangbeenmoon.py b/best-time-to-buy-and-sell-stock/sangbeenmoon.py new file mode 100644 index 0000000000..2fe1894930 --- /dev/null +++ b/best-time-to-buy-and-sell-stock/sangbeenmoon.py @@ -0,0 +1,13 @@ +# TC : O(n) +# SC : O(1) + +class Solution: + def maxProfit(self, prices: List[int]) -> int: + answer = 0 + mm = 10001 + + for price in prices: + mm = min(mm, price) + answer = max(answer, price - mm) + + return answer diff --git a/encode-and-decode-strings/sangbeenmoon.py b/encode-and-decode-strings/sangbeenmoon.py new file mode 100644 index 0000000000..7478d851e0 --- /dev/null +++ b/encode-and-decode-strings/sangbeenmoon.py @@ -0,0 +1,43 @@ +class Solution: + """ + @param: strs: a list of strings + @return: encodes a list of strings to a single string. + """ + def encode(self, strs): + # write your code here + result = "" + + delimiter = ":;" + + for i, str in enumerate(strs): + result = result + str + if i != len(strs): + result = result + delimiter + + print(result) + + return result + + """ + @param: str: A string + @return: decodes a single string to a list of strings + """ + def decode(self, str): + result = [] + + i = 0 + delimiter = ":;" + + while i < len(str): + + j = i + while j < len(str) - 1: + if str[j:j+2] == delimiter: + result.append(str[i:j]) + break + else: + j = j + 1 + + i = j + 2 + + return result diff --git a/group-anagrams/sangbeenmoon.py b/group-anagrams/sangbeenmoon.py new file mode 100644 index 0000000000..72a260e00a --- /dev/null +++ b/group-anagrams/sangbeenmoon.py @@ -0,0 +1,35 @@ +# TC : O(m^2 * n) m : len(strs), n : len(strs[0]) +# SC : O(m * n) + +class Solution: + def groupAnagrams(self, strs: List[str]) -> List[List[str]]: + sorted_strs = [] + for str in strs: + sorted_strs.append(sorted(str)) + + answer = [] + + i = 0 + + visited = [False] * 10001 + + while i < len(strs): + if visited[i]: + i = i + 1 + continue + + sub_answer = [] + target = sorted_strs[i] + sub_answer.append(strs[i]) + + for j in range(i+1, len(strs)): + if sorted_strs[j] == target: + visited[j] = True + sub_answer.append(strs[j]) + + i = i + 1 + answer.append(sub_answer) + + return answer + + diff --git a/implement-trie-prefix-tree/sangbeenmoon.java b/implement-trie-prefix-tree/sangbeenmoon.java new file mode 100644 index 0000000000..67c598f6b3 --- /dev/null +++ b/implement-trie-prefix-tree/sangbeenmoon.java @@ -0,0 +1,57 @@ +import java.util.HashMap; +import java.util.Map; + +class Trie { + + Map map; + boolean isEnd = false; + + public Trie() { + map = new HashMap<>(); + } + + public void insert(String word) { + Trie trie; + if (map.containsKey(word.charAt(0))) { + trie = map.get(word.charAt(0)); + } else { + trie = new Trie(); + } + map.put(word.charAt(0), trie); + + if (word.length() == 1) { + map.get(word.charAt(0)).isEnd = true; + return; + } + + trie.insert(word.substring(1)); + } + + public boolean search(String word) { + if (map.containsKey(word.charAt(0))) { + if (word.length() == 1) { + return map.get(word.charAt(0)).isEnd; + } + return map.get(word.charAt(0)).search(word.substring(1)); + } + return false; + } + + public boolean startsWith(String prefix) { + if (map.containsKey(prefix.charAt(0))) { + if (prefix.length() == 1) { + return true; + } + return map.get(prefix.charAt(0)).startsWith(prefix.substring(1)); + } + 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/sangbeenmoon.py b/word-break/sangbeenmoon.py new file mode 100644 index 0000000000..d580913361 --- /dev/null +++ b/word-break/sangbeenmoon.py @@ -0,0 +1,29 @@ +# 실패한 풀이. +# AC 를 받기는 했으나 test case 가 좀 더 촘촘했다면 TLE 에 걸렸을 것임. +# string 이 아닌 index 로 memoization 을 하는 걸 떠올려보자. + +class Solution: + answer = False + visited = {} + def wordBreak(self, s: str, wordDict: List[str]) -> bool: + + self.answer = False + self.visited = {} + self.go(0,s,wordDict) + + return self.answer + + def go(self, i:int, s: str, wordDict: List[str]): + if i >= (len(s)): + self.answer = True + return + + for word in wordDict: + if i + len(word) > len(s): + continue + + if word == s[i:i + len(word)]: + if not s[i + len(word) : ] in self.visited: + self.go(i + len(word), s, wordDict) + + self.visited[s[i:]] = False