From eb177e6cd29b98cb4444a1f6da9ae59f7e9e545c Mon Sep 17 00:00:00 2001 From: printjin Date: Sun, 27 Apr 2025 10:09:34 +0900 Subject: [PATCH 1/7] Solve : Best Time to Buy And Sell Stock --- best-time-to-buy-and-sell-stock/printjin-gmailcom.py | 10 ++++++++++ 1 file changed, 10 insertions(+) create mode 100644 best-time-to-buy-and-sell-stock/printjin-gmailcom.py diff --git a/best-time-to-buy-and-sell-stock/printjin-gmailcom.py b/best-time-to-buy-and-sell-stock/printjin-gmailcom.py new file mode 100644 index 000000000..fa225ef62 --- /dev/null +++ b/best-time-to-buy-and-sell-stock/printjin-gmailcom.py @@ -0,0 +1,10 @@ +class Solution: + def maxProfit(self, price): + min_price = float('inf') + max_profit = 0 + for price in prices: + if price < min_price: + min_price = price + elif price - min_price > max_profit: + max_profit = price - min_price + return max_profit From 92240cdf42aa2cffd69d2fe163000f7f1ae6e4dd Mon Sep 17 00:00:00 2001 From: printjin Date: Sun, 27 Apr 2025 10:20:39 +0900 Subject: [PATCH 2/7] Solve : Group Anagrams --- group-anagrams/printjin-gmailcom.py | 9 +++++++++ 1 file changed, 9 insertions(+) create mode 100644 group-anagrams/printjin-gmailcom.py diff --git a/group-anagrams/printjin-gmailcom.py b/group-anagrams/printjin-gmailcom.py new file mode 100644 index 000000000..cb4c934e8 --- /dev/null +++ b/group-anagrams/printjin-gmailcom.py @@ -0,0 +1,9 @@ +from collections import defaultdict + +class Solution: + def groupAnagrams(self, strs): + anagrams = defaultdict(list) + for word in strs: + key = ''.join(sorted(word)) + anagrams[key].append(word) + return list(anagrams.values()) From 649197afbc4f5581b850c0eaa42503a4d3a6bac9 Mon Sep 17 00:00:00 2001 From: printjin Date: Sun, 27 Apr 2025 10:23:23 +0900 Subject: [PATCH 3/7] Solve : Group Anagram 2 --- encode-and-decode-strings/printjin-gmailcom.py | 0 group-anagrams/printjin-gmailcom.py | 6 ++++-- 2 files changed, 4 insertions(+), 2 deletions(-) create mode 100644 encode-and-decode-strings/printjin-gmailcom.py diff --git a/encode-and-decode-strings/printjin-gmailcom.py b/encode-and-decode-strings/printjin-gmailcom.py new file mode 100644 index 000000000..e69de29bb diff --git a/group-anagrams/printjin-gmailcom.py b/group-anagrams/printjin-gmailcom.py index cb4c934e8..ffcc1c2f8 100644 --- a/group-anagrams/printjin-gmailcom.py +++ b/group-anagrams/printjin-gmailcom.py @@ -4,6 +4,8 @@ class Solution: def groupAnagrams(self, strs): anagrams = defaultdict(list) for word in strs: - key = ''.join(sorted(word)) - anagrams[key].append(word) + count = [0] * 26 + for c in word: + count[ord(c) - ord('a')] += 1 + anagrams[tuple(count)].append(word) return list(anagrams.values()) From 9bdaebb3ca0af8ac3d8ebf5140b0caff1297e4eb Mon Sep 17 00:00:00 2001 From: printjin Date: Sun, 27 Apr 2025 10:24:34 +0900 Subject: [PATCH 4/7] Solve : Encode and Decode Strings --- encode-and-decode-strings/printjin-gmailcom.py | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/encode-and-decode-strings/printjin-gmailcom.py b/encode-and-decode-strings/printjin-gmailcom.py index e69de29bb..06957083a 100644 --- a/encode-and-decode-strings/printjin-gmailcom.py +++ b/encode-and-decode-strings/printjin-gmailcom.py @@ -0,0 +1,18 @@ +class Solution: + def encode(self, strs): + res = '' + for s in strs: + res += str(len(s)) + '#' + s + return res + + def decode(self, s): + res = [] + i = 0 + while i < len(s): + j = i + while s[j] != '#': + j += 1 + length = int(s[i:j]) + res.append(s[j+1:j+1+length]) + i = j + 1 + length + return res From 13aa8e6a6f7bceb4d9d878dd86715837dc5f41ba Mon Sep 17 00:00:00 2001 From: printjin Date: Sun, 27 Apr 2025 10:33:24 +0900 Subject: [PATCH 5/7] Solve : Implement Trie Prefix Tree --- .../printjin-gmailcom.py | 27 +++++++++++++++++++ 1 file changed, 27 insertions(+) create mode 100644 implement-trie-prefix-tree/printjin-gmailcom.py diff --git a/implement-trie-prefix-tree/printjin-gmailcom.py b/implement-trie-prefix-tree/printjin-gmailcom.py new file mode 100644 index 000000000..a135f04ac --- /dev/null +++ b/implement-trie-prefix-tree/printjin-gmailcom.py @@ -0,0 +1,27 @@ +class Trie: + def __init__(self): + self.trie = {} + + def insert(self, word): + node = self.trie + for char in word: + if char not in node: + node[char] = {} + node = node[char] + node['#'] = {} + + def search(self, word): + node = self.trie + for char in word: + if char not in node: + return False + node = node[char] + return '#' in node + + def startsWith(self, prefix): + node = self.trie + for char in prefix: + if char not in node: + return False + node = node[char] + return True From 1384fbf63a382354074eb306ba050d82f3478edc Mon Sep 17 00:00:00 2001 From: printjin Date: Sun, 27 Apr 2025 10:40:57 +0900 Subject: [PATCH 6/7] Solve : Word Break --- word-break/printjin-gmailcom.py | 13 +++++++++++++ 1 file changed, 13 insertions(+) create mode 100644 word-break/printjin-gmailcom.py diff --git a/word-break/printjin-gmailcom.py b/word-break/printjin-gmailcom.py new file mode 100644 index 000000000..70ccf4445 --- /dev/null +++ b/word-break/printjin-gmailcom.py @@ -0,0 +1,13 @@ +class Solution: + def wordBreak(self, s, wordDict): + wordSet = set(wordDict) + dp = [False] * (len(s) + 1) + dp[0] = True + + for i in range(1, len(s) + 1): + for j in range(i): + if dp[j] and s[j:i] in wordSet: + dp[i] = True + break + + return dp[len(s)] From 8d49797669230d1a358096c4621b22a9bc548266 Mon Sep 17 00:00:00 2001 From: printjin Date: Sun, 27 Apr 2025 10:42:58 +0900 Subject: [PATCH 7/7] Solve : Word Break 2 --- word-break/printjin-gmailcom.py | 23 +++++++++++++++-------- 1 file changed, 15 insertions(+), 8 deletions(-) diff --git a/word-break/printjin-gmailcom.py b/word-break/printjin-gmailcom.py index 70ccf4445..b882e0a09 100644 --- a/word-break/printjin-gmailcom.py +++ b/word-break/printjin-gmailcom.py @@ -1,13 +1,20 @@ class Solution: def wordBreak(self, s, wordDict): wordSet = set(wordDict) - dp = [False] * (len(s) + 1) - dp[0] = True + memo = {} - for i in range(1, len(s) + 1): - for j in range(i): - if dp[j] and s[j:i] in wordSet: - dp[i] = True - break + def backtrack(start): + if start == len(s): + return True + if start in memo: + return memo[start] + + for end in range(start + 1, len(s) + 1): + if s[start:end] in wordSet and backtrack(end): + memo[start] = True + return True + + memo[start] = False + return False - return dp[len(s)] + return backtrack(0)