diff --git a/best-time-to-buy-and-sell-stock/hu6r1s.py b/best-time-to-buy-and-sell-stock/hu6r1s.py new file mode 100644 index 000000000..f59ef5309 --- /dev/null +++ b/best-time-to-buy-and-sell-stock/hu6r1s.py @@ -0,0 +1,25 @@ +class Solution: + """ + 1. 브루트포스 + 2중 for문이라서 O(n^2)임. + prices의 길이가 10^5이므로 10^10이 되면서 시간초과가 발생 + + 2. 이분탐색으로 가능할까 했지만 DP를 사용해야 하는 문제 같음 + 시간복잡도는 O(n)이 나옴 + """ + """ + def maxProfit(self, prices: List[int]) -> int: + max_profit = 0 + for i in range(len(prices)-1): + for j in range(i, len(prices)): + profit = prices[j] - prices[i] + max_profit = max(max_profit, profit) + return max_profit + """ + def maxProfit(self, prices: List[int]) -> int: + max_profit = 0 + min_price = prices[0] + for price in prices: + max_profit = max(max_profit, price - min_price) + min_price = min(price, min_price) + return max_profit diff --git a/encode-and-decode-strings/hu6r1s.py b/encode-and-decode-strings/hu6r1s.py new file mode 100644 index 000000000..03454ce19 --- /dev/null +++ b/encode-and-decode-strings/hu6r1s.py @@ -0,0 +1,14 @@ +class Solution: + """ + @param: strs: a list of strings + @return: encodes a list of strings to a single string. + """ + def encode(self, strs): + return "secretKey!@#".join(strs) + + """ + @param: str: A string + @return: decodes a single string to a list of strings + """ + def decode(self, str): + return str.split("secretKey!@#") diff --git a/group-anagrams/hu6r1s.py b/group-anagrams/hu6r1s.py new file mode 100644 index 000000000..266a73e85 --- /dev/null +++ b/group-anagrams/hu6r1s.py @@ -0,0 +1,12 @@ +from collections import defaultdict + +class Solution: + """ + 1. 정렬된 값이 딕셔너리에 있으면 리스트 형식으로 삽입 + """ + def groupAnagrams(self, strs: List[str]) -> List[List[str]]: + dict_strs = defaultdict(list) + + for word in strs: + dict_strs[str(sorted(word))].append(word) + return list(dict_strs.values()) diff --git a/implement-trie-prefix-tree/hu6r1s.py b/implement-trie-prefix-tree/hu6r1s.py new file mode 100644 index 000000000..1c3c7feaa --- /dev/null +++ b/implement-trie-prefix-tree/hu6r1s.py @@ -0,0 +1,35 @@ +class Trie: + + def __init__(self): + self.root = {"$": True} + + def insert(self, word: str) -> None: + node = self.root + for ch in word: + if ch not in node: + node[ch] = {"$": False} + node = node[ch] + node["$"] = True + + def search(self, word: str) -> bool: + node = self.root + for ch in word: + if ch not in node: + return False + node = node[ch] + return node["$"] + + def startsWith(self, prefix: str) -> bool: + node = self.root + for ch in prefix: + if ch not in node: + return False + node = node[ch] + return True + + +# Your Trie object will be instantiated and called as such: +# obj = Trie() +# obj.insert(word) +# param_2 = obj.search(word) +# param_3 = obj.startsWith(prefix) diff --git a/word-break/hu6r1s.java b/word-break/hu6r1s.java new file mode 100644 index 000000000..dc0a0d883 --- /dev/null +++ b/word-break/hu6r1s.java @@ -0,0 +1,19 @@ +import java.util.*; + +class Solution { + public boolean wordBreak(String s, List wordDict) { + Set wordSet = new HashSet<>(wordDict); + boolean[] dp = new boolean[s.length() + 1]; + dp[0] = true; + + for (int i = 1; i < s.length() + 1; i++) { + for (int j = 0; j < i; j++) { + if (dp[j] && (wordSet.contains(s.substring(j, i)))) { + dp[i] = true; + break; + } + } + } + return dp[s.length()]; + } +} diff --git a/word-break/hu6r1s.py b/word-break/hu6r1s.py new file mode 100644 index 000000000..8eae51278 --- /dev/null +++ b/word-break/hu6r1s.py @@ -0,0 +1,14 @@ +class Solution: + def wordBreak(self, s: str, wordDict: List[str]) -> bool: + word_set = set(wordDict) # O(1) 조회를 위해 set으로 변환 + n = len(s) + dp = [False] * (n + 1) + dp[0] = True # 공집합은 항상 가능 + + for i in range(1, n + 1): + for j in range(i): + if dp[j] and s[j:i] in word_set: + dp[i] = True + break # i번째까지 나눌 수 있으면 더 확인할 필요 없음 + + return dp[-1]