๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
์•Œ๊ณ ๋ฆฌ์ฆ˜/LeetCode

[LeetCode] 004. Median of Two Sorted Arrays JAVA

by Bhinney 2022. 12. 22.
 

Median of Two Sorted Arrays - LeetCode

Median of Two Sorted Arrays - Given two sorted arrays nums1 and nums2 of size m and n respectively, return the median of the two sorted arrays. The overall run time complexity should be O(log (m+n)).   Example 1: Input: nums1 = [1,3], nums2 = [2] Output:

leetcode.com


๐Ÿ“๋ฌธ์ œ

ENG

Given two sorted arrays nums1 and nums2 of size m and n respectively, return the median of the two sorted arrays.

The overall run time complexity should be O(log (m+n)).
KOR

ํฌ๊ธฐ๊ฐ€ ๊ฐ๊ฐ m๊ณผ n์ธ ๋‘ ๊ฐœ์˜ ์ •๋ ฌ๋œ ๋ฐฐ์—ด nums1๊ณผ nums2๊ฐ€ ์ฃผ์–ด์ง€๋ฉด ๋‘ ๊ฐœ์˜ ์ •๋ ฌ๋œ ๋ฐฐ์—ด์˜ ์ค‘์•™๊ฐ’์„ ๋ฐ˜ํ™˜ํ•ด์•ผ ํ•œ๋‹ค.
์ „์ฒด ์‹คํ–‰ ์‹œ๊ฐ„ ๋ณต์žก๋„๋Š” O(log(m+n))์—ฌ์•ผ ํ•œ๋‹ค.

๐Ÿ“์˜ˆ์‹œ

Input: nums1 = [1,3], nums2 = [2]
Output: 2.00000
Explanation: merged array = [1,2,3] and median is 2.

 

Input: nums1 = [1,2], nums2 = [3,4]
Output: 2.50000
Explanation: merged array = [1,2,3,4] and median is (2 + 3) / 2 = 2.5.

๐Ÿ“ํ’€์ด ์„ค๋ช…

  • ๊ธธ์ด๋ฅผ ๋ณ€์ˆ˜์— ๋‹ด์•„์„œ ์‚ฌ์šฉํ•˜๊ธฐ ์œ„ํ•ด ์„ ์–ธํ•ด์ฃผ์—ˆ๋‹ค.
public static double findMedianSortedArrays(int[] nums1, int[] nums2) {

   int n = nums1.length;
   int m = nums2.length;

}
  • ์ƒˆ๋กœ์šด ๋ฐฐ์—ด์„ ๋งŒ๋“ค์–ด ์ฃผ์—ˆ๋‹ค.
  • ๊ธธ์ด๋Š” ์ž…๋ ฅ๋˜๋Š” ๋‘ ๋ฐฐ์—ด์˜ ๊ธธ์ด๋ฅผ ๋”ํ•ด ๋ฐ˜์œผ๋กœ ๋‚˜๋ˆ„๊ณ  +1์„ ํ•ด์ฃผ์—ˆ๋‹ค.
public static double findMedianSortedArrays(int[] nums1, int[] nums2) {
   ...

   int[] arr = new int[(m + n) / 2 + 1];

}
  • ์ฒซ ๋ฒˆ์งธ ๋ฐฐ์—ด์˜ ๊ธธ์ด ๋น„๊ต๋ฅผ ์œ„ํ•œ ๋ณ€์ˆ˜์™€ ๋‘ ๋ฒˆ์งธ ๋ฐฐ์—ด์˜ ๊ธธ์ด ๋น„๊ต๋ฅผ ์œ„ํ•œ ๋ณ€์ˆ˜๋ฅผ ๊ฐ๊ฐ ์„ ์–ธํ•œ ํ›„ ์ดˆ๊ธฐํ™” ํ•ด์ฃผ์—ˆ๋‹ค.
  • ๋ฐ˜๋ณต๋ฌธ์„ ๋Œ๋ฉฐ ์ˆซ์ž์˜ ํฌ๊ธฐ๋ฅผ ๋น„๊ตํ•˜์—ฌ ์ƒˆ๋กœ์šด ๋ฐฐ์—ด์— ๋„ฃ์–ด์ฃผ์—ˆ๋‹ค.
  • ์กฐ๊ฑด๋ฌธ์€ ๊ธธ์ด ๋น„๊ต๋ฅผ ์œ„ํ•œ ๊ฒƒ์ธ๋ฐ, ๋‘ ๋ฐฐ์—ด์˜ ๊ธธ์ด๊ฐ€ ๋‹ค๋ฅผ ์ˆ˜๋„ ์žˆ๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค.
  • ๋น„๊ตํ•˜์—ฌ ๋” ์ž‘์€ ์ˆ˜๋ฅผ ์ƒˆ ๋ฐฐ์—ด์— ๋„ฃ์–ด์ฃผ์—ˆ๋‹ค.
  • ๋‘ ๋ฐฐ์—ด์ด ์ •๋ ฌ๋œ ๋ฐฐ์—ด์ด๊ธฐ ๋•Œ๋ฌธ์— ๊ฐ€๋Šฅํ–ˆ๋‹ค.
public static double findMedianSortedArrays(int[] nums1, int[] nums2) {
   ...

   while (one + two < arr.length) {
      if (one == n) {
         arr[one + two] = nums2[two++];
      } else if (two == m) {
         arr[one + two] = nums1[one++];
      } else {
         arr[one + two] = nums1[one] < nums2[two] ?
            nums1[one++] : nums2[two++];
      }
   }
}
  • ๊ทธ๋ฆฌ๊ณ  ๋ฆฌํ„ด ๊ฐ’์„ ๊ณ„์‚ฐํ•ด์ฃผ์—ˆ๋‹ค.
  • ์•„๋ž˜ ์ฒ˜๋Ÿผ ๊ณ„์‚ฐ์‹์„ ์ ์–ด์ฃผ๋ฉด, ๋‘ ๋ฐฐ์—ด์˜ ํ•ฉ์ด ํ™€์ˆ˜ ์ผ ๊ฒฝ์šฐ ๊ฐ™์€ ๊ฐ’์„ ๋”ํ•œ ํ›„ ๋‚˜๋ˆ ์ฃผ๊ธฐ ๋•Œ๋ฌธ์— ์ค‘๊ฐ„๊ฐ’์„ ์ฐพ์„ ์ˆ˜ ์žˆ๊ณ  ๋‘ ๋ฐฐ์—ด์˜ ํ•ฉ์ด ์ง์ˆ˜์ผ ๊ฒฝ์šฐ ๊ฐ๊ฐ์˜ ์ค‘๊ฐ„๊ฐ’์„ ๋”ํ•ด ๋‚˜๋ˆ„์–ด ์ฃผ์–ด ์ค‘๊ฐ„๊ฐ’์„ ์ฐพ์„ ์ˆ˜ ์žˆ๋‹ค.
  • 2.0์œผ๋กœ ๋‚˜๋ˆ„์–ด์ค€ ์ด์œ ๋Š” ๋‚˜๋ˆ„๋Š” ๊ฐ’์ด double์ด๊ฒŒ ํ•˜๊ธฐ ์œ„ํ•ด์„œ๋‹ค. 
  • ๋งŒ์•ฝ 2๋กœ ๋‚˜๋ˆˆ๋‹ค๋ฉด, ๋‚˜๋ˆ„๊ธฐ ๊ณผ์ •์—์„œ ์†Œ์ˆ˜์ ์ด ๋‚ ์•„๊ฐ€๊ณ  ๊ทธ ๊ฒฐ๊ณผ ๊ฐ’์ด doubleํ˜•ํƒœ๋กœ ์ถœ๋ ฅ๋˜๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค.
public static double findMedianSortedArrays(int[] nums1, int[] nums2) {
   ...

   return (arr[(n + m) / 2] + arr[(n + m - 1) / 2]) / 2.0;
}

๐Ÿ“์ตœ์ข… ํ’€์ด

public class MedianOfTwoSortedArrays {
   public static void main(String[] args) {
      int[] arr1 = {};
      int[] arr2 = {2,4};
      int[] num1 = {1,3,7};
      int[] num2 = {2,4,5};
      System.out.println(findMedianSortedArrays(arr1, arr2));
      System.out.println(findMedianSortedArrays(num1, num2)); 
   }
   public static double findMedianSortedArrays(int[] nums1, int[] nums2) {

      int n = nums1.length;
      int m = nums2.length;

      int[] arr = new int[(m + n) / 2 + 1];

      int one = 0;
      int two = 0;

      while (one + two < arr.length) {
         if (one == n) {
            arr[one + two] = nums2[two++];
         } else if (two == m) {
            arr[one + two] = nums1[one++];
         } else {
            arr[one + two] = nums1[one] < nums2[two] ?
               nums1[one++] : nums2[two++];
         }
      }

      return (arr[(n + m) / 2] + arr[(n + m - 1) / 2]) / 2.0;
   }
}

 

'์•Œ๊ณ ๋ฆฌ์ฆ˜ > LeetCode' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๋‹ค๋ฅธ ๊ธ€

[LeetCode] 011.Container With Most Water JAVA  (0) 2022.12.26
[LeetCode] 012. Integer to Roman JAVA  (0) 2022.12.23
[LeetCode] 008. String to Integer (atoi) JAVA  (0) 2022.12.21
[LeetCode]006. Zigzag Conversion JAVA  (0) 2022.12.21
[LeetCode]007. Reverse Integer JAVA  (0) 2022.12.19

๋Œ“๊ธ€