Skip to content

[PDKhan] WEEK 06 solutions #1422

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 5 commits 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
22 changes: 22 additions & 0 deletions container-with-most-water/PDKhan.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
class Solution {
public:
int maxArea(vector<int>& height) {
int result = 0;
int left = 0;
int right = height.size() - 1;

while(left < right){
int len = right - left;

if(height[left] < height[right]){
result = max(result, height[left] * len);
left++;
}else{
result = max(result, height[right] * len);
right--;
}
}

return result;
}
};
60 changes: 60 additions & 0 deletions design-add-and-search-words-data-structure/PDKhan.cpp
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

전 생각해보지 못한 알파벳 개수로 arr제한 한 부분이 인상적입니다!!👍

Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
class Trie{
public:
Trie* children[26];
bool isEnd;

Trie(){
for(int i = 0; i < 26; i++)
children[i] = nullptr;

isEnd = false;
}
};

class WordDictionary {
private:
Trie* trie;
public:
WordDictionary() {
trie = new Trie();
}

void addWord(string word) {
Trie* node = trie;

for(char ch : word){
int index = ch - 'a';

if(node->children[index] == nullptr)
node->children[index] = new Trie();

node = node->children[index];
}

node->isEnd = true;
}

bool dfs(Trie* node, int index, string word){
if(node == nullptr)
return false;

if(index == word.length())
return node->isEnd;

char ch = word[index];

if(ch == '.'){
for(int i = 0; i < 26; i++){
if(dfs(node->children[i], index + 1, word) == true)
return true;
}
}else
return dfs(node->children[ch-'a'], index + 1, word);

return false;
}

bool search(string word) {
return dfs(trie, 0, word);
}
};
17 changes: 17 additions & 0 deletions longest-increasing-subsequence/PDKhan.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
class Solution {
public:
int lengthOfLIS(vector<int>& nums) {
vector<int> sub;

for(int num : nums){
auto it = lower_bound(sub.begin(), sub.end(), num);

if(it == sub.end())
sub.push_back(num);
else
*it = num;
}

return sub.size();
}
};
57 changes: 57 additions & 0 deletions spiral-matrix/PDKhan.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
class Solution {
public:
vector<int> spiralOrder(vector<vector<int>>& matrix) {
enum direction { R, D, L, U };

enum direction dir = R;
vector<int> result;
int min_R = 0;
int max_R = matrix.size() - 1;
int min_C = 0;
int max_C = matrix[0].size() - 1;
int r = 0;
int c = 0;
int size = matrix.size() * matrix[0].size();

while(result.size() < size){
result.push_back(matrix[r][c]);

switch(dir){
case R:
if(c == max_C){
dir = D;
min_R++;
r++;
}else
c++;
break;
case D:
if(r == max_R){
dir = L;
max_C--;
c--;
}else
r++;
break;
case L:
if(c == min_C){
dir = U;
max_R--;
r--;
}else
c--;
break;
case U:
if(r == min_R){
dir = R;
min_C++;
c++;
}else
r--;
break;
}
}

return result;
}
};
25 changes: 25 additions & 0 deletions valid-parentheses/PDKhan.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
class Solution {
public:
bool isValid(string s) {
stack<char> st;

for(char ch : s){
if(ch == ')'){
if(st.empty() || st.top() != '(')
return false;
st.pop();
}else if(ch == '}'){
if(st.empty() || st.top() != '{')
return false;
st.pop();
}else if(ch == ']'){
if(st.empty() || st.top() != '[')
return false;
st.pop();
}else
st.push(ch);
}

return st.empty();
}
};