Skip to content

Commit ffb9f5b

Browse files
authored
Merge pull request #1085 from aa601/main
[yeoju] Week 13
2 parents d4581c7 + eb7d9aa commit ffb9f5b

File tree

3 files changed

+66
-0
lines changed

3 files changed

+66
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
'''
2+
TC: O(h) (h๋Š” ํŠธ๋ฆฌ์˜ ๋†’์ด)
3+
SC: O(1)
4+
ํ’€์ด ๋ฐฉ๋ฒ•: ์ด์ง„ ํƒ์ƒ‰ ํŠธ๋ฆฌ์˜ ์„ฑ์งˆ์„ ์ด์šฉํ•ด์„œ
5+
p,q์˜ ๊ฐ’์ด ํ˜„์žฌ ๋…ธ๋“œ ์‚ฌ์ด์— ์žˆ๊ฑฐ๋‚˜ p,q์˜ ๊ฐ’ ์ค‘ ํ•˜๋‚˜๋ผ๋„ ํ˜„์žฌ ๋…ธ๋“œ์™€ ๊ฐ™์„ ๋•Œ๊นŒ์ง€ ํƒ์ƒ‰ํ•œ๋‹ค
6+
'''
7+
# Definition for a binary tree node.
8+
# class TreeNode:
9+
# def __init__(self, x):
10+
# self.val = x
11+
# self.left = None
12+
# self.right = None
13+
14+
class Solution:
15+
def lowestCommonAncestor(self, root: 'TreeNode', p: 'TreeNode', q: 'TreeNode') -> 'TreeNode':
16+
while root:
17+
if p.val < root.val and q.val < root.val:
18+
root = root.left
19+
elif p.val > root.val and q.val > root.val:
20+
root = root.right
21+
else:
22+
return root
+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
'''
2+
TC: O(n)
3+
SC: O(n)
4+
'''
5+
6+
class Solution:
7+
def maxDepth(self, root: Optional[TreeNode]) -> int:
8+
# ์žฌ๊ท€ํ•จ์ˆ˜ ํ˜ธ์ถœ ์‹œ ๋…ธ๋“œ๊ฐ€ ์กด์žฌํ•˜์ง€ ์•Š์„ ๋•Œ ํƒˆ์ถœํ•œ๋‹ค
9+
if not root:
10+
return 0
11+
# root๋ฅผ ๊ธฐ์ค€์œผ๋กœ ์™ผ์ชฝ ์„œ๋ธŒํŠธ๋ฆฌ, ์˜ค๋ฅธ์ชฝ ์„œ๋ธŒํŠธ๋ฆฌ๋ฅผ ์žฌ๊ท€ํ˜ธ์ถœ๋กœ ์Œ“์•„๋†“๊ณ  ๋ฆฌํ”„๋…ธ๋“œ๋ถ€ํ„ฐ ์„ธ๋ฉด์„œ ํŠธ๋ฆฌ์˜ ๊นŠ์ด๋ฅผ ๊ตฌํ•œ๋‹ค
12+
return max(self.maxDepth(root.left), self.maxDepth(root.right)) + 1

โ€Žmeeting-rooms/aa601.py

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
'''
2+
TC: O(nlogn)
3+
SC: O(1)
4+
ํ’€์ด ๋ฐฉ๋ฒ•: ๊ตฌ๊ฐ„ ์ค‘ ์ฒซ๋ฒˆ์งธ ์ธ์ž์— ๋Œ€ํ•ด ์ •๋ ฌํ•œ ๋’ค, ์ด์›ƒํ•œ ์ธ์ž์— ๋Œ€ํ•ด์„œ๋งŒ ๊ฒน์น˜๋Š”์ง€ ํ™•์ธํ•œ๋‹ค
5+
์‹œ๊ฐ„ ๋ณต์žก๋„๋Š” ์ •๋ ฌ์— ๋“œ๋Š” nlogn๋งŒํผ ํ•„์š”ํ•˜๋‹ค
6+
'''
7+
8+
from typing import (
9+
List,
10+
)
11+
from lintcode import (
12+
Interval,
13+
)
14+
"""
15+
Definition of Interval:
16+
class Interval(object):
17+
def __init__(self, start, end):
18+
self.start = start
19+
self.end = end
20+
"""
21+
22+
class Solution:
23+
"""
24+
@param intervals: an array of meeting time intervals
25+
@return: if a person could attend all meetings
26+
"""
27+
def can_attend_meetings(self, intervals: List[Interval]) -> bool:
28+
intervals.sort(key=lambda x: x.start)
29+
for i in range(1, len(intervals)):
30+
if intervals[i - 1].end > intervals[i].start:
31+
return False
32+
return True

0 commit comments

Comments
ย (0)