์ผ | ์ | ํ | ์ | ๋ชฉ | ๊ธ | ํ |
---|---|---|---|---|---|---|
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 | 29 | 30 |
Tags
- ์๊ณ ๋ฆฌ์ฆ
- SQL
- LeetCode
- ์ฝ๋์
- stratascratch
- Python
- ํ์ด์ฌ์๊ณ ๋ฆฌ์ฆ
- Microsoft
- slidingwindow
- medium
- GenAI
- heap
- dfs
- two-pointer
- ์์ฑํAI
- ๊ตฌ๊ธํต๋ฉ
- codeup
- nlp
- ๋ฆฌํธ์ฝ๋
- ํ์ด์ฌ๊ธฐ์ด100์
- ๋ํธ์ฝ๋
- ์ฌ๋ผ์ด๋ฉ์๋์ฐ
- Python3
- ์์ฐ์ด์ฒ๋ฆฌ
- ํฌํฌ์ธํฐ
- GenerativeAI
- ํ์ด์ฌ
- ๋ฆฟ์ฝ๋
- gcp
- sql์ฝํ
Archives
- Today
- Total
Tech for good
[Leetcode/Binary Search] 4. Median of Two Sorted Arrays ๋ณธ๋ฌธ
IT/Computer Science
[Leetcode/Binary Search] 4. Median of Two Sorted Arrays
Diana Kang 2025. 2. 17. 00:55https://leetcode.com/problems/median-of-two-sorted-arrays/description/

๐น ๋ฌธ์ ํ์ด ์ ๊ทผ
์ด ๋ฌธ์ ๋ ๋ ๊ฐ์ ์ ๋ ฌ๋ ๋ฐฐ์ด(nums1, nums2)์ด ์ฃผ์ด์ง ๋, ๋ณํฉ ํ ์ค์๊ฐ(median)์ ์ฐพ๋ ๋ฌธ์ ์ด๋ค.
โ ์ต์ ํ ๋ฐฉ๋ฒ (์ด์ง ํ์ ํ์ฉ)
- ์ด์ง ํ์(Binary Search)์ ์ฌ์ฉํ์ฌ ํ ๋ฐฐ์ด์ ์ ๋ฐ์ผ๋ก ๋ถํ ํ๋ฉด์ ๋ต์ ์ฐพ์.
- ์๊ฐ ๋ณต์ก๋: O(log(min(m, n)))
class Solution:
def findMedianSortedArrays(self, nums1: List[int], nums2: List[int]) -> float:
# nums1์ด ํญ์ ์งง์ ๋ฐฐ์ด์ด ๋๋๋ก ๋ณด์ฅ
if len(nums1) > len(nums2):
nums1, nums2 = nums2, nums1
x, y = len(nums1), len(nums2)
low, high = 0, x
while low <= high:
partitionX = (low + high) // 2
partitionY = (x + y + 1) // 2 - partitionX
# ์ผ์ชฝ ํํฐ์
์ ์ต๋๊ฐ
maxX = float('-inf') if partitionX == 0 else nums1[partitionX - 1]
maxY = float('-inf') if partitionY == 0 else nums2[partitionY - 1]
# ์ค๋ฅธ์ชฝ ํํฐ์
์ ์ต์๊ฐ
minX = float('inf') if partitionX == x else nums1[partitionX]
minY = float('inf') if partitionY == y else nums2[partitionY]
if maxX <= minY and maxY <= minX:
# ๋ฐฐ์ด์ ์ ์ฒด ๊ธธ์ด๊ฐ ์ง์์ธ ๊ฒฝ์ฐ
if (x + y) % 2 == 0:
return (max(maxX, maxY) + min(minX, minY)) / 2
else:
return max(maxX, maxY)
elif maxX > minY:
high = partitionX - 1 # ์ผ์ชฝ์ผ๋ก ์ด๋
else:
low = partitionX + 1 # ์ค๋ฅธ์ชฝ์ผ๋ก ์ด๋
๐น ์๊ณ ๋ฆฌ์ฆ ์ค๋ช (Binary Search)
- nums1์ ํญ์ ์งง์ ๋ฐฐ์ด๋ก ์ค์
- nums1์ด nums2๋ณด๋ค ๊ธธ๋ฉด swap โ ์ด์ง ํ์ ๋ฒ์๋ฅผ ์ต์ํ.
- ์ด์ง ํ์ ์ํ
- partitionX: nums1์ ์ ๋ฐ์ผ๋ก ๋๋๋ ์์น.
- partitionY: nums2๋ฅผ ๋๋๋ ์์น ((x + y + 1) // 2 - partitionX).
- ์ผ์ชฝ/์ค๋ฅธ์ชฝ ํํฐ์
๊ฐ ๊ตฌํ๊ธฐ
- maxX: nums1 ์ผ์ชฝ ๋ถ๋ถ ์ต๋๊ฐ.
- maxY: nums2 ์ผ์ชฝ ๋ถ๋ถ ์ต๋๊ฐ.
- minX: nums1 ์ค๋ฅธ์ชฝ ๋ถ๋ถ ์ต์๊ฐ.
- minY: nums2 ์ค๋ฅธ์ชฝ ๋ถ๋ถ ์ต์๊ฐ.
- ์กฐ๊ฑด ๊ฒ์ฌ
- maxX โค minY AND maxY โค minX โ ์ค์๊ฐ ์ฐพ์.
- (x + y) % 2 == 0: ์ง์ ๊ฐ โ (max(left) + min(right)) / 2
- ํ์ ๊ฐ โ max(left)
- maxX > minY โ high = partitionX - 1 (์ผ์ชฝ ์ด๋).
- maxY > minX โ low = partitionX + 1 (์ค๋ฅธ์ชฝ ์ด๋).
๐น ์๊ฐ ๋ณต์ก๋ ๋ถ์
- ์ด์ง ํ์ ์ฌ์ฉ โ O(log(min(m, n)))
- ์ต์ข ์๊ฐ ๋ณต์ก๋: O(log(min(m, n)))
- ๊ณต๊ฐ ๋ณต์ก๋: O(1) (์ถ๊ฐ ๊ณต๊ฐ ์ฌ์ฉ ์์)
'IT > Computer Science' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[Leetcode/Greedy] 1455. Check If a Word Occurs As a Prefix of Any Word in a Sentence (0) | 2025.02.17 |
---|---|
[Leetcode/Backtracking] 465. Optimal Account Balancing (0) | 2025.02.17 |
[Leetcode/Hash Map+Sorting] 3167. Better Compression of String (0) | 2025.02.17 |
[Leetcode/Two Pointer] 75. Sort Colors (0) | 2025.02.16 |
[Leetcode/Two Pointer] 283. Move Zeroes (0) | 2025.02.16 |