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 1 Solution explanation #313

Merged
merged 9 commits into from
Aug 16, 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
20 changes: 20 additions & 0 deletions contains-duplicate/heozeop.cpp
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;
}
};
46 changes: 46 additions & 0 deletions kth-smallest-element-in-a-bst/heozeop.cpp
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());
Copy link
Contributor

@bky373 bky373 Aug 15, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

다른 문제에서도 그렇고, 이 문제 역시 깔끔하게 해결하신 것 같습니다!
다만 현재는 큐를 한 번 순회하고, 정렬을 위해 다시 한 번 순회하여 총 두 번의 순회를 하고 있는데,
한 번의 순회만으로도 문제를 해결할 수 있을 것 같습니다!

return numbers[k - 1];
}
};

19 changes: 19 additions & 0 deletions number-of-1-bits/heozeop.cpp
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;
}
};

71 changes: 71 additions & 0 deletions palindromic-substrings/heozeop.cpp
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;
}
};
38 changes: 38 additions & 0 deletions top-k-frequent-elements/heozeop.cpp
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;
}
};