-
-
Notifications
You must be signed in to change notification settings - Fork 194
[yyyyyyyyyKim] WEEK 13 solutions #1614
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
Changes from all commits
6a10d8c
23fda39
ceb9227
1499e7d
798e93b
2d4e1c9
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
class MedianFinder: | ||
# Follow up : 두 개의 heap 사용해서 구현 가능(시간복잡도 O(log n), 추가 공부 필요함) | ||
# heap 사용하지 않고 이진 탐색 삽입으로 풀었음 | ||
|
||
def __init__(self): | ||
# 숫자 저장할 리스트 생성 | ||
self.arr = [] | ||
|
||
|
||
def addNum(self, num: int) -> None: | ||
# 숫자 추가하고 정렬하기 | ||
# self.arr.append(num) | ||
# self.arr.sort() -> 시간초과(시간복잡도 O(n log n)) | ||
# 이진탐색삽입(시간복잡도 O(n)) | ||
bisect.insort(self.arr, num) | ||
|
||
def findMedian(self) -> float: | ||
# 길이가 홀수면 가운데 값 리턴 | ||
if len(self.arr)%2 == 1: | ||
return self.arr[len(self.arr)//2] | ||
# 길이가 짝수면 가운데 두 수의 평균 리턴 | ||
return (self.arr[len(self.arr)//2-1] + self.arr[len(self.arr)//2]) / 2 | ||
|
||
# Your MedianFinder object will be instantiated and called as such: | ||
# obj = MedianFinder() | ||
# obj.addNum(num) | ||
# param_2 = obj.findMedian() |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
class Solution: | ||
def insert(self, intervals: List[List[int]], newInterval: List[int]) -> List[List[int]]: | ||
|
||
# 시간복잡도 O(n log n), 공간복잡도 O(n) | ||
|
||
# newInterval 추가하고 정렬 | ||
intervals.append(newInterval) | ||
intervals.sort() | ||
|
||
answer = [intervals[0]] | ||
|
||
for i in range(1, len(intervals)): | ||
prev = answer[-1] | ||
curr = intervals[i] | ||
|
||
# 병합하기 | ||
# answer의 끝 값보다 현재의 시작값이 더 작으면 겹치는 것 -> 현재의 끝값과 answer의 끝값 중 더 큰 값으로 병합 | ||
if curr[0] <= prev[1]: | ||
prev[1] = max(prev[1], curr[1]) | ||
|
||
# 겹치지 않으면 현재값을 answer에 추가 | ||
else: | ||
answer.append(curr) | ||
|
||
return answer |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
# Definition for a binary tree node. | ||
# class TreeNode: | ||
# def __init__(self, val=0, left=None, right=None): | ||
# self.val = val | ||
# self.left = left | ||
# self.right = right | ||
class Solution: | ||
def kthSmallest(self, root: Optional[TreeNode], k: int) -> int: | ||
|
||
# DFS | ||
# 시간복잡도 최악 O(n)/ 공간복잡도 O(log n) ~ O(n) (재귀깊이) | ||
self.count = 0 | ||
self.answer = None | ||
|
||
def dfs(node): | ||
if not node: | ||
return | ||
|
||
# 가장 작은 값부터(왼쪽서브트리) 재귀로 탐색 | ||
dfs(node.left) | ||
|
||
self.count += 1 | ||
|
||
# k번째 작은 값 찾으면 바로 종료하기 | ||
if self.count == k: | ||
self.answer = node.val | ||
return | ||
|
||
# 오른쪽 서브트리 탐색 | ||
dfs(node.right) | ||
|
||
dfs(root) | ||
|
||
return self.answer |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
# Definition for a binary tree node. | ||
# class TreeNode: | ||
# def __init__(self, x): | ||
# self.val = x | ||
# self.left = None | ||
# self.right = None | ||
|
||
class Solution: | ||
def lowestCommonAncestor(self, root: 'TreeNode', p: 'TreeNode', q: 'TreeNode') -> 'TreeNode': | ||
|
||
# BST(이진 탐색 트리) 특성 활용해서 밸류값으로 LCA 찾기 | ||
# 시간복잡도 O(log n)/ 최악O(n), 공간복잡도 O(1) | ||
while root: | ||
# p, q가 둘 다 root보다 작으면, LCA는 왼쪽 서브트리에 있음 | ||
if p.val < root.val and q.val < root.val: | ||
root = root.left | ||
# p, q가 둘 다 root보다 크면, LCA는 오른쪽 서브트리에 있음 | ||
elif p.val > root.val and q.val > root.val: | ||
root = root.right | ||
# p, q가 root의 양쪽에 나눠져 있는 경우 -> root가 LCA | ||
else: | ||
return root |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
from typing import ( | ||
List, | ||
) | ||
from lintcode import ( | ||
Interval, | ||
) | ||
|
||
""" | ||
Definition of Interval: | ||
class Interval(object): | ||
def __init__(self, start, end): | ||
self.start = start | ||
self.end = end | ||
""" | ||
|
||
class Solution: | ||
""" | ||
@param intervals: an array of meeting time intervals | ||
@return: if a person could attend all meetings | ||
""" | ||
def can_attend_meetings(self, intervals: List[Interval]) -> bool: | ||
|
||
# 시간복잡도(O(n^2)), 공간복잡도 O(1) | ||
# 시작점을 기준으로 정렬(선택정렬) | ||
for i in range(len(intervals)): | ||
idx = i | ||
for j in range(i+1,len(intervals)): | ||
if intervals[j].start < intervals[idx].start: | ||
idx = j | ||
if idx != i: | ||
intervals[i], intervals[idx] = intervals[idx], intervals[i] | ||
Comment on lines
+23
to
+31
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 내장 함수 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 저는 LintCode에 있는 문제로 풀었는데, LintCode에는 intervals가 리스트가 아닌 클래스 객체라서 sort()로는 정렬이 안 된다고 생각했습니다. 그래서 속성에 직접 접근해서 정렬했습니다! |
||
|
||
# 겹치는 회의 있는지 확인 | ||
for i in range(1, len(intervals)): | ||
if intervals[i].start < intervals[i-1].end: | ||
return False | ||
|
||
return True |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
제가 사용하는 자바스크립트에는 힙 같은 자료구조가 내장되어 있지 않아 힙을 직접 구현해야 해서 막막했는데, 이진 탐색 삽입으로 풀이하신 거 보고 저도 이진 탐색으로 풀 수 있었어요..! 감사합니다 👍