Skip to content

Commit 95bed90

Browse files
authored
Merge pull request #1623 from sungjinwi/main
[sungjinwi] Week 13 solution
2 parents fb62b61 + 0e7eb6e commit 95bed90

File tree

5 files changed

+243
-0
lines changed

5 files changed

+243
-0
lines changed
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
/*
2+
풀이 :
3+
maxHeap과 minHeap 사용
4+
5+
heap에 담긴 총 수 : N
6+
7+
addNum()
8+
작은 수 집합은 maxHeap, 큰 수 집합은 minHeap에 담는다
9+
minHeap과 maxHeap의 개수차이를 1로 유지하면서 addNum
10+
11+
TC : O (log N)
12+
힙에 넣고 뺄 때 O (logN) 의 시간 소요
13+
14+
SC : O (N)
15+
총 힙 크기는 N에 비례
16+
17+
findMedian()
18+
더 크게 유지되는 것은 minHeap이므로 둘 의 개수가 같지 않을경우(총 홀수개) minHeap.top() 리턴
19+
같을 경우 총개수가 짝수개이므로 두 힙.top()의 평균을 리턴
20+
21+
TC : O (1)
22+
각 힙에서 root값 확인은 O(1)의 시간 소요
23+
SC : O (1)
24+
*/
25+
26+
#include <queue>
27+
#include <vector>
28+
using namespace std;
29+
30+
class MedianFinder {
31+
public:
32+
priority_queue<int> maxHeap;
33+
priority_queue<int, vector<int>, greater<int>> minHeap;
34+
35+
MedianFinder() {
36+
37+
}
38+
39+
void addNum(int num) {
40+
if (minHeap.empty() || num >= minHeap.top())
41+
minHeap.push(num);
42+
else
43+
maxHeap.push(num);
44+
45+
if (minHeap.size() > maxHeap.size() + 1) {
46+
maxHeap.push(minHeap.top());
47+
minHeap.pop();
48+
}
49+
else if (maxHeap.size() > minHeap.size()) {
50+
minHeap.push(maxHeap.top());
51+
maxHeap.pop();
52+
}
53+
}
54+
55+
double findMedian() {
56+
if (maxHeap.size() == minHeap.size())
57+
return static_cast<double>(maxHeap.top() + minHeap.top()) / 2;
58+
else
59+
return minHeap.top();
60+
}
61+
};
62+
63+
/**
64+
* Your MedianFinder object will be instantiated and called as such:
65+
* MedianFinder* obj = new MedianFinder();
66+
* obj->addNum(num);
67+
* double param_2 = obj->findMedian();
68+
*/

insert-interval/sungjinwi.cpp

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/*
2+
풀이 :
3+
기존 배열 중 start가 newInterval보다 빠르면서 newInterval과 겹치지 않는 요소들 result에 push
4+
겹치는 요소들은 newInterval에 통합 후 result에 push
5+
그 후 나머지 겹치지 않는 부분 result에 push
6+
7+
intervals 개수 : N
8+
9+
TC : O (N)
10+
11+
SC : O (1)
12+
리턴하는 배열 외에 추가 공간 사용 X
13+
*/
14+
15+
#include <vector>
16+
using namespace std;
17+
18+
class Solution {
19+
public:
20+
vector<vector<int>> insert(vector<vector<int>>& intervals, vector<int>& newInterval) {
21+
vector<vector<int>> result;
22+
int i = 0;
23+
int n = intervals.size();
24+
25+
while (i < n && intervals[i][1] < newInterval[0]) {
26+
result.push_back(intervals[i]);
27+
i++;
28+
}
29+
30+
while (i < n && intervals[i][0] <= newInterval[1]) {
31+
newInterval[0] = min(intervals[i][0], newInterval[0]);
32+
newInterval[1] = max(intervals[i][1], newInterval[1]);
33+
i++;
34+
}
35+
result.push_back(newInterval);
36+
37+
while (i < n) {
38+
result.push_back(intervals[i]);
39+
i++;
40+
}
41+
return result;
42+
}
43+
};
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/*
2+
풀이 :
3+
중위순회로 트리를 순회하면서 순서대로 vector에 담는다
4+
BST는 중위순회 시 크기 순서로 탐색되므로 vector에서 k번째인 요소를 return
5+
6+
트리 개수 : N
7+
8+
TC : O (N)
9+
모든 트리 dfs로 순회
10+
11+
SC : O (N)
12+
배열 크기는 트리 개수에 비례
13+
*/
14+
15+
#include <vector>
16+
using namespace std;
17+
/**
18+
* Definition for a binary tree node.
19+
* struct TreeNode {
20+
* int val;
21+
* TreeNode *left;
22+
* TreeNode *right;
23+
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
24+
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
25+
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
26+
* };
27+
*/
28+
class Solution {
29+
public:
30+
int kthSmallest(TreeNode* root, int k) {
31+
vector<int> v;
32+
dfs(root, v);
33+
return v[k - 1];
34+
}
35+
void dfs(TreeNode* root, vector<int>& v) {
36+
if (!root)
37+
return ;
38+
if (root->left)
39+
dfs(root->left, v);
40+
v.push_back(root->val);
41+
if (root->right)
42+
dfs(root->right, v);
43+
}
44+
};
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/*
2+
풀이 :
3+
현재 root를 기준으로 p, q가 어딨는지 판별
4+
p, q값이 모두 root 보다 작으면 왼쪽, 모두 root 보다 크면 오른쪽 노드로 이동
5+
그 외의 경우 (root 값이 p 또는 q와 같을 경우, p와 q사이에 있는 경우)에는
6+
현재 root가 LCA이므로 root 리턴
7+
8+
트리 높이 : H
9+
10+
TC : O (H)
11+
반복문이 트리 높이에 비례
12+
13+
SC : O (1)
14+
*/
15+
16+
17+
#include <algorithm>
18+
using namespace std;
19+
/**
20+
* Definition for a binary tree node.
21+
* struct TreeNode {
22+
* int val;
23+
* TreeNode *left;
24+
* TreeNode *right;
25+
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
26+
* };
27+
*/
28+
29+
class Solution {
30+
public:
31+
TreeNode* lowestCommonAncestor(TreeNode* root, TreeNode* p, TreeNode* q) {
32+
int small = min(p->val, q->val);
33+
int big = max(p->val, q->val);
34+
while (root) {
35+
if (root->val > big)
36+
root = root->left;
37+
else if (root->val < small)
38+
root = root->right;
39+
else
40+
break;
41+
}
42+
return root;
43+
}
44+
};

meeting-rooms/sungjinwi.cpp

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/*
2+
풀이 :
3+
시작 시간 기준으로 intervals 정렬 후 반복문 돌면서 옆 구간과 겹치는 구간이 있으면 false 모두 안 겹치면 true
4+
5+
intervals 개수 : N
6+
7+
TC : O (N log N)
8+
정렬 시간복잡도
9+
10+
SC : O (1)
11+
*/
12+
13+
#include <vector>
14+
#include <algorithm>
15+
16+
using namespace std;
17+
18+
/**
19+
* Definition of Interval:
20+
* class Interval {
21+
* public:
22+
* int start, end;
23+
* Interval(int start, int end) {
24+
* this->start = start;
25+
* this->end = end;
26+
* }
27+
* }
28+
*/
29+
30+
class Solution {
31+
public:
32+
/**
33+
* @param intervals: an array of meeting time intervals
34+
* @return: if a person could attend all meetings
35+
*/
36+
bool canAttendMeetings(vector<Interval> &intervals) {
37+
sort(intervals.begin(), intervals.end(), [] (Interval& a, Interval& b) {return a.start <= b.start;});
38+
for (int i = 0; i < intervals.size() - 1; i++) {
39+
if (intervals[i].end > intervals[i + 1].start)
40+
return false;
41+
}
42+
return true;
43+
}
44+
};

0 commit comments

Comments
 (0)