Skip to content

[crumbs22] Week 06 solutions #1443

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 10, 2025
Merged
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
28 changes: 28 additions & 0 deletions container-with-most-water/crumbs22.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#include <vector>
#include <iostream>
#include <algorithm>

using namespace std;

class Solution {
public:
int maxArea(vector<int>& 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);
}
};
56 changes: 56 additions & 0 deletions design-add-and-search-words-data-structure/crumbs22.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
#include <iostream>
#include <string>

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);
}
};
22 changes: 22 additions & 0 deletions longest-increasing-subsequence/crumbs22.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#include <iostream>
#include <vector>
#include <cmath>

using namespace std;

class Solution {
public:
int lengthOfLIS(vector<int>& nums) {
vector<int> 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);
}
};
40 changes: 40 additions & 0 deletions valid-parentheses/crumbs22.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
#include <iostream>
#include <stack>
#include <string>

using namespace std;

class Solution {
public:
bool isValid(string s) {
stack<char> 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());
}
};