Skip to content

Commit cc29e97

Browse files
authored
Merge branch 'DaleStudy:main' into main
2 parents 5530ced + 8702661 commit cc29e97

File tree

10 files changed

+446
-0
lines changed

10 files changed

+446
-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+
};

contains-duplicate/soobing2.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
/**
2+
* 문제 유형
3+
* - Array
4+
*
5+
* 문제 설명
6+
* - 중복된 수가 있는 경우 true, 없으면 false 반환
7+
*
8+
* 아이디어
9+
* - 전체적으로 순회하면서 중복된 수가 있는지 확인
10+
*
11+
*/
12+
function containsDuplicate(nums: number[]): boolean {
13+
const set = new Set<number>();
14+
for (let i = 0; i < nums.length; i++) {
15+
if (set.has(nums[i])) return true;
16+
17+
set.add(nums[i]);
18+
}
19+
return false;
20+
}

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)

house-robber/soobing2.ts

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
/**
2+
* 문제 유형: DP
3+
*
4+
* 문제 설명
5+
* - 바로 옆집을 한번에 털 수 없을때, 최대로 털 수 있는 돈을 구하는 문제
6+
* - 함정은, 홀수의 합 vs 짝수의 합만 비교해서는 안된다. 2개 초과해서 털 수 있는 경우가 있음 (ex. [2, 1, 1, 2])
7+
*
8+
* 아이디어
9+
* - DP 문제 답게 Top-down, Bottom-up 두 개 다 풀 수 있음
10+
*/
11+
12+
function robBottomUp(nums: number[]): number {
13+
const n = nums.length;
14+
const dp = Array(n).fill(0);
15+
16+
if (n === 1) return nums[0];
17+
if (n === 2) return Math.max(nums[0], nums[1]);
18+
19+
dp[0] = nums[0];
20+
dp[1] = Math.max(nums[0], nums[1]);
21+
22+
for (let i = 2; i < n; i++) {
23+
dp[i] = Math.max(dp[i - 1], dp[i - 2] + nums[i]);
24+
}
25+
26+
return dp[n - 1];
27+
}
28+
29+
function robTopDown(nums: number[]): number {
30+
const n = nums.length;
31+
const memo = new Map<number, number>();
32+
33+
const dp = (i: number) => {
34+
if (i < 0) return 0;
35+
if (memo.has(i)) return memo.get(i);
36+
37+
const res = Math.max(dp(i - 1)!, dp(i - 2)! + nums[i]);
38+
memo.set(i, res);
39+
return res;
40+
};
41+
return dp(n - 1)!;
42+
}
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: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
/**
2+
* 문제 유형: 그래프(Union Find)
3+
*
4+
* 문제 설명
5+
* - 주어진 배열에서 가장 긴 연속된 수열의 길이를 구하는 문제
6+
*
7+
* 아이디어
8+
* 1) 중복된 수 제거 후 순회하며 값이 1차이 나는 항목끼리 합집합 만들기
9+
*
10+
* 미션
11+
* - 더 빠른 풀이: HashSet 기반 Greedy로 풀어보기.
12+
*
13+
*/
14+
class UnionFind {
15+
parent: Map<number, number> = new Map();
16+
size: Map<number, number> = new Map();
17+
18+
constructor(nums: number[]) {
19+
for (const num of nums) {
20+
this.parent.set(num, num);
21+
this.size.set(num, 1);
22+
}
23+
}
24+
25+
find(x: number): number {
26+
if (this.parent.get(x) !== x) {
27+
this.parent.set(x, this.find(this.parent.get(x)!));
28+
}
29+
30+
return this.parent.get(x)!;
31+
}
32+
33+
union(x: number, y: number): void {
34+
const rootX = this.find(x);
35+
const rootY = this.find(y);
36+
37+
if (rootX === rootY) return;
38+
39+
const sizeX = this.size.get(rootX);
40+
const sizeY = this.size.get(rootY);
41+
if (sizeX < sizeY) {
42+
this.parent.set(rootX, rootY);
43+
this.size.set(rootY, sizeX + sizeY);
44+
} else {
45+
this.parent.set(rootY, rootX);
46+
this.size.set(rootX, sizeX + sizeY);
47+
}
48+
}
49+
50+
getMaxSize(): number {
51+
let max = 0;
52+
for (const size of this.size.values()) {
53+
max = Math.max(max, size);
54+
}
55+
return max;
56+
}
57+
}
58+
function longestConsecutive(nums: number[]): number {
59+
if (nums.length === 0) return 0;
60+
61+
const uniqueNums = Array.from(new Set(nums));
62+
const uf = new UnionFind(uniqueNums);
63+
64+
for (const num of uniqueNums) {
65+
if (uf.parent.has(num + 1)) {
66+
uf.union(num, num + 1);
67+
}
68+
}
69+
70+
return uf.getMaxSize();
71+
}
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+
};

0 commit comments

Comments
 (0)