Skip to content

Commit 841476f

Browse files
committed
#222 & #242 & #257 & #272 & #282 Solutions
1 parent 70c9b19 commit 841476f

File tree

5 files changed

+234
-0
lines changed

5 files changed

+234
-0
lines changed
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
// class Solution {
2+
// public:
3+
// int maxArea(vector<int>& height) {
4+
// int n = height.size();
5+
// pair<int, int> left_wall = {0, height[0]};
6+
// int maxi = -1;
7+
// vector<int> dp(n, 0);
8+
9+
// for(int i = 1; i < n; i++) {
10+
// for(int j = 0; j < i; j++) {
11+
// dp[i] = max(dp[i], min(height[i], height[j]) * (i - j));
12+
// if(height[j] > height[i])
13+
// break;
14+
// }
15+
// maxi = max(maxi, dp[i]);
16+
// }
17+
18+
// return maxi;
19+
// }
20+
// };
21+
22+
class Solution {
23+
public:
24+
int maxArea(vector<int>& height) {
25+
int left = 0, right = height.size() - 1;
26+
int ans = -1;
27+
28+
while(left != right) {
29+
ans = max(ans, (right - left) * min(height[left], height[right]));
30+
if(height[left] < height[right])
31+
left++;
32+
else
33+
right--;
34+
}
35+
36+
return ans;
37+
}
38+
};
39+
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
struct TrieNode {
2+
unordered_map<char, TrieNode*> child;
3+
bool isEnd;
4+
TrieNode() : isEnd(false) {}
5+
};
6+
7+
class WordDictionary {
8+
public:
9+
TrieNode* root;
10+
WordDictionary() {
11+
root = new TrieNode();
12+
}
13+
14+
void addWord(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+
bool find = false;
26+
stack<pair<int, TrieNode*>> sta;
27+
28+
sta.push({0, root});
29+
while(!sta.empty()) {
30+
int now = sta.top().first;
31+
TrieNode* cur = sta.top().second;
32+
sta.pop();
33+
34+
if(now == word.size() && cur->isEnd)
35+
return true;
36+
37+
if(word[now] == '.') {
38+
for(auto mp : cur->child)
39+
sta.push({now + 1, mp.second});
40+
} else {
41+
if(cur->child.find(word[now]) != cur->child.end())
42+
sta.push({now + 1, cur->child[word[now]]});
43+
}
44+
}
45+
46+
return false;
47+
}
48+
};
49+
50+
/**
51+
* Your WordDictionary object will be instantiated and called as such:
52+
* WordDictionary* obj = new WordDictionary();
53+
* obj->addWord(word);
54+
* bool param_2 = obj->search(word);
55+
*/
56+
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
// class Solution {
2+
// public:
3+
// int lengthOfLIS(vector<int>& nums) {
4+
// int n = nums.size(), maxi, ans = 1;
5+
// vector<int> dp(n, 0);
6+
7+
// dp[0] = 1;
8+
// for(int i = 1; i < n; i++) {
9+
// maxi = 0;
10+
// for(int j = 0; j < i; j++)
11+
// if(nums[j] < nums[i] && maxi < dp[j])
12+
// maxi = dp[j];
13+
// dp[i] = maxi + 1;
14+
// ans = max(dp[i] , ans);
15+
// }
16+
17+
// return ans;
18+
// }
19+
// };
20+
21+
class Solution {
22+
public:
23+
int lengthOfLIS(vector<int>& nums) {
24+
vector<int> sub;
25+
26+
for(int num : nums) {
27+
auto it = lower_bound(sub.begin(), sub.end(), num);
28+
int idx = it - sub.begin();
29+
if(idx == sub.size())
30+
sub.push_back(num);
31+
else
32+
sub[idx] = num;
33+
}
34+
35+
return sub.size();
36+
}
37+
};
38+

spiral-matrix/ys-han00.cpp

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
// class Solution {
2+
// public:
3+
// vector<int> spiralOrder(vector<vector<int>>& matrix) {
4+
// int n = matrix.size(), m = matrix[0].size();
5+
// vector<int> sorted;
6+
// vector<vector<bool>> check(n, vector<bool>(m, false));
7+
// queue<pair<int, pair<int, int>>> que;
8+
9+
// int dx[4] = {-1, 0, 1, 0};
10+
// int dy[4] = {0, 1, 0, -1};
11+
// // 0: up, 1: right, 2: down, 3: left
12+
// que.push({1, {0, 0}});
13+
// sorted.push_back(matrix[0][0]);
14+
// check[0][0] = true;
15+
// while(!que.empty()) {
16+
// int d = que.front().first;
17+
// int x = que.front().second.first;
18+
// int y = que.front().second.second;
19+
// que.pop();
20+
21+
// int new_x = x + dx[d];
22+
// int new_y = y + dy[d];
23+
// if(new_x >= 0 && new_y >= 0 && new_x < n && new_y < m && check[new_x][new_y] == false) {
24+
// check[new_x][new_y] = true;
25+
// sorted.push_back(matrix[new_x][new_y]);
26+
// que.push({d, {new_x, new_y}});
27+
// continue;
28+
// }
29+
// d = (d + 1) % 4;
30+
// new_x = x + dx[d];
31+
// new_y = y + dy[d];
32+
// if(new_x >= 0 && new_y >= 0 && new_x < n && new_y < m && check[new_x][new_y] == false) {
33+
// check[new_x][new_y] = true;
34+
// sorted.push_back(matrix[new_x][new_y]);
35+
// que.push({d, {new_x, new_y}});
36+
// }
37+
// }
38+
39+
// return sorted;
40+
// }
41+
// };
42+
43+
class Solution {
44+
public:
45+
vector<int> spiralOrder(vector<vector<int>>& matrix) {
46+
int n = matrix.size(), m = matrix[0].size();
47+
int top = 0, down = n - 1, left = 0, right = m - 1;
48+
vector<int> sorted;
49+
while(top <= down && left <= right) {
50+
for(int i = left; i <= right; i++)
51+
sorted.push_back(matrix[top][i]);
52+
top++;
53+
54+
for(int i = top; i <= down; i++)
55+
sorted.push_back(matrix[i][right]);
56+
right--;
57+
58+
if(top <= down) {
59+
for(int i = right; i >= left; i--)
60+
sorted.push_back(matrix[down][i]);
61+
down--;
62+
}
63+
64+
if(left <= right) {
65+
for(int i = down; i >= top; i--)
66+
sorted.push_back(matrix[i][left]);
67+
left++;
68+
}
69+
}
70+
71+
return sorted;
72+
}
73+
};
74+

valid-parentheses/ys-han00.cpp

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
class Solution {
2+
public:
3+
bool isValid(string s) {
4+
stack<char> sta;
5+
6+
for(char c : s) {
7+
if(c == '(' || c == '[' || c == '{') {
8+
sta.push(c);
9+
continue;
10+
}
11+
12+
if(sta.empty())
13+
sta.push(c);
14+
else if(c == ')' && sta.top() == '(')
15+
sta.pop();
16+
else if(c == ']' && sta.top() == '[')
17+
sta.pop();
18+
else if(c == '}' && sta.top() == '{')
19+
sta.pop();
20+
else
21+
sta.push(c);
22+
}
23+
24+
return (sta.empty() ? true : false);
25+
}
26+
};
27+

0 commit comments

Comments
 (0)