From 2dcb9dd6fac436c5f12797a9907160ee050a6063 Mon Sep 17 00:00:00 2001 From: BangDori Date: Fri, 23 May 2025 17:25:49 +0900 Subject: [PATCH] =?UTF-8?q?[=EA=B0=95=EB=B3=91=EC=A4=80]=204?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- bangdori/4.js | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 bangdori/4.js diff --git a/bangdori/4.js b/bangdori/4.js new file mode 100644 index 0000000..782e2fa --- /dev/null +++ b/bangdori/4.js @@ -0,0 +1,32 @@ +/** + * @param {number[]} nums1 + * @param {number[]} nums2 + * @return {number} + */ +var findMedianSortedArrays = function (nums1, nums2) { + const totalSize = nums1.length + nums2.length; + const isOdd = totalSize % 2 === 1; + const answer = []; + + let i1 = 0; + let i2 = 0; + for (let count = 1; count <= totalSize; count++) { + if (answer.length === 2) continue; + + if (i1 >= nums1.length || (i2 < nums2.length && nums2[i2] <= nums1[i1])) { + i2++; + + if (count >= totalSize / 2) { + answer.push(nums2[i2 - 1]); + } + } else { + i1++; + + if (count >= totalSize / 2) { + answer.push(nums1[i1 - 1]); + } + } + } + + return isOdd ? answer[0] : (answer[0] + answer[1]) / 2; +};