개발일지/TIL

[leetcode] 88. Merge Sorted Array

JangKroed 2023. 8. 23. 22:01
728x90
반응형

Merge Sorted Array

문제

You are given two integer arrays nums1 and nums2, sorted in non-decreasing order, and two integers m and n, representing the number of elements in nums1 and nums2 respectively.

Merge nums1 and nums2 into a single array sorted in non-decreasing order.

The final sorted array should not be returned by the function, but instead be stored inside the array nums1. To accommodate this, nums1 has a length of m + n, where the first m elements denote the elements that should be merged, and the last n elements are set to 0 and should be ignored. nums2 has a length of n.

 

예시

Example 1:

Input: nums1 = [1,2,3,0,0,0], m = 3, nums2 = [2,5,6], n = 3
Output: [1,2,2,3,5,6]
Explanation: The arrays we are merging are [1,2,3] and [2,5,6].
The result of the merge is [1,2,2,3,5,6] with the underlined elements coming from nums1.

Example 2:

Input: nums1 = [1], m = 1, nums2 = [], n = 0
Output: [1]
Explanation: The arrays we are merging are [1] and [].
The result of the merge is [1].

Example 3:

Input: nums1 = [0], m = 0, nums2 = [1], n = 1
Output: [1]
Explanation: The arrays we are merging are [] and [1].
The result of the merge is [1].
Note that because m = 0, there are no elements in nums1. The 0 is only there to ensure the merge result can fit in nums1.

 

조건

Constraints:

  • nums1.length == m + n
  • nums2.length == n
  • 0 <= m, n <= 200
  • 1 <= m + n <= 200
  • -109 <= nums1[i], nums2[j] <= 109

 

풀이 - 1

우선 nums1이 const로 선언되어있어서 그런지 배열 자체를 재할당하는 코드는 적용이 안되서 처음부터 많이 헤멘것 같다.

배열에 배열을 재할당을 하게되면 메모리 참조값이 바뀌어서 그런걸까 ?

/**
 Do not return anything, modify nums1 in-place instead.
 */
function merge(nums1: number[], m: number, nums2: number[], n: number): void {
  let idx = 0;
  for (let i = 0; i < m + n; i++) {
    if (nums1[i] > 0 && i < m) {
      idx++;
    } else if (i >= n) {
      const targetIdx = idx - n > 0 ? idx - n : 0;
      nums1[idx] = nums2[targetIdx];
      idx++;
    } else {
        m++;
    }
  }
  nums1.sort()
}

예제만 통과하는 수준이다. 

실제 submit 했을때 nums1 = [0,0,0,0,0], m = 0, nums2 = [1,2,3,4,5], n = 5의 케이스를 통과하지 못하고 [1,1,1,1,1]이 나왔다.

 

풀이 - 2

아무리 생각해도 반복문 한번만 돌면서 문제를 풀려면 두 배열의 각각 인덱스를 변수로 할당하여 사용하는 방법을 써야할것 같았다.

/**
 Do not return anything, modify nums1 in-place instead.
 */
function merge(nums1: number[], m: number, nums2: number[], n: number): void {
  let num1Idx = 0;
  let num2Idx = 0;

  for (let i = 0; i < m + n; i++) {
    if (nums1[num1Idx] && m > 0) {
      nums1[num1Idx] = nums1[num1Idx];
      num1Idx++;
    } else if (nums2[num2Idx] && n > 0) {
      nums1[num1Idx] = nums2[num2Idx];
      num1Idx++;
      num2Idx++;
    }
  }

  nums1.sort()
}

조건을 자세하게 살펴보지 않아  nums1 = [-1,0,1,1,0,0,0,0,0], m = 4, nums2 = [-1,0,2,2,3], n = 5의 케이스를 통과하지 못했다.

 

풀이 - 3

될거라고는 생각안했지만 배열안에 있는 요소가 0이 아닐경우의 조건을 추가해주었다.

/**
 Do not return anything, modify nums1 in-place instead.
 */
function merge(nums1: number[], m: number, nums2: number[], n: number): void {
  let num1Idx = 0;
  let num2Idx = 0;

  for (let i = 0; i < m + n; i++) {
    if (nums1[num1Idx] !== 0 && m > 0) {
      nums1[num1Idx] = nums1[num1Idx];
      num1Idx++;
    } else if (nums2[num2Idx] !== 0 && n > 0) {
      nums1[num1Idx] = nums2[num2Idx];
      num1Idx++;
      num2Idx++;
    }
  }

  nums1.sort()
}
 

nums1 = [-1,0,0,3,3,3,0,0,0], m = 6, nums2 = [1,2,2], n = 3의 케이스를 통과하지 못했다.

이렇게 가다간 모든 테스트 케이스가 나올거같아 두려워진다.

 

풀이 - 4

하나씩 고치다보면 언젠가는 풀리겠지 ...

/**
 Do not return anything, modify nums1 in-place instead.
 */
function merge(nums1: number[], m: number, nums2: number[], n: number): void {
  let num1Idx = 0;
  let num2Idx = 0;

  for (let i = 0; i < m + n; i++) {
    if (nums1[num1Idx] !== 0 && m > 0) {
      nums1[num1Idx] = nums1[num1Idx];
      num1Idx++;
    } else if (nums2[num2Idx] !== 0 && n > 0) {
      nums1[num1Idx] = nums2[num2Idx] ? nums2[num2Idx] : 0;
      num1Idx++;
      num2Idx++;
    }
  }

  nums1.sort()
}

nums1 = [-1,0,1,1,0,0,0,0,0], m = 4, nums2 = [-1,0,2,2,3], n = 5의 케이스를 통과하지 못했다.

정답은 [-1,-1,0,0,1,1,2,2,3]인데 [-1,-1,0,0,0,0,0,1,1]로 나온것으로 보아 삼항에 들어간 조건문이 잘못된거같다.

 

풀이 - 5

나.. 바본가 ...?

간단한 문제를 너무 어렵게 풀려고 했던것 같다.

/**
 Do not return anything, modify nums1 in-place instead.
 */
function merge(nums1: number[], m: number, nums2: number[], n: number): void {
  let idx = 0;

  for (let i = m; i < m + n; i++) {
    nums1[i] = nums2[idx];
    idx++;
  }

  nums1.sort()
}

nums1 = [-10,-10,-9,-9,-9,-8,-8,-7,-7,-7,-6,-6,-6,-6,-6,-6,-6,-5,-5,-5,-4,-4,-4,-3,-3,-2,-2,-1,-1,0,1,1,1,2,2,2,3,3,3,4,5,5,6,6,6,6,7,7,7,7,8,9,9,9,9,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],

m = 55,

nums2 = [-10,-10,-9,-9,-9,-9,-8,-8,-8,-8,-8,-7,-7,-7,-7,-7,-7,-7,-7,-6,-6,-6,-6,-5,-5,-5,-5,-5,-4,-4,-4,-4,-4,-3,-3,-3,-2,-2,-2,-2,-2,-2,-2,-1,-1,-1,0,0,0,0,0,1,1,1,2,2,2,2,2,2,2,2,3,3,3,3,4,4,4,4,4,4,4,5,5,5,5,5,5,6,6,6,6,6,7,7,7,7,7,7,7,8,8,8,8,9,9,9,9],

n = 99의 케이스를 실패하였는데, 10의 자리수가 있는데 그냥 sort를 사용해서 틀린문제로 보인다.

 

풀이 - 6

/**
 Do not return anything, modify nums1 in-place instead.
 */
function merge(nums1: number[], m: number, nums2: number[], n: number): void {
  let idx = 0;

  for (let i = m; i < m + n; i++) {
    nums1[i] = nums2[idx];
    idx++;
  }

  nums1.sort((a,b) => a - b)
}

성공 !

728x90
반응형