From 86579d903c51732e86e5e131ae4bc3c3476cf113 Mon Sep 17 00:00:00 2001 From: sungjinwi <0202wsj@gmail.com> Date: Wed, 25 Jun 2025 22:44:52 +0900 Subject: [PATCH 1/5] #231 solution --- meeting-rooms/sungjinwi.cpp | 44 +++++++++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) create mode 100644 meeting-rooms/sungjinwi.cpp diff --git a/meeting-rooms/sungjinwi.cpp b/meeting-rooms/sungjinwi.cpp new file mode 100644 index 000000000..2814c7d1d --- /dev/null +++ b/meeting-rooms/sungjinwi.cpp @@ -0,0 +1,44 @@ +/* + 풀이 : + 시작 시간 기준으로 intervals 정렬 후 반복문 돌면서 옆 구간과 겹치는 구간이 있으면 false 모두 안 겹치면 true + + intervals 개수 : N + + TC : O (N log N) + 정렬 시간복잡도 + + SC : O (1) +*/ + +#include +#include + +using namespace std; + +/** + * Definition of Interval: + * class Interval { + * public: + * int start, end; + * Interval(int start, int end) { + * this->start = start; + * this->end = end; + * } + * } + */ + +class Solution { + public: + /** + * @param intervals: an array of meeting time intervals + * @return: if a person could attend all meetings + */ + bool canAttendMeetings(vector &intervals) { + sort(intervals.begin(), intervals.end(), [] (Interval& a, Interval& b) {return a.start <= b.start;}); + for (int i = 0; i < intervals.size() - 1; i++) { + if (intervals[i].end > intervals[i + 1].start) + return false; + } + return true; + } + }; From 5de817ff8981b95493a04588b495caf6734a5853 Mon Sep 17 00:00:00 2001 From: sungjinwi <0202wsj@gmail.com> Date: Wed, 25 Jun 2025 22:48:56 +0900 Subject: [PATCH 2/5] #249 solution --- .../sungjinwi.cpp | 44 +++++++++++++++++++ 1 file changed, 44 insertions(+) create mode 100644 lowest-common-ancestor-of-a-binary-search-tree/sungjinwi.cpp diff --git a/lowest-common-ancestor-of-a-binary-search-tree/sungjinwi.cpp b/lowest-common-ancestor-of-a-binary-search-tree/sungjinwi.cpp new file mode 100644 index 000000000..5bb0175b8 --- /dev/null +++ b/lowest-common-ancestor-of-a-binary-search-tree/sungjinwi.cpp @@ -0,0 +1,44 @@ +/* + 풀이 : + 현재 root를 기준으로 p, q가 어딨는지 판별 + p, q값이 모두 root 보다 작으면 왼쪽, 모두 root 보다 크면 오른쪽 노드로 이동 + 그 외의 경우 (root 값이 p 또는 q와 같을 경우, p와 q사이에 있는 경우)에는 + 현재 root가 LCA이므로 root 리턴 + + 트리 높이 : H + + TC : O (H) + 반복문이 트리 높이에 비례 + + SC : O (1) +*/ + + +#include +using namespace std; +/** + * Definition for a binary tree node. + * struct TreeNode { + * int val; + * TreeNode *left; + * TreeNode *right; + * TreeNode(int x) : val(x), left(NULL), right(NULL) {} + * }; + */ + + class Solution { + public: + TreeNode* lowestCommonAncestor(TreeNode* root, TreeNode* p, TreeNode* q) { + int small = min(p->val, q->val); + int big = max(p->val, q->val); + while (root) { + if (root->val > big) + root = root->left; + else if (root->val < small) + root = root->right; + else + break; + } + return root; + } + }; From 05d9b79a91d90b6b4caf326d7264e48cbe6ebfb4 Mon Sep 17 00:00:00 2001 From: sungjinwi <0202wsj@gmail.com> Date: Wed, 25 Jun 2025 22:54:05 +0900 Subject: [PATCH 3/5] #252 solution --- kth-smallest-element-in-a-bst/sungjinwi.cpp | 44 +++++++++++++++++++++ 1 file changed, 44 insertions(+) create mode 100644 kth-smallest-element-in-a-bst/sungjinwi.cpp diff --git a/kth-smallest-element-in-a-bst/sungjinwi.cpp b/kth-smallest-element-in-a-bst/sungjinwi.cpp new file mode 100644 index 000000000..34dce6c76 --- /dev/null +++ b/kth-smallest-element-in-a-bst/sungjinwi.cpp @@ -0,0 +1,44 @@ +/* + 풀이 : + 중위순회로 트리를 순회하면서 순서대로 vector에 담는다 + BST는 중위순회 시 크기 순서로 탐색되므로 vector에서 k번째인 요소를 return + + 트리 개수 : N + + TC : O (N) + 모든 트리 dfs로 순회 + + SC : O (N) + 배열 크기는 트리 개수에 비례 +*/ + +#include +using namespace std; +/** + * 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) { + vector v; + dfs(root, v); + return v[k - 1]; + } + void dfs(TreeNode* root, vector& v) { + if (!root) + return ; + if (root->left) + dfs(root->left, v); + v.push_back(root->val); + if (root->right) + dfs(root->right, v); + } + }; From ecb59e7271004f83b877ec4a64287a9eb243f0a5 Mon Sep 17 00:00:00 2001 From: sungjinwi <0202wsj@gmail.com> Date: Wed, 25 Jun 2025 22:58:56 +0900 Subject: [PATCH 4/5] # 277 solution --- insert-interval/sungjinwi.cpp | 43 +++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) create mode 100644 insert-interval/sungjinwi.cpp diff --git a/insert-interval/sungjinwi.cpp b/insert-interval/sungjinwi.cpp new file mode 100644 index 000000000..951c8c58f --- /dev/null +++ b/insert-interval/sungjinwi.cpp @@ -0,0 +1,43 @@ +/* + 풀이 : + 기존 배열 중 start가 newInterval보다 빠르면서 newInterval과 겹치지 않는 요소들 result에 push + 겹치는 요소들은 newInterval에 통합 후 result에 push + 그 후 나머지 겹치지 않는 부분 result에 push + + intervals 개수 : N + + TC : O (N) + + SC : O (1) + 리턴하는 배열 외에 추가 공간 사용 X +*/ + +#include +using namespace std; + +class Solution { + public: + vector> insert(vector>& intervals, vector& newInterval) { + vector> result; + int i = 0; + int n = intervals.size(); + + while (i < n && intervals[i][1] < newInterval[0]) { + result.push_back(intervals[i]); + i++; + } + + while (i < n && intervals[i][0] <= newInterval[1]) { + newInterval[0] = min(intervals[i][0], newInterval[0]); + newInterval[1] = max(intervals[i][1], newInterval[1]); + i++; + } + result.push_back(newInterval); + + while (i < n) { + result.push_back(intervals[i]); + i++; + } + return result; + } + }; From 0e7eb6e5be67852e21cd642cd4aea006a9bddc91 Mon Sep 17 00:00:00 2001 From: sungjinwi <0202wsj@gmail.com> Date: Wed, 25 Jun 2025 23:08:08 +0900 Subject: [PATCH 5/5] #289 solution --- find-median-from-data-stream/sungjinwi.cpp | 68 ++++++++++++++++++++++ 1 file changed, 68 insertions(+) create mode 100644 find-median-from-data-stream/sungjinwi.cpp diff --git a/find-median-from-data-stream/sungjinwi.cpp b/find-median-from-data-stream/sungjinwi.cpp new file mode 100644 index 000000000..f08d11119 --- /dev/null +++ b/find-median-from-data-stream/sungjinwi.cpp @@ -0,0 +1,68 @@ +/* + 풀이 : + maxHeap과 minHeap 사용 + + heap에 담긴 총 수 : N + + addNum() + 작은 수 집합은 maxHeap, 큰 수 집합은 minHeap에 담는다 + minHeap과 maxHeap의 개수차이를 1로 유지하면서 addNum + + TC : O (log N) + 힙에 넣고 뺄 때 O (logN) 의 시간 소요 + + SC : O (N) + 총 힙 크기는 N에 비례 + + findMedian() + 더 크게 유지되는 것은 minHeap이므로 둘 의 개수가 같지 않을경우(총 홀수개) minHeap.top() 리턴 + 같을 경우 총개수가 짝수개이므로 두 힙.top()의 평균을 리턴 + + TC : O (1) + 각 힙에서 root값 확인은 O(1)의 시간 소요 + SC : O (1) +*/ + +#include +#include +using namespace std; + +class MedianFinder { + public: + priority_queue maxHeap; + priority_queue, greater> minHeap; + + MedianFinder() { + + } + + void addNum(int num) { + if (minHeap.empty() || num >= minHeap.top()) + minHeap.push(num); + else + maxHeap.push(num); + + if (minHeap.size() > maxHeap.size() + 1) { + maxHeap.push(minHeap.top()); + minHeap.pop(); + } + else if (maxHeap.size() > minHeap.size()) { + minHeap.push(maxHeap.top()); + maxHeap.pop(); + } + } + + double findMedian() { + if (maxHeap.size() == minHeap.size()) + return static_cast(maxHeap.top() + minHeap.top()) / 2; + else + return minHeap.top(); + } + }; + + /** + * Your MedianFinder object will be instantiated and called as such: + * MedianFinder* obj = new MedianFinder(); + * obj->addNum(num); + * double param_2 = obj->findMedian(); + */