Skip to content

Commit 4e52bdf

Browse files
authored
Merge pull request #1342 from KwonNayeon/main
[KwonNayeon] Week 4 solutions
2 parents 134f7ea + 7e1db34 commit 4e52bdf

File tree

2 files changed

+17
-10
lines changed

2 files changed

+17
-10
lines changed

find-minimum-in-rotated-sorted-array/KwonNayeon.py

+6-10
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
11
"""
2-
Constraints:
3-
- n == nums.length
4-
- 1 <= n <= 5000
5-
- -5000 <= nums[i] <= 5000
6-
- All the integers of nums are unique.
7-
- nums is sorted and rotated between 1 and n times.
2+
Input/Output/Constraints:
3+
- Input: A rotated sorted array (e.g., [4,5,6,7,0,1,2])
4+
- Output: The minimum element in the array (e.g., 0)
5+
- Constraints: Algorithm must run in O(log n) time
86
97
Time Complexity: O(log n)
108
- 이진 탐색을 사용하므로 매 단계마다 탐색 범위가 절반으로 줄어듦
@@ -16,19 +14,17 @@
1614
1. 이진 탐색(Binary Search) 활용
1715
2. mid와 right 값을 비교하여 조건을 나눔
1816
- Case 1: nums[mid] > nums[right]
19-
- 오른쪽 부분이 정렬되어 있지 않음
2017
- 최솟값은 mid 오른쪽에 존재
2118
- left = mid + 1
2219
- Case 2: nums[mid] <= nums[right]
23-
- mid부터 right까지는 정렬되어 있음
2420
- 최솟값은 mid를 포함한 왼쪽에 존재 가능
2521
- right = mid
22+
3. Pivot이 일어난 지점의 값을 반환
2623
"""
2724

2825
class Solution:
2926
def findMin(self, nums: List[int]) -> int:
30-
left = 0
31-
right = len(nums) - 1
27+
left, right = 0, len(nums) - 1
3228

3329
while left < right:
3430
mid = (left + right) // 2

merge-two-sorted-lists/KwonNayeon.py

+11
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
2. -100 <= Node.val <= 100
55
3. list1 and list2 are sorted in non-decreasing order
66
7+
<Solution 1>
8+
79
Time Complexity: n과 m이 각각 list1과 list2의 길이를 나타낼 때, O(n + m)
810
- 각 노드를 한 번씩만 방문하기 때문
911
@@ -43,3 +45,12 @@ def mergeTwoLists(self, list1: Optional[ListNode], list2: Optional[ListNode]) ->
4345
current.next = list2
4446

4547
return result.next
48+
49+
"""
50+
<Solution 2>
51+
Time Complexity:
52+
53+
Space Complexity:
54+
55+
풀이 방법:
56+
"""

0 commit comments

Comments
 (0)