-
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 1 Solution explanation #313
Merged
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
87bb8ea
solve: contains-duplicate
heozeop e4fef82
renmae to fit with contribution guide
heozeop 8f9edb2
feat: add complexity
heozeop 8966444
solve: number of 1 bit
heozeop 3f96089
docs: add tc and sc
heozeop ec611d9
solve: top k frequent elements
heozeop c844df2
solve: kth smallest in bst
heozeop 42d5450
solve: palindromic substring
heozeop 1250979
feat: add dp version of palindromic substring
heozeop 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 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,20 @@ | ||
// Time Complexity : O(n) | ||
// Spatial Complexity : O(n) | ||
|
||
#include <set> | ||
|
||
class Solution { | ||
public: | ||
bool containsDuplicate(vector<int>& nums) { | ||
set<int> count; | ||
|
||
for(int num : nums) { | ||
if (count.find(num) != count.end()) { | ||
return true; | ||
} | ||
count.insert(num); | ||
} | ||
|
||
return false; | ||
} | ||
}; |
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,46 @@ | ||
// Time Complexity: O(nlogn) | ||
// Spatial Complexity O(n) | ||
|
||
#include <vector> | ||
#include <queue> | ||
/** | ||
* Definition for a binary tree node. | ||
* struct TreeNode { | ||
* int val; | ||
* TreeNode *left; | ||
* TreeNode *right; | ||
* TreeNode() : val(0), left(nullptr), right(nullptr) {} | ||
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {} | ||
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {} | ||
* }; | ||
*/ | ||
class Solution { | ||
public: | ||
int kthSmallest(TreeNode* root, int k) { | ||
queue<TreeNode*> q; | ||
q.push(root); | ||
|
||
vector<int> numbers; | ||
TreeNode *curNode, *nextNode; | ||
while(!q.empty()) { | ||
curNode = q.front(); | ||
q.pop(); | ||
|
||
numbers.push_back(curNode->val); | ||
|
||
nextNode = curNode->left; | ||
if(nextNode != nullptr) { | ||
q.push(nextNode); | ||
} | ||
|
||
nextNode = curNode->right; | ||
if(nextNode != nullptr) { | ||
q.push(nextNode); | ||
} | ||
} | ||
|
||
sort(numbers.begin(), numbers.end()); | ||
return numbers[k - 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,19 @@ | ||
// Time Complexity: O(log(n)) | ||
// Spatial Complexity: O(1) | ||
|
||
class Solution { | ||
public: | ||
int hammingWeight(int n) { | ||
int numberOf1Bit = 0; | ||
while(n) { | ||
if(n % 2) { | ||
numberOf1Bit += 1; | ||
} | ||
|
||
n = n >> 1; | ||
} | ||
|
||
return numberOf1Bit; | ||
} | ||
}; | ||
|
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,71 @@ | ||
// Time Complexity: O(n^3) | ||
// Spatial Complexity: O(1) | ||
|
||
// #include <set> | ||
// #include <string> | ||
|
||
class Solution { | ||
private: | ||
bool isPalindrom(string s) { | ||
int sLength = s.length(); | ||
|
||
for(int i = 0; i < sLength / 2; ++i) { | ||
if(s[i] != s[sLength - 1 - i]) { | ||
return false; | ||
} | ||
} | ||
|
||
return true; | ||
} | ||
|
||
public: | ||
int countSubstrings(string s) { | ||
int answer = 0; | ||
|
||
int sLength = s.length(); | ||
string temp; | ||
for(int i = 1; i <= sLength; ++i) { | ||
for(int j = 0; j + i <= sLength; ++j) { | ||
temp = s.substr(j, i); | ||
|
||
if(this->isPalindrom(temp)) { | ||
++answer; | ||
} | ||
} | ||
} | ||
|
||
return answer; | ||
} | ||
}; | ||
|
||
// DP version | ||
// O(n^2) | ||
// #include <set> | ||
// #include <vector> | ||
|
||
class Solution2 { | ||
public: | ||
int countSubstrings(string str) { | ||
int answer = 0; | ||
|
||
int n = str.length(); | ||
vector<vector<bool>> dp(n + 1, vector<bool>(n + 1, false)); | ||
|
||
for(int e = 0; e < n; ++e) { | ||
for(int s = 0; s <= e; ++s) { | ||
if (str[s] != str[e]) { | ||
continue; | ||
} | ||
|
||
if (s != e && s + 1 != e && !dp[s + 1][e - 1]) { | ||
continue; | ||
} | ||
|
||
++answer; | ||
dp[s][e] = true; | ||
} | ||
} | ||
|
||
return answer; | ||
} | ||
}; |
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,38 @@ | ||
// Time complexity O(nlogn) | ||
// Spatal Complexity O(n) | ||
|
||
#include <map> | ||
#include <vector> | ||
|
||
using namespace std; | ||
|
||
|
||
bool cmp(const pair<int,int>& a, const pair<int,int>& b) { | ||
return a.second > b.second; | ||
} | ||
|
||
class Solution { | ||
public: | ||
vector<int> topKFrequent(vector<int>& nums, int k) { | ||
map<int, int> countMap; | ||
|
||
for(int num : nums) { | ||
if(countMap.find(num) == countMap.end()) { | ||
countMap.insert({num, 0}); | ||
} | ||
|
||
++countMap[num]; | ||
} | ||
|
||
vector<pair<int,int>> vec(countMap.begin(), countMap.end()); | ||
sort(vec.begin(), vec.end(), cmp); | ||
|
||
vector<int> answer; | ||
for(int i = 0; i < k; ++i) { | ||
answer.push_back(vec[i].first); | ||
} | ||
|
||
return answer; | ||
} | ||
}; | ||
|
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.
다른 문제에서도 그렇고, 이 문제 역시 깔끔하게 해결하신 것 같습니다!
다만 현재는 큐를 한 번 순회하고, 정렬을 위해 다시 한 번 순회하여 총 두 번의 순회를 하고 있는데,
한 번의 순회만으로도 문제를 해결할 수 있을 것 같습니다!