-
Notifications
You must be signed in to change notification settings - Fork 126
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #359 from heozeop/main
[crispy] week 2 solution
- Loading branch information
Showing
5 changed files
with
180 additions
and
0 deletions.
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(nlogn) | ||
// 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; | ||
} | ||
}; | ||
|