일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | ||||||
2 | 3 | 4 | 5 | 6 | 7 | 8 |
9 | 10 | 11 | 12 | 13 | 14 | 15 |
16 | 17 | 18 | 19 | 20 | 21 | 22 |
23 | 24 | 25 | 26 | 27 | 28 |
Tags
- English
- 쓰릴오브파이트
- 사이드
- 파비최
- Writing
- 개발자
- 링피트
- 리얼 클래스
- 30분
- 매일
- 만화도
- 월간
- 10분
- 잡생각
- 스탭퍼
- 3줄정리
- FIT XR
- 읽기
- Daily Challenge
- 뭐든
- 운동
- leetcode
- 프로젝트
- realclass
- Problem Solving
- 영어원서읽기
- 화상영어
- 미드시청
- 괜찮음
- 영어공부
Archives
- Today
- Total
파비의 매일매일 공부기록
2023.09.21 Today's Challenge 본문
https://leetcode.com/problems/median-of-two-sorted-arrays/
LeetCode - The World's Leading Online Programming Learning Platform
Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview.
leetcode.com
이진탐색의 변형 문제.
class Solution:
def findMedianSortedArrays(self, nums1: List[int], nums2: List[int]) -> float:
if len(nums1) > len(nums2):
return self.findMedianSortedArrays(nums2, nums1)
m, n = len(nums1), len(nums2)
left, right = 0, m
while left <= right:
partitionA = (left + right) // 2
partitionB = (m + n + 1) // 2 - partitionA
maxLeftA = float('-inf') if partitionA == 0 else nums1[partitionA - 1]
minRightA = float('inf') if partitionA == m else nums1[partitionA]
maxLeftB = float('-inf') if partitionB == 0 else nums2[partitionB - 1]
minRightB = float('inf') if partitionB == n else nums2[partitionB]
if maxLeftA <= minRightB and maxLeftB <= minRightA:
if (m + n) % 2 == 0:
return (max(maxLeftA, maxLeftB) + min(minRightA, minRightB)) / 2
else:
return max(maxLeftA, maxLeftB)
elif maxLeftA > minRightB:
right = partitionA - 1
else:
left = partitionA + 1
반응형
'Problem Solving > LeetCode' 카테고리의 다른 글
2023.09.23 Today's Challenge (0) | 2023.09.23 |
---|---|
2023.09.22 Today's Challenge (0) | 2023.09.22 |
2023.09.20 Today's Challenge (0) | 2023.09.20 |
2023.09.19 Today's Challenge (0) | 2023.09.19 |
2023.09.18 Today's Challenge (0) | 2023.09.18 |
Comments