Skip to content
Open
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
39 changes: 39 additions & 0 deletions container-with-most-water/ys-han00.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
// class Solution {
// public:
// int maxArea(vector<int>& height) {
// int n = height.size();
// pair<int, int> left_wall = {0, height[0]};
// int maxi = -1;
// vector<int> dp(n, 0);

// for(int i = 1; i < n; i++) {
// for(int j = 0; j < i; j++) {
// dp[i] = max(dp[i], min(height[i], height[j]) * (i - j));
// if(height[j] > height[i])
// break;
// }
// maxi = max(maxi, dp[i]);
// }

// return maxi;
// }
// };

class Solution {
public:
int maxArea(vector<int>& height) {
int left = 0, right = height.size() - 1;
int ans = -1;

while(left != right) {
ans = max(ans, (right - left) * min(height[left], height[right]));
if(height[left] < height[right])
left++;
else
right--;
}

return ans;
}
};

56 changes: 56 additions & 0 deletions design-add-and-search-words-data-structure/ys-han00.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
struct TrieNode {
unordered_map<char, TrieNode*> child;
bool isEnd;
TrieNode() : isEnd(false) {}
};

class WordDictionary {
public:
TrieNode* root;
WordDictionary() {
root = new TrieNode();
}

void addWord(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) {
bool find = false;
stack<pair<int, TrieNode*>> sta;

sta.push({0, root});
while(!sta.empty()) {
int now = sta.top().first;
TrieNode* cur = sta.top().second;
sta.pop();

if(now == word.size() && cur->isEnd)
return true;

if(word[now] == '.') {
for(auto mp : cur->child)
sta.push({now + 1, mp.second});
} else {
if(cur->child.find(word[now]) != cur->child.end())
sta.push({now + 1, cur->child[word[now]]});
}
}

return false;
}
};

/**
* Your WordDictionary object will be instantiated and called as such:
* WordDictionary* obj = new WordDictionary();
* obj->addWord(word);
* bool param_2 = obj->search(word);
*/

38 changes: 38 additions & 0 deletions longest-increasing-subsequence/ys-han00.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
// class Solution {
// public:
// int lengthOfLIS(vector<int>& nums) {
// int n = nums.size(), maxi, ans = 1;
// vector<int> dp(n, 0);

// dp[0] = 1;
// for(int i = 1; i < n; i++) {
// maxi = 0;
// for(int j = 0; j < i; j++)
// if(nums[j] < nums[i] && maxi < dp[j])
// maxi = dp[j];
// dp[i] = maxi + 1;
// ans = max(dp[i] , ans);
// }

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

class Solution {
public:
int lengthOfLIS(vector<int>& nums) {
vector<int> sub;

for(int num : nums) {
auto it = lower_bound(sub.begin(), sub.end(), num);
int idx = it - sub.begin();
if(idx == sub.size())
sub.push_back(num);
else
sub[idx] = num;
}

return sub.size();
}
};

74 changes: 74 additions & 0 deletions spiral-matrix/ys-han00.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
// class Solution {
// public:
// vector<int> spiralOrder(vector<vector<int>>& matrix) {
// int n = matrix.size(), m = matrix[0].size();
// vector<int> sorted;
// vector<vector<bool>> check(n, vector<bool>(m, false));
// queue<pair<int, pair<int, int>>> que;

// int dx[4] = {-1, 0, 1, 0};
// int dy[4] = {0, 1, 0, -1};
// // 0: up, 1: right, 2: down, 3: left
// que.push({1, {0, 0}});
// sorted.push_back(matrix[0][0]);
// check[0][0] = true;
// while(!que.empty()) {
// int d = que.front().first;
// int x = que.front().second.first;
// int y = que.front().second.second;
// que.pop();

// int new_x = x + dx[d];
// int new_y = y + dy[d];
// if(new_x >= 0 && new_y >= 0 && new_x < n && new_y < m && check[new_x][new_y] == false) {
// check[new_x][new_y] = true;
// sorted.push_back(matrix[new_x][new_y]);
// que.push({d, {new_x, new_y}});
// continue;
// }
// d = (d + 1) % 4;
// new_x = x + dx[d];
// new_y = y + dy[d];
// if(new_x >= 0 && new_y >= 0 && new_x < n && new_y < m && check[new_x][new_y] == false) {
// check[new_x][new_y] = true;
// sorted.push_back(matrix[new_x][new_y]);
// que.push({d, {new_x, new_y}});
// }
// }

// return sorted;
// }
// };

class Solution {
public:
vector<int> spiralOrder(vector<vector<int>>& matrix) {
int n = matrix.size(), m = matrix[0].size();
int top = 0, down = n - 1, left = 0, right = m - 1;
vector<int> sorted;
while(top <= down && left <= right) {
for(int i = left; i <= right; i++)
sorted.push_back(matrix[top][i]);
top++;

for(int i = top; i <= down; i++)
sorted.push_back(matrix[i][right]);
right--;

if(top <= down) {
for(int i = right; i >= left; i--)
sorted.push_back(matrix[down][i]);
down--;
}

if(left <= right) {
for(int i = down; i >= top; i--)
sorted.push_back(matrix[i][left]);
left++;
}
}

return sorted;
}
};

27 changes: 27 additions & 0 deletions valid-parentheses/ys-han00.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
class Solution {
public:
bool isValid(string s) {
stack<char> sta;

for(char c : s) {
if(c == '(' || c == '[' || c == '{') {
sta.push(c);
continue;
}

if(sta.empty())
sta.push(c);
else if(c == ')' && sta.top() == '(')
sta.pop();
else if(c == ']' && sta.top() == '[')
sta.pop();
else if(c == '}' && sta.top() == '{')
sta.pop();
else
sta.push(c);
}

return (sta.empty() ? true : false);
}
};