Skip to content

Commit 16d588f

Browse files
committed
#221 & #236 & #256 & #271 Solutions
1 parent 5f080b5 commit 16d588f

File tree

4 files changed

+221
-0
lines changed

4 files changed

+221
-0
lines changed
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
// class Solution {
2+
// public:
3+
// int maxProfit(vector<int>& prices) {
4+
// int ans = 0;
5+
// for(int i = 0; i < prices.size(); i++)
6+
// for(int j = i + 1; j < prices.size(); j++)
7+
// ans = max(prices[j] - prices[i], ans);
8+
9+
// return ans;
10+
// }
11+
// };
12+
13+
// class Solution {
14+
// public:
15+
// int maxProfit(vector<int>& prices) {
16+
// int now = 1, l_idx = 0, r_idx = 0, ans = 0;
17+
18+
// while(now < prices.size()) {
19+
// if(prices[now] < prices[l_idx]) {
20+
// ans = max(ans, prices[r_idx] - prices[l_idx]);
21+
// l_idx = now;
22+
// r_idx = now;
23+
// continue;
24+
// }
25+
// if(prices[now] > prices[r_idx])
26+
// r_idx = now;
27+
// now++;
28+
// }
29+
// ans = max(ans, prices[r_idx] - prices[l_idx]);
30+
// return ans;
31+
// }
32+
// };
33+
34+
class Solution {
35+
public:
36+
int maxProfit(vector<int>& prices) {
37+
int buy = prices[0];
38+
int ans = 0;
39+
for(int i = 1; i < prices.size(); i++) {
40+
ans = max(ans, prices[i] - buy);
41+
buy = min(buy, prices[i]);
42+
}
43+
44+
return ans;
45+
}
46+
};
47+

group-anagrams/ys-han00.cpp

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
// class Solution {
2+
// public:
3+
// vector<vector<string>> groupAnagrams(vector<string>& strs) {
4+
// map<string, vector<string>> anagram;
5+
// vector<vector<string>> ans;
6+
7+
// for(int i = 0; i < strs.size(); i++) {
8+
// string now = strs[i];
9+
// sort(now.begin(), now.end());
10+
11+
// if(anagram.find(now) == anagram.end())
12+
// anagram[now] = vector<string> ({strs[i]});
13+
// else
14+
// anagram[now].push_back(strs[i]);
15+
// }
16+
17+
// for(map<string, vector<string>>::iterator iter = anagram.begin(); iter != anagram.end(); iter++)
18+
// ans.push_back(iter->second);
19+
20+
// return ans;
21+
// }
22+
// };
23+
24+
class Solution {
25+
public:
26+
vector<vector<string>> groupAnagrams(vector<string>& strs) {
27+
unordered_map<string, vector<string>> mp;
28+
vector<vector<string>> ans;
29+
30+
for(string &s : strs) {
31+
string key = s;
32+
sort(key.begin(), key.end());
33+
mp[key].push_back(s);
34+
}
35+
36+
for(auto &element : mp)
37+
ans.push_back(element.second);
38+
39+
return ans;
40+
}
41+
};
42+
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
struct TrieNode {
2+
unordered_map<char, TrieNode*> child;
3+
bool isEnd;
4+
TrieNode() : isEnd(false) {}
5+
};
6+
7+
class Trie {
8+
public:
9+
TrieNode* root;
10+
Trie() {
11+
root = new TrieNode();
12+
}
13+
14+
void insert(string word) {
15+
TrieNode* cur = root;
16+
for(char c : word) {
17+
if(cur->child.find(c) == cur->child.end())
18+
cur->child[c] = new TrieNode();
19+
cur = cur->child[c];
20+
}
21+
cur->isEnd = true;
22+
}
23+
24+
bool search(string word) {
25+
TrieNode* cur = root;
26+
int now = 0;
27+
while(now < word.size()) {
28+
if(cur->child.find(word[now]) == cur->child.end())
29+
return false;
30+
cur = cur->child[word[now++]];
31+
}
32+
return cur->isEnd;
33+
}
34+
35+
bool startsWith(string prefix) {
36+
TrieNode* cur = root;
37+
int now = 0;
38+
while(now < prefix.size()) {
39+
if(cur->child.find(prefix[now]) == cur->child.end())
40+
return false;
41+
cur = cur->child[prefix[now++]];
42+
}
43+
return true;
44+
}
45+
};
46+
47+
/**
48+
* Your Trie object will be instantiated and called as such:
49+
* Trie* obj = new Trie();
50+
* obj->insert(word);
51+
* bool param_2 = obj->search(word);
52+
* bool param_3 = obj->startsWith(prefix);
53+
*/
54+

word-break/ys-han00.cpp

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
// class Solution {
2+
// public:
3+
// bool ans = false;
4+
// unordered_map<int, bool> check;
5+
// void dfs(string s, vector<string>& wordDict, int idx) {
6+
// if(ans) return;
7+
// if(idx == s.size()) {
8+
// ans = true;
9+
// return;
10+
// }
11+
// for(auto word : wordDict) {
12+
13+
// if(!check[idx + word.size()] && idx + word.size() <= s.size() && s.substr(idx, word.size()) == word) {
14+
// check[idx + word.size()] = true;
15+
// dfs(s, wordDict, idx + word.size());
16+
// }
17+
// }
18+
// }
19+
20+
// bool wordBreak(string s, vector<string>& wordDict) {
21+
// dfs(s, wordDict, 0);
22+
// return ans;
23+
// }
24+
// };
25+
26+
// class Solution {
27+
// public:
28+
// bool dfs(const string &s, const vector<string> &wordDict, int idx, vector<int> &check) {
29+
// if(idx == s.size()) return true;
30+
// if(check[idx] != -1) return check[idx];
31+
32+
// for(const auto &word : wordDict) {
33+
// int nextIdx = idx + word.size();
34+
// if(nextIdx > s.size()) continue;
35+
36+
// bool matched = true;
37+
// for(int i = 0; i < word.size(); i++) {
38+
// if(s[idx + i] != word[i]) {
39+
// matched = false;
40+
// break;
41+
// }
42+
// }
43+
// if(!matched) continue;
44+
45+
// if(dfs(s, wordDict, nextIdx, check))
46+
// return check[idx] = 1;
47+
// }
48+
// return check[idx] = 0;
49+
// }
50+
51+
// bool wordBreak(string s, vector<string>& wordDict) {
52+
// vector<int> check(s.size() + 1, -1);
53+
// return dfs(s, wordDict, 0, check);
54+
// }
55+
// };
56+
57+
class Solution {
58+
public:
59+
bool wordBreak(string s, vector<string>& wordDict) {
60+
int n = s.size(), m;
61+
vector<bool> dp(n + 1, false);
62+
dp[0] = 1;
63+
64+
for(int i = 1; i <= n; i++) {
65+
for(string word : wordDict) {
66+
m = word.size();
67+
if(i - m < 0) continue;
68+
if(s.substr(i - m, m) == word)
69+
dp[i] = dp[i - m];
70+
if(dp[i] == true)
71+
break;
72+
}
73+
}
74+
75+
return dp[n];
76+
}
77+
};
78+

0 commit comments

Comments
 (0)