Skip to content

Commit 8702661

Browse files
authored
Merge pull request #1740 from kay30kim/main
[kay30kim] WEEK 01 solutions
2 parents b3d7d16 + 8b080dd commit 8702661

File tree

5 files changed

+195
-0
lines changed

5 files changed

+195
-0
lines changed

contains-duplicate/kay30kim.cpp

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
/*
2+
Clarifications
3+
1) Is this sorted Array?
4+
2) What is the maximum / minimum value in nums?
5+
6+
Solutions
7+
1) Brute force - two nested for loop
8+
T(C): O(n^2)
9+
S(C): O(1)
10+
2) Sort - Mergesort or library sort(Merge+quick)
11+
T(C) : O(NlogN)
12+
S(C) : O(N)
13+
3) Hash
14+
T(C) : O(1)
15+
S(C) : O(N)
16+
*/
17+
18+
class Solution {
19+
public:
20+
// Solution 2-1) Library Sort
21+
// bool containsDuplicate(vector<int>& nums) {
22+
// sort(nums.begin(), nums.end());
23+
// for (size_t i = 0; i < nums.size() - 1; i++)
24+
// {
25+
// if (nums[i] == nums[i + 1])
26+
// return true;
27+
// }
28+
// return false;
29+
// }
30+
31+
// Solution 2-2) Implement Sort
32+
void mergeSort(vector<int>& nums, int start, int end, vector<int>& temp)
33+
{
34+
if (start >= end)
35+
return;
36+
int mid = (start + end) / 2;
37+
mergeSort(nums, start, mid, temp);
38+
mergeSort(nums, mid + 1, end, temp);
39+
int i = start, j = mid + 1, k = start;
40+
while (i <= mid && j <= end)
41+
{
42+
if (nums[i] < nums[j])
43+
temp[k++] = nums[i++];
44+
else
45+
temp[k++] = nums[j++];
46+
}
47+
while (i <= mid)
48+
temp[k++] = nums[i++];
49+
while (j <= end)
50+
temp[k++] = nums[j++];
51+
for (int p = start; p <= end; p++)
52+
nums[p] = temp[p];
53+
}
54+
// Solution 1-2) Library Sort
55+
// bool containsDuplicate(vector<int>& nums) {
56+
// vector<int> temp(nums.size(), 0);
57+
// mergeSort(nums, 0, nums.size() - 1, temp);
58+
// for (size_t i = 0; i < nums.size() - 1; i++)
59+
// {
60+
// if (nums[i] == nums[i + 1])
61+
// return true;
62+
// }
63+
// return false;
64+
// }
65+
66+
// solution 2 - hash
67+
bool containsDuplicate(vector<int>& nums) {
68+
unordered_map<int, int> hash;
69+
for (size_t i = 0; i < nums.size(); i++)
70+
{
71+
if (hash.find(nums[i]) != hash.end())
72+
return true;
73+
hash[nums[i]] = i;
74+
}
75+
return false;
76+
}
77+
};

house-robber/kay30kim.cpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
class Solution {
2+
public:
3+
int rob(vector<int>& nums) {
4+
vector<vector<int>> dp(nums.size() + 1, vector<int>(2, 0));
5+
dp[0][1] = nums[0];
6+
for (int i = 1; i < nums.size(); i++)
7+
{
8+
dp[i][0] = max(dp[i - 1][0], dp[i - 1][1]);
9+
dp[i][1] = dp[i - 1][0] + nums[i];
10+
}
11+
return max(dp[nums.size() - 1][0], dp[nums.size() - 1][1]);
12+
}
13+
};
14+
//T(C) : O(N)
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
class Solution {
2+
public:
3+
int longestConsecutive(vector<int>& nums) {
4+
unordered_set<int> isNum;
5+
int ans = 0;
6+
for (auto num : nums)
7+
isNum.insert(num);
8+
for (auto num : isNum) // (auto num : nums)하면 틀린다 일부러 시작하는 위치를 많이 넣는 테스트케이스 존재
9+
{
10+
if (isNum.find(num - 1) == isNum.end() && isNum.find(num) !=isNum.end())
11+
{
12+
int curLen = 0;
13+
for (int seqNum = num; isNum.find(seqNum) != isNum.end(); seqNum++)
14+
{
15+
curLen += 1;
16+
}
17+
ans = max(ans, curLen);
18+
}
19+
}
20+
return ans;
21+
}
22+
};
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
// 1) Sort -> val, cnt ->
2+
// T(C) : O(NlogN)
3+
// 2) Heap
4+
// T(C) : O(NlogN)
5+
// 3) Bucket Sort
6+
// T(C) : O(N)
7+
class Solution {
8+
private:
9+
struct Node
10+
{
11+
int val;
12+
int cnt;
13+
bool operator<(const Node &input) const
14+
{
15+
return cnt < input.cnt;
16+
}
17+
};
18+
public:
19+
// Solution 2 - Heap
20+
// vector<int> topKFrequent(vector<int>& nums, int k) {
21+
// priority_queue<Node> pq;
22+
// unordered_map<int, int> numCnt;
23+
// vector<int> topKvalueVec;
24+
// for(auto num : nums)
25+
// numCnt[num] += 1;
26+
// for(auto it : numCnt)
27+
// pq.push({it.first, it.second});
28+
// for (int i = 0; i < k && !pq.empty(); i++)
29+
// {
30+
// topKvalueVec.push_back(pq.top().val);
31+
// pq.pop();
32+
// }
33+
// return topKvalueVec;
34+
// }
35+
36+
// Solution 3 - Bucket Sort
37+
vector<int> topKFrequent(vector<int>& nums, int k) {
38+
unordered_map<int,int> numToCnt;
39+
vector<vector<int>> freq(nums.size() + 1, vector<int>());
40+
vector<int> ans;
41+
for (int i = 0; i < nums.size(); i++)
42+
numToCnt[nums[i]] += 1;
43+
for (auto it : numToCnt)
44+
freq[it.second].push_back(it.first);
45+
for (int cnt = nums.size(); cnt >= 0; cnt--)
46+
{
47+
for (auto num : freq[cnt])
48+
{
49+
ans.push_back(num);
50+
if (ans.size() >= k)
51+
return ans;
52+
}
53+
}
54+
return ans;
55+
}
56+
};

two-sum/kay30kim.cpp

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
// 1) Brute Force - two nested for loop
2+
// T(C) : O(N^2)
3+
// 2) sort & two pointer
4+
// T(C) : O(NlogN)
5+
// 3) hash
6+
// T(C) : O(N)
7+
8+
9+
class Solution {
10+
public:
11+
vector<int> twoSum(vector<int>& nums, int target) {
12+
vector<int> indicesVector;
13+
unordered_map<int, int> numToIdx;
14+
for (int i = 0; i < nums.size(); i++)
15+
{
16+
if(numToIdx.find(target - nums[i]) != numToIdx.end())
17+
{
18+
indicesVector.push_back(numToIdx[target - nums[i]]);
19+
indicesVector.push_back(i);
20+
break;
21+
}
22+
numToIdx[nums[i]] = i;
23+
}
24+
return indicesVector;
25+
}
26+
};

0 commit comments

Comments
 (0)