์ผ | ์ | ํ | ์ | ๋ชฉ | ๊ธ | ํ |
---|---|---|---|---|---|---|
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
- ํ์ด์ฌ๊ธฐ์ด100์
- ๋จธ์ ๋ฌ๋
- ์ฝ๋์
- ํ์ด์ฌ๊ธฐ์ด
- Microsoft
- LeetCode
- ํ์ด์ฌ์๊ณ ๋ฆฌ์ฆ
- ๋ฆฌํธ์ฝ๋
- ์์ฐ์ด์ฒ๋ฆฌ
- ๋น ๋ฐ์ดํฐ
- ๋ฐ์ดํฐ์ฌ์ด์ธ์ค
- ํ์ด์ฌ
- GenerativeAI
- Blazor
- C#
- ํฌํฌ์ธํฐ
- Azure
- TwoPointer
- nlp
- GenAI
- ํด๋ผ์ฐ๋
- ์๊ณ ๋ฆฌ์ฆ
- Python3
- ๋ฆฟ์ฝ๋
- ๊ตฌ๊ธํต๋ฉ
- gcp
- Python
- two-pointer
- ์์ฑํAI
- codeup
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 |