Skip to content

[kay30kim] WEEK 01 solutions #1740

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

Merged
merged 2 commits into from
Jul 28, 2025
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
77 changes: 77 additions & 0 deletions contains-duplicate/kay30kim.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
/*
Clarifications
1) Is this sorted Array?
2) What is the maximum / minimum value in nums?

Solutions
1) Brute force - two nested for loop
T(C): O(n^2)
S(C): O(1)
2) Sort - Mergesort or library sort(Merge+quick)
T(C) : O(NlogN)
S(C) : O(N)
3) Hash
T(C) : O(1)
S(C) : O(N)
*/

class Solution {
public:
// Solution 2-1) Library Sort
// bool containsDuplicate(vector<int>& nums) {
// sort(nums.begin(), nums.end());
// for (size_t i = 0; i < nums.size() - 1; i++)
// {
// if (nums[i] == nums[i + 1])
// return true;
// }
// return false;
// }

// Solution 2-2) Implement Sort
void mergeSort(vector<int>& nums, int start, int end, vector<int>& temp)
{
if (start >= end)
return;
int mid = (start + end) / 2;
mergeSort(nums, start, mid, temp);
mergeSort(nums, mid + 1, end, temp);
int i = start, j = mid + 1, k = start;
while (i <= mid && j <= end)
{
if (nums[i] < nums[j])
temp[k++] = nums[i++];
else
temp[k++] = nums[j++];
}
while (i <= mid)
temp[k++] = nums[i++];
while (j <= end)
temp[k++] = nums[j++];
for (int p = start; p <= end; p++)
nums[p] = temp[p];
}
// Solution 1-2) Library Sort
// bool containsDuplicate(vector<int>& nums) {
// vector<int> temp(nums.size(), 0);
// mergeSort(nums, 0, nums.size() - 1, temp);
// for (size_t i = 0; i < nums.size() - 1; i++)
// {
// if (nums[i] == nums[i + 1])
// return true;
// }
// return false;
// }

// solution 2 - hash
bool containsDuplicate(vector<int>& nums) {
unordered_map<int, int> hash;
for (size_t i = 0; i < nums.size(); i++)
{
if (hash.find(nums[i]) != hash.end())
return true;
hash[nums[i]] = i;
}
return false;
}
};
14 changes: 14 additions & 0 deletions house-robber/kay30kim.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
class Solution {
public:
int rob(vector<int>& nums) {
vector<vector<int>> dp(nums.size() + 1, vector<int>(2, 0));
dp[0][1] = nums[0];
for (int i = 1; i < nums.size(); i++)
{
dp[i][0] = max(dp[i - 1][0], dp[i - 1][1]);
dp[i][1] = dp[i - 1][0] + nums[i];
}
return max(dp[nums.size() - 1][0], dp[nums.size() - 1][1]);
}
};
//T(C) : O(N)
22 changes: 22 additions & 0 deletions longest-consecutive-sequence/kay30kim.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
class Solution {
public:
int longestConsecutive(vector<int>& nums) {
unordered_set<int> isNum;
int ans = 0;
for (auto num : nums)
isNum.insert(num);
for (auto num : isNum) // (auto num : nums)하면 틀린다 일부러 시작하는 위치를 많이 넣는 테스트케이스 존재
{
if (isNum.find(num - 1) == isNum.end() && isNum.find(num) !=isNum.end())
{
int curLen = 0;
for (int seqNum = num; isNum.find(seqNum) != isNum.end(); seqNum++)
{
curLen += 1;
}
ans = max(ans, curLen);
}
}
return ans;
}
};
56 changes: 56 additions & 0 deletions top-k-frequent-elements/kay30kim.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
// 1) Sort -> val, cnt ->
// T(C) : O(NlogN)
// 2) Heap
// T(C) : O(NlogN)
// 3) Bucket Sort
// T(C) : O(N)
class Solution {
private:
struct Node
{
int val;
int cnt;
bool operator<(const Node &input) const
{
return cnt < input.cnt;
}
};
public:
// Solution 2 - Heap
// vector<int> topKFrequent(vector<int>& nums, int k) {
// priority_queue<Node> pq;
// unordered_map<int, int> numCnt;
// vector<int> topKvalueVec;
// for(auto num : nums)
// numCnt[num] += 1;
// for(auto it : numCnt)
// pq.push({it.first, it.second});
// for (int i = 0; i < k && !pq.empty(); i++)
// {
// topKvalueVec.push_back(pq.top().val);
// pq.pop();
// }
// return topKvalueVec;
// }

// Solution 3 - Bucket Sort
vector<int> topKFrequent(vector<int>& nums, int k) {
unordered_map<int,int> numToCnt;
vector<vector<int>> freq(nums.size() + 1, vector<int>());
vector<int> ans;
for (int i = 0; i < nums.size(); i++)
numToCnt[nums[i]] += 1;
for (auto it : numToCnt)
freq[it.second].push_back(it.first);
for (int cnt = nums.size(); cnt >= 0; cnt--)
{
for (auto num : freq[cnt])
{
ans.push_back(num);
if (ans.size() >= k)
return ans;
}
}
return ans;
}
};
26 changes: 26 additions & 0 deletions two-sum/kay30kim.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// 1) Brute Force - two nested for loop
// T(C) : O(N^2)
// 2) sort & two pointer
// T(C) : O(NlogN)
// 3) hash
// T(C) : O(N)


class Solution {
public:
vector<int> twoSum(vector<int>& nums, int target) {
vector<int> indicesVector;
unordered_map<int, int> numToIdx;
for (int i = 0; i < nums.size(); i++)
{
if(numToIdx.find(target - nums[i]) != numToIdx.end())
{
indicesVector.push_back(numToIdx[target - nums[i]]);
indicesVector.push_back(i);
break;
}
numToIdx[nums[i]] = i;
}
return indicesVector;
}
};