diff --git a/container-with-most-water/crumbs22.cpp b/container-with-most-water/crumbs22.cpp new file mode 100644 index 000000000..9096ace31 --- /dev/null +++ b/container-with-most-water/crumbs22.cpp @@ -0,0 +1,28 @@ +#include +#include +#include + +using namespace std; + +class Solution { + public: + int maxArea(vector& height) { + int left = 0; + int right = height.size() - 1; + int maxArea = 0; + + while (left < right) { + + int area = (right - left) * std::min(height[left], height[right]); + if (area > maxArea) + maxArea = area; + + if (height[left] < height[right]) + left++; + else + right--; + + } + return (maxArea); + } + }; diff --git a/design-add-and-search-words-data-structure/crumbs22.cpp b/design-add-and-search-words-data-structure/crumbs22.cpp new file mode 100644 index 000000000..aad9562a1 --- /dev/null +++ b/design-add-and-search-words-data-structure/crumbs22.cpp @@ -0,0 +1,56 @@ +#include +#include + +using namespace std; + +struct TrieNode { + bool isEnd; + TrieNode* children[26]; + TrieNode() : isEnd(false) { + for (int i = 0; i < 26; i++) + children[i] = nullptr; + } +}; + +class WordDictionary { +private: + TrieNode* root; + bool dfs(string& word, int idx, TrieNode* node) { + if (!node) + return (false); + if (idx == word.size()) // 단어가 끝났을 때만 true + return (node->isEnd); + + char c = word[idx]; + if (c == '.') { + for (int i = 0; i < 26; i++) { + if (dfs(word, idx + 1, node->children[i])) + return (true); + } + return (false); + } + else { + return dfs(word, idx + 1, node->children[c - 'a']); + } + } + +public: + WordDictionary() { + root = new TrieNode(); + } + + void addWord(string word) { + TrieNode* cur = root; + for (char c : word) { + int index = c - 'a'; + if (!cur->children[index]) + cur->children[index] = new TrieNode(); + cur = cur->children[index]; + } + cur->isEnd = true; + } + + bool search(string word) { + return dfs(word, 0, root); + } +}; diff --git a/longest-increasing-subsequence/crumbs22.cpp b/longest-increasing-subsequence/crumbs22.cpp new file mode 100644 index 000000000..53e39b513 --- /dev/null +++ b/longest-increasing-subsequence/crumbs22.cpp @@ -0,0 +1,22 @@ +#include +#include +#include + +using namespace std; + +class Solution { + public: + int lengthOfLIS(vector& nums) { + vector dp(nums.size(), 1); + int ans = 1; + + for (int i = 1; i < nums.size(); i++) { + for (int j = 0; j < i; j++) { + if (nums[i] > nums[j]) + dp[i] = max(dp[i], dp[j] + 1); + ans = max(ans, dp[i]); + } + } + return (ans); + } + }; diff --git a/valid-parentheses/crumbs22.cpp b/valid-parentheses/crumbs22.cpp new file mode 100644 index 000000000..d055ad7f9 --- /dev/null +++ b/valid-parentheses/crumbs22.cpp @@ -0,0 +1,40 @@ +#include +#include +#include + +using namespace std; + +class Solution { + public: + bool isValid(string s) { + stack st; + + for (char c : s) { + // if (!st.empty()) + // cout << st.top(); + if (c == '}') { + if (st.empty() || st.top() != '{') { + return (false); + } + st.pop(); + continue ; + } + else if (c == ')') { + if (st.empty() || st.top() != '(') { + return (false); + } + st.pop(); + continue ; + } + else if (c == ']') { + if (st.empty() || st.top() != '[') { + return (false); + } + st.pop(); + continue ; + } + st.push(c); + } + return (st.empty()); + } + };