๐ฅ๏ธย ์์ํ๋ฉฐ
๊ฐ๋จํ ๋ฐฐ์ด ๊ด๋ จ ๋ฌธ์ ์
๋๋ค.
ย
์ด ๋ฌธ์ ๋ฅผ ํด๊ฒฐํ๊ธฐ ์ํด ๋จผ์ ๋ ๋ฐฐ์ด์ ํ๋์ ๋ฐฐ์ด๋ก ๋ณํฉํ๊ณ , ๋ณํฉ๋ ๋ฐฐ์ด์ ์ ๋ ฌํ ๋ค์ ์ค์๊ฐ์ ์ฐพ๋ ๋ฐฉ๋ฒ์ ์ฌ์ฉํ์ต๋๋ค.
ย
- ๋ฐฐ์ด ๋ณํฉ:
nums1๊ณผnums2๋ฅผ ๋ํ์ฌ ํ๋์ ๋ฐฐ์ด๋ก ๋ณํฉํฉ๋๋ค.sorted()ํจ์๋ฅผ ์ฌ์ฉํ์ฌ ๋ณํฉ๋ ๋ฐฐ์ด์ ์ ๋ ฌํฉ๋๋ค.
- ์ค์๊ฐ ์ฐพ๊ธฐ:
- ๋ณํฉ๋ ๋ฐฐ์ด์ ๊ธธ์ด๋ฅผ ๋ณ์
N์ ์ ์ฅํฉ๋๋ค. - ๋ฐฐ์ด์ ์ค๊ฐ ์ธ๋ฑ์ค๋ฅผ ๋ณ์
mid์ ์ ์ฅํฉ๋๋ค. - ๋ฐฐ์ด์ ๊ธธ์ด๊ฐ ํ์์ธ ๊ฒฝ์ฐ ์ค๊ฐ ์ธ๋ฑ์ค์ ํด๋นํ๋ ๊ฐ์ ๋ฐํํฉ๋๋ค.
- ๋ฐฐ์ด์ ๊ธธ์ด๊ฐ ์ง์์ธ ๊ฒฝ์ฐ ์ค๊ฐ ๋ ๊ฐ์ ํ๊ท ์ ๋ฐํํฉ๋๋ค.
ย
โ๏ธย Python
from typing import List class Solution: def findMedianSortedArrays(self, nums1: List[int], nums2: List[int]) -> float: merged_arr = sorted(nums1 + nums2) N = len(merged_arr) mid = N // 2 if N % 2 == 1: return merged_arr[mid] else: return (merged_arr[mid - 1] + merged_arr[mid]) / 2
ย
ย
![[Leetcode] 4. Median of Two Sorted Arrays](https://reo91004.notion.site/image/https%3A%2F%2Fprod-files-secure.s3.us-west-2.amazonaws.com%2Fb4dac9d0-810c-47c4-800c-0ca10b8d0529%2Fde29f40a-05a3-4aec-b112-7e56072966a0%2FLeetCode_Logo_Black.png?table=block&id=8d95cca2-70ff-4dcf-bad8-e87ce6c82e0c&cache=v2)
![[Leetcode] 4. Median of Two Sorted Arrays](https://reo91004.notion.site/image/https%3A%2F%2Fprod-files-secure.s3.us-west-2.amazonaws.com%2Fb4dac9d0-810c-47c4-800c-0ca10b8d0529%2F45a3427e-47fb-412a-a090-3bf6d1afdb04%2Fleetcode.png?table=block&id=8d95cca2-70ff-4dcf-bad8-e87ce6c82e0c&cache=v2)


๋๊ธ