-
-
Notifications
You must be signed in to change notification settings - Fork 248
[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
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
a4cf7ca
Valid Parentheses solution
PDKhan 8edebd3
container with most watter solution
PDKhan a705e6f
Design Add And Search Words Data Structure solution
PDKhan d5b3ef2
Longest Increasing Subsequence solution
PDKhan e6c4510
Spiral Matrix solution
PDKhan File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
}; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
}; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
}; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
}; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
}; |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
전 생각해보지 못한 알파벳 개수로 arr제한 한 부분이 인상적입니다!!👍