diff --git a/container-with-most-water/ys-han00.cpp b/container-with-most-water/ys-han00.cpp new file mode 100644 index 000000000..a9fc2c449 --- /dev/null +++ b/container-with-most-water/ys-han00.cpp @@ -0,0 +1,39 @@ +// class Solution { +// public: +// int maxArea(vector& height) { +// int n = height.size(); +// pair left_wall = {0, height[0]}; +// int maxi = -1; +// vector 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& 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; + } +}; + diff --git a/design-add-and-search-words-data-structure/ys-han00.cpp b/design-add-and-search-words-data-structure/ys-han00.cpp new file mode 100644 index 000000000..d199dadbb --- /dev/null +++ b/design-add-and-search-words-data-structure/ys-han00.cpp @@ -0,0 +1,56 @@ +struct TrieNode { + unordered_map 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> 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); + */ + diff --git a/longest-increasing-subsequence/ys-han00.cpp b/longest-increasing-subsequence/ys-han00.cpp new file mode 100644 index 000000000..5f56e4f2e --- /dev/null +++ b/longest-increasing-subsequence/ys-han00.cpp @@ -0,0 +1,38 @@ +// class Solution { +// public: +// int lengthOfLIS(vector& nums) { +// int n = nums.size(), maxi, ans = 1; +// vector 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& nums) { + vector 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(); + } +}; + diff --git a/spiral-matrix/ys-han00.cpp b/spiral-matrix/ys-han00.cpp new file mode 100644 index 000000000..ec9cdeaa7 --- /dev/null +++ b/spiral-matrix/ys-han00.cpp @@ -0,0 +1,74 @@ +// class Solution { +// public: +// vector spiralOrder(vector>& matrix) { +// int n = matrix.size(), m = matrix[0].size(); +// vector sorted; +// vector> check(n, vector(m, false)); +// queue>> 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 spiralOrder(vector>& matrix) { + int n = matrix.size(), m = matrix[0].size(); + int top = 0, down = n - 1, left = 0, right = m - 1; + vector 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; + } +}; + diff --git a/valid-parentheses/ys-han00.cpp b/valid-parentheses/ys-han00.cpp new file mode 100644 index 000000000..a6692a51a --- /dev/null +++ b/valid-parentheses/ys-han00.cpp @@ -0,0 +1,27 @@ +class Solution { +public: + bool isValid(string s) { + stack 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); + } +}; +