From 16d588fca9276c5deb2b508468cb3f8be354773d Mon Sep 17 00:00:00 2001 From: ys-han00 Date: Wed, 10 Dec 2025 21:45:04 +0900 Subject: [PATCH] #221 & #236 & #256 & #271 Solutions --- best-time-to-buy-and-sell-stock/ys-han00.cpp | 47 ++++++++++++ group-anagrams/ys-han00.cpp | 42 +++++++++++ implement-trie-prefix-tree/ys-han00.cpp | 54 ++++++++++++++ word-break/ys-han00.cpp | 78 ++++++++++++++++++++ 4 files changed, 221 insertions(+) create mode 100644 best-time-to-buy-and-sell-stock/ys-han00.cpp create mode 100644 group-anagrams/ys-han00.cpp create mode 100644 implement-trie-prefix-tree/ys-han00.cpp create mode 100644 word-break/ys-han00.cpp diff --git a/best-time-to-buy-and-sell-stock/ys-han00.cpp b/best-time-to-buy-and-sell-stock/ys-han00.cpp new file mode 100644 index 0000000000..e1f012f500 --- /dev/null +++ b/best-time-to-buy-and-sell-stock/ys-han00.cpp @@ -0,0 +1,47 @@ +// class Solution { +// public: +// int maxProfit(vector& prices) { +// int ans = 0; +// for(int i = 0; i < prices.size(); i++) +// for(int j = i + 1; j < prices.size(); j++) +// ans = max(prices[j] - prices[i], ans); + +// return ans; +// } +// }; + +// class Solution { +// public: +// int maxProfit(vector& prices) { +// int now = 1, l_idx = 0, r_idx = 0, ans = 0; + +// while(now < prices.size()) { +// if(prices[now] < prices[l_idx]) { +// ans = max(ans, prices[r_idx] - prices[l_idx]); +// l_idx = now; +// r_idx = now; +// continue; +// } +// if(prices[now] > prices[r_idx]) +// r_idx = now; +// now++; +// } +// ans = max(ans, prices[r_idx] - prices[l_idx]); +// return ans; +// } +// }; + +class Solution { +public: + int maxProfit(vector& prices) { + int buy = prices[0]; + int ans = 0; + for(int i = 1; i < prices.size(); i++) { + ans = max(ans, prices[i] - buy); + buy = min(buy, prices[i]); + } + + return ans; + } +}; + diff --git a/group-anagrams/ys-han00.cpp b/group-anagrams/ys-han00.cpp new file mode 100644 index 0000000000..4f5dcf0fbf --- /dev/null +++ b/group-anagrams/ys-han00.cpp @@ -0,0 +1,42 @@ +// class Solution { +// public: +// vector> groupAnagrams(vector& strs) { +// map> anagram; +// vector> ans; + +// for(int i = 0; i < strs.size(); i++) { +// string now = strs[i]; +// sort(now.begin(), now.end()); + +// if(anagram.find(now) == anagram.end()) +// anagram[now] = vector ({strs[i]}); +// else +// anagram[now].push_back(strs[i]); +// } + +// for(map>::iterator iter = anagram.begin(); iter != anagram.end(); iter++) +// ans.push_back(iter->second); + +// return ans; +// } +// }; + +class Solution { +public: + vector> groupAnagrams(vector& strs) { + unordered_map> mp; + vector> ans; + + for(string &s : strs) { + string key = s; + sort(key.begin(), key.end()); + mp[key].push_back(s); + } + + for(auto &element : mp) + ans.push_back(element.second); + + return ans; + } +}; + diff --git a/implement-trie-prefix-tree/ys-han00.cpp b/implement-trie-prefix-tree/ys-han00.cpp new file mode 100644 index 0000000000..2ec809f3cb --- /dev/null +++ b/implement-trie-prefix-tree/ys-han00.cpp @@ -0,0 +1,54 @@ +struct TrieNode { + unordered_map child; + bool isEnd; + TrieNode() : isEnd(false) {} +}; + +class Trie { +public: + TrieNode* root; + Trie() { + root = new TrieNode(); + } + + void insert(string word) { + TrieNode* cur = root; + for(char c : word) { + if(cur->child.find(c) == cur->child.end()) + cur->child[c] = new TrieNode(); + cur = cur->child[c]; + } + cur->isEnd = true; + } + + bool search(string word) { + TrieNode* cur = root; + int now = 0; + while(now < word.size()) { + if(cur->child.find(word[now]) == cur->child.end()) + return false; + cur = cur->child[word[now++]]; + } + return cur->isEnd; + } + + bool startsWith(string prefix) { + TrieNode* cur = root; + int now = 0; + while(now < prefix.size()) { + if(cur->child.find(prefix[now]) == cur->child.end()) + return false; + cur = cur->child[prefix[now++]]; + } + return true; + } +}; + +/** + * Your Trie object will be instantiated and called as such: + * Trie* obj = new Trie(); + * obj->insert(word); + * bool param_2 = obj->search(word); + * bool param_3 = obj->startsWith(prefix); + */ + diff --git a/word-break/ys-han00.cpp b/word-break/ys-han00.cpp new file mode 100644 index 0000000000..e55f829b90 --- /dev/null +++ b/word-break/ys-han00.cpp @@ -0,0 +1,78 @@ +// class Solution { +// public: +// bool ans = false; +// unordered_map check; +// void dfs(string s, vector& wordDict, int idx) { +// if(ans) return; +// if(idx == s.size()) { +// ans = true; +// return; +// } +// for(auto word : wordDict) { + +// if(!check[idx + word.size()] && idx + word.size() <= s.size() && s.substr(idx, word.size()) == word) { +// check[idx + word.size()] = true; +// dfs(s, wordDict, idx + word.size()); +// } +// } +// } + +// bool wordBreak(string s, vector& wordDict) { +// dfs(s, wordDict, 0); +// return ans; +// } +// }; + +// class Solution { +// public: +// bool dfs(const string &s, const vector &wordDict, int idx, vector &check) { +// if(idx == s.size()) return true; +// if(check[idx] != -1) return check[idx]; + +// for(const auto &word : wordDict) { +// int nextIdx = idx + word.size(); +// if(nextIdx > s.size()) continue; + +// bool matched = true; +// for(int i = 0; i < word.size(); i++) { +// if(s[idx + i] != word[i]) { +// matched = false; +// break; +// } +// } +// if(!matched) continue; + +// if(dfs(s, wordDict, nextIdx, check)) +// return check[idx] = 1; +// } +// return check[idx] = 0; +// } + +// bool wordBreak(string s, vector& wordDict) { +// vector check(s.size() + 1, -1); +// return dfs(s, wordDict, 0, check); +// } +// }; + +class Solution { +public: + bool wordBreak(string s, vector& wordDict) { + int n = s.size(), m; + vector dp(n + 1, false); + dp[0] = 1; + + for(int i = 1; i <= n; i++) { + for(string word : wordDict) { + m = word.size(); + if(i - m < 0) continue; + if(s.substr(i - m, m) == word) + dp[i] = dp[i - m]; + if(dp[i] == true) + break; + } + } + + return dp[n]; + } +}; +