-
Notifications
You must be signed in to change notification settings - Fork 126
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
[crispy] week 2 solution #359
Merged
Merged
Changes from 5 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
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
37 changes: 37 additions & 0 deletions
37
construct-binary-tree-from-preorder-and-inorder-traversal/heozeop.cpp
This file contains 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,37 @@ | ||
// Time Complexity: O(n^2) | ||
// Spatial Complexity: O(n) | ||
|
||
class Solution { | ||
private: | ||
int findIndex(int targetVal, vector<int>& inorder) { | ||
auto pos = find(inorder.begin(), inorder.end(), targetVal); | ||
if (pos == inorder.end()) { | ||
return -1; | ||
} | ||
|
||
return pos - inorder.begin(); | ||
} | ||
|
||
TreeNode* dfs(vector<int>& preorder, vector<int>& inorder, int preorderIndex, int startIndex, int endIndex) { | ||
if (preorder.size() <= preorderIndex || startIndex > endIndex) { | ||
return nullptr; | ||
} | ||
|
||
int targetValue = preorder[preorderIndex]; | ||
int rootIndex = this->findIndex(targetValue, inorder); | ||
if(rootIndex < 0) { | ||
return nullptr; | ||
} | ||
|
||
int leftSubtreeLength = rootIndex - startIndex; | ||
|
||
TreeNode* left = dfs(preorder, inorder, preorderIndex + 1, startIndex, rootIndex - 1); | ||
TreeNode* right = dfs(preorder, inorder, preorderIndex + 1 + leftSubtreeLength, rootIndex + 1, endIndex); | ||
|
||
return new TreeNode(targetValue, left, right); | ||
} | ||
public: | ||
TreeNode* buildTree(vector<int>& preorder, vector<int>& inorder) { | ||
return this->dfs(preorder, inorder, 0, 0, preorder.size() - 1); | ||
} | ||
}; |
This file contains 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,31 @@ | ||
// Time Complexity: O(n^2) | ||
// Spatial Complexity: O(n) | ||
|
||
class Solution { | ||
private: | ||
int count1(int n) { | ||
int ans = 0; | ||
|
||
while(n) { | ||
if (n % 2) { | ||
++ans; | ||
} | ||
|
||
n /= 2; | ||
} | ||
|
||
return ans; | ||
} | ||
|
||
public: | ||
vector<int> countBits(int n) { | ||
vector<int> ans(n + 1); | ||
|
||
for(int i = 0; i <= n; ++i) { | ||
ans[i] = this->count1(i); | ||
} | ||
|
||
return ans; | ||
} | ||
}; | ||
|
This file contains 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,31 @@ | ||
// Time Complexity: O(n) | ||
// Spatial Complexity: O(n) | ||
|
||
class Solution { | ||
public: | ||
int numDecodings(string s) { | ||
if(s.length() < 1 || s[0] == '0') { | ||
return 0; | ||
} | ||
|
||
vector<int> dp(s.length() + 1, 0); | ||
dp[0] = dp[1] = 1; | ||
if(s[1] == '0') { | ||
dp[1] = 0; | ||
} | ||
|
||
int prev,pprev; | ||
for(int i = 2; i <= s.length(); ++i) { | ||
prev = s[i - 1] - '0'; | ||
if (prev <= 9 && prev > 0) { | ||
dp[i] += dp[i-1]; | ||
} | ||
pprev = (s[i - 2] - '0') * 10 + prev; | ||
if(pprev <= 26 && pprev > 9) { | ||
dp[i] += dp[i-2]; | ||
} | ||
} | ||
|
||
return dp[s.length()]; | ||
} | ||
}; |
This file contains 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,55 @@ | ||
// Time Complexity: O(n) | ||
// Spatial Complexity: O(n) | ||
|
||
class Codec { | ||
private: | ||
string genereateRandomString(size_t size) { | ||
string randomString = ""; | ||
for(size_t i = 0; i < size; ++i) { | ||
randomString += static_cast<char>(rand() % 256); | ||
} | ||
|
||
return randomString; | ||
} | ||
|
||
vector<string> split(string target, string delimiter) { | ||
vector<string> ans; | ||
|
||
int delimiterLength = delimiter.size(); | ||
size_t pos = target.find(delimiter); | ||
while(pos != string::npos) { | ||
ans.push_back(target.substr(0, pos)); | ||
|
||
target = target.substr(pos + delimiterLength, target.size()); | ||
pos = target.find(delimiter); | ||
} | ||
ans.push_back(target); | ||
|
||
return ans; | ||
} | ||
|
||
string delimiter = this->genereateRandomString(10); | ||
|
||
public: | ||
|
||
// Encodes a list of strings to a single string. | ||
string encode(vector<string>& strs) { | ||
string encodedString = strs[0]; | ||
|
||
for(int i = 1; i < strs.size(); ++i) { | ||
encodedString += this->delimiter + strs[i]; | ||
} | ||
|
||
return encodedString; | ||
} | ||
|
||
// Decodes a single string to a list of strings. | ||
vector<string> decode(string s) { | ||
|
||
return split(s, this->delimiter); | ||
} | ||
}; | ||
|
||
// Your Codec object will be instantiated and called as such: | ||
// Codec codec; | ||
// codec.decode(codec.encode(strs)); |
This file contains 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,26 @@ | ||
// Time Complexity: O(n) | ||
// Spatial Complexity: O(1) | ||
|
||
class Solution { | ||
public: | ||
bool isAnagram(string s, string t) { | ||
int numberOfAlphabet[26]; | ||
|
||
for(char character : s) { | ||
numberOfAlphabet[character - 'a']++; | ||
} | ||
|
||
for(char character : t) { | ||
numberOfAlphabet[character - 'a']--; | ||
} | ||
|
||
for(int i = 0; i < 26; ++i) { | ||
if (numberOfAlphabet[i] != 0) { | ||
return false; | ||
} | ||
} | ||
|
||
return true; | ||
} | ||
}; | ||
|
Oops, something went wrong.
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.
count1()
함수가 로그 시간이 걸리지 않을까요? 🤔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.
그러네요! 감사합니다!