Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 47 additions & 0 deletions best-time-to-buy-and-sell-stock/ys-han00.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
// class Solution {
// public:
// int maxProfit(vector<int>& 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<int>& 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<int>& 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;
}
};

42 changes: 42 additions & 0 deletions group-anagrams/ys-han00.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
// class Solution {
// public:
// vector<vector<string>> groupAnagrams(vector<string>& strs) {
// map<string, vector<string>> anagram;
// vector<vector<string>> 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<string> ({strs[i]});
// else
// anagram[now].push_back(strs[i]);
// }

// for(map<string, vector<string>>::iterator iter = anagram.begin(); iter != anagram.end(); iter++)
// ans.push_back(iter->second);

// return ans;
// }
// };

class Solution {
public:
vector<vector<string>> groupAnagrams(vector<string>& strs) {
unordered_map<string, vector<string>> mp;
vector<vector<string>> 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;
}
};

54 changes: 54 additions & 0 deletions implement-trie-prefix-tree/ys-han00.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
struct TrieNode {
unordered_map<char, TrieNode*> 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);
*/

78 changes: 78 additions & 0 deletions word-break/ys-han00.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
// class Solution {
// public:
// bool ans = false;
// unordered_map<int, bool> check;
// void dfs(string s, vector<string>& 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<string>& wordDict) {
// dfs(s, wordDict, 0);
// return ans;
// }
// };

// class Solution {
// public:
// bool dfs(const string &s, const vector<string> &wordDict, int idx, vector<int> &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<string>& wordDict) {
// vector<int> check(s.size() + 1, -1);
// return dfs(s, wordDict, 0, check);
// }
// };

class Solution {
public:
bool wordBreak(string s, vector<string>& wordDict) {
int n = s.size(), m;
vector<bool> 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];
}
};