Skip to content
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 6 commits into from
Aug 25, 2024
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
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);
}
};
31 changes: 31 additions & 0 deletions counting-bits/heozeop.cpp
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;
}
};

31 changes: 31 additions & 0 deletions decode-ways/heozeop.cpp
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()];
}
};
55 changes: 55 additions & 0 deletions encode-and-decode-strings/heozeop.cpp
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));
26 changes: 26 additions & 0 deletions valid-anagram/heozeop.cpp
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;
}
};