[Leetcode] 4. Median of Two Sorted Arrays
[Leetcode] 4. Median of Two Sorted Arrays

[Leetcode] 4. Median of Two Sorted Arrays

์–ธ์–ด
Python
C
๋‚œ์ด๋„
๋‹ค์‹œ ํ’€์–ด๋ณด๊ธฐ
๋‹ค์‹œ ํ’€์–ด๋ณด๊ธฐ
์•Œ๊ณ ๋ฆฌ์ฆ˜ ์œ ํ˜•
์ž‘์„ฑ์ž
๋ฐ•์šฉ์„ฑ๋ฐ•์šฉ์„ฑ
์ƒ์„ฑ ์ผ์‹œ
2024๋…„ 06์›” 03์ผ

๐Ÿ–ฅ๏ธย ์‹œ์ž‘ํ•˜๋ฉฐ

๊ฐ„๋‹จํ•œ ๋ฐฐ์—ด ๊ด€๋ จ ๋ฌธ์ œ์ž…๋‹ˆ๋‹ค.
ย 
์ด ๋ฌธ์ œ๋ฅผ ํ•ด๊ฒฐํ•˜๊ธฐ ์œ„ํ•ด ๋จผ์ € ๋‘ ๋ฐฐ์—ด์„ ํ•˜๋‚˜์˜ ๋ฐฐ์—ด๋กœ ๋ณ‘ํ•ฉํ•˜๊ณ , ๋ณ‘ํ•ฉ๋œ ๋ฐฐ์—ด์„ ์ •๋ ฌํ•œ ๋‹ค์Œ ์ค‘์•™๊ฐ’์„ ์ฐพ๋Š” ๋ฐฉ๋ฒ•์„ ์‚ฌ์šฉํ–ˆ์Šต๋‹ˆ๋‹ค.
ย 
  • ๋ฐฐ์—ด ๋ณ‘ํ•ฉ:
    • 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
ย 
ย 

๋Œ“๊ธ€

guest