
๋ฌธ์ ํด๊ฒฐ ๊ณผ์
โ๏ธ ๋๋ง์ ๋ฐฉ์์ผ๋ก ๋ฌธ์ ์ ๋ฆฌํ๊ธฐ (=์ฒด๊ณํ)
https://leetcode.com/problems/merge-sorted-array/?envType=study-plan-v2&envId=top-interview-150
Merge Sorted Array - LeetCode
Can you solve this real interview question? 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 an
leetcode.com
๋ ๋ฐฐ์ด์ merge ํ์ฌ ์ค๋ฆ์ฐจ์์ผ๋ก ์ ๋ ฌํ๋ผ.
- ์
๋ ฅ
- nums1 | arrray := nums2๊น์ง mergeํ์ฌ ์ต์ข ์ ๋ ฌํด์ผํ ๋ฐฐ์ด, | nums1 | == m + n
- m | int := nums1์์ 0์ด ์๋ ์์์ ๊ฐ์, | m | ∈ [0, 200]
- nums2 | array := nums1์ merge ๋์ด์ผํ ๋ฐฐ์ด, | nums2 | == n
- n | int := nums2์ ๊ธธ์ด, | n | ∈ [0, 200]
- ์ถ๋ ฅ
- nums1 | arrray := nums2๊น์ง mergeํ์ฌ ์ค๋ฆ์ฐจ์ ์ ๋ ฌ๋ ๋ฐฐ์ด
- ์กฐ๊ฑด
- | m + n | ∈ [1, 200]
- | nums1[i], nums2[j] | ∈ [-10^9, 10^9]
- ์ต์ข ์ ์ผ๋ก ์ ๋ ฌ๋ ๋ฐฐ์ด์ ํจ์๋ก ๋ฆฌํดํ๊ฑฐ๋ ์ถ๋ ฅํ์ง ์๋๋ค. nums1 ๋ฐฐ์ด ์์ ์ ์ฅ๋์ด ์์ด์ผ ํ๋ค.
๐ ์ด๋ค ์์ผ๋ก ์ ๊ทผํ๋๊ฐ?
๋ฉํ ๋๊ป์ 1์ฃผ ์ฐจ์๋ ํจ์จ์ ์ธ ์๊ณ ๋ฆฌ์ฆ๋ณด๋ค๋ ์ฃผ๋จน๊ตฌ๊ตฌ์์ผ๋ก๋ผ๋ ๋ฌธ์ ๋ฅผ ํ์ด๋ด๋ ๊ฒ ์ค์ํ๋ค๊ณ ํ์ จ๋ค.
๋ฐ๋ผ์ ๋จ์ํ๊ฒ nums1์ 0 ์๋ฆฌ์ nums2์ ์์๋ค์ ์ฐจ๋ก๋ก ๋์ ํ ๋ค, ์ค๋ฆ์ฐจ์ ์ ๋ ฌ์ ์๊ฐํ๋ค.
๐ ์๊ณ ๋ฆฌ์ฆ ์ ํ: ์ ๋ ฅ์ ํฌ๊ธฐ๋ฅผ ๋ฐํ์ผ๋ก
nums1์์ 0(=nums2๋ก ์ฑ์์ผ ํ ์์)์ ๊ฐ์๋ n์ด๋ค.
n๋ฒ ๋ฐ๋ณตํ์ ๋์ ์๊ฐ๋ณต์ก๋๋ O(n) ์ด๊ณ n์ ์ต๋ 200์ด๋ฏ๋ก ๋ฐ๋ณต๋ฌธ ํ๋๋ก ํด๊ฒฐํ ์ ์๋ค.
๐ ์์ฌ ์ฝ๋
for (nums1์์ 0์ด ์์๋๋ ๊ณณ๋ถํฐ, nums1 ๋๊น์ง, 1์ฉ ๋ํด) {
nums1์์ 0 ์๋ฆฌ์ nums2 ์์๋ค์ ํ๋์ฉ ๋ฃ๋๋ค.
}
nums2๋ฅผ ์ค๋ฆ์ฐจ์ ์ ๋ ฌํ๋ค.
๐ฏ ๊ตฌํ ์ฝ๋
import java.util.Arrays;
class Solution {
public void merge(int[] nums1, int m, int[] nums2, int n) {
for(int i = m; i < nums1.length; i++) {
nums1[i] = nums2[i-m];
}
Arrays.sort(nums1);
}
}
๋ด ์ฝ๋๋ฅผ ํ๊ตฌํด๋ณด๊ธฐ
โฑ ์ฑ๋ฅ ์ธก์ (์๊ฐ๋ณต์ก๋/๊ณต๊ฐ๋ณต์ก๋)
for๋ฌธ์์ i๊ฐ m๋ถํฐ nums1.length๊น์ง, ์ฆ (m + n) - m = n ๋ฒ ๋ฐ๋ณตํ๋ค.
๋ฐ๋ผ์ ์๊ฐ ๋ณต์ก๋๋ O(n)์ด๋ค.
๊ณต๊ฐ์ ์ถ๊ฐ๋ก ํ ๋น๋ฐ์ง ์์์ผ๋ฏ๋ก ๊ณต๊ฐ ๋ณต์ก๋๋ O(1)์ด๋ค.
โ๏ธ ์ฑ๋ฅ ํ๊ณ

LeetCode ๋ฌธ์ ์์๋ ์๊ฐ ๋ณต์ก๋๋ฅผ O(m + n) ๋ด๋ก ๊ตฌํํ๋ผ๊ณ ํ๋ค.
๋ฐ๋ผ์ ๋์์ง ์์ ์ฑ๋ฅ์ ๊ฐ์ ธ๊ฐ๋ค๊ณ ์๊ฐํ๋ค.
๐ก ๋ฌด์์/์ด๋ป๊ฒ ๊ฐ์ ํ ์ ์์๊น?
๋ค๋ฅธ ์ฌ๋์ ์ฝ๋๋ฅผ ํ๊ตฌํด๋ณด๊ธฐ
๐ฏ ๊ตฌํ ์ฝ๋
class Solution {
public void merge(int[] nums1, int m, int[] nums2, int n) {
// variables to work as pointers
int i = m - 1;
int j = n - 1;
int k = nums1.length - 1;
while(j >= 0){
if(i >= 0 && nums1[i] > nums2[j]){
nums1[k] = nums1[i];
k--;
i--;
} else{
nums1[k] = nums2[j];
k--;
j--;
}
}
}
}

i๋ nums1์์ ์๋ฏธ์๋(=0์ด ์๋) ์์์ ๋ง์ง๋ง์ ๊ฐ๋ฆฌํค๊ณ , j๋ nums2์ ๋ง์ง๋ง์ ๊ฐ๋ฆฌํจ๋ค.
k๋ nums1์ ๊ฐ์ฅ ๋ง์ง๋ง์ ๊ฐ๋ฆฌํจ๋ค.
nums1[i]์ nums2[j]๋ฅผ ๋น๊ตํ๋ฉด์ ๋ ํฐ ๊ฐ์ k์ ๋ฃ๋๋ค. (์ด๋ ๊ฒ ๊ตฌํํ๋ฉด ์๋์ผ๋ก ์ค๋ฆ์ฐจ์ ์ ๋ ฌ๊น์ง ๋๋ค.)
๊ทธ๋ฆฌ๊ณ ์ฌ์ฉํ ํฌ์ธํฐ(i ํน์ j)์ ๊ฐ์ 1 ๊ฐ์์ํจ๋ค.
j์ ๊ฐ์ด 0๋ณด๋ค ํฌ๊ฑฐ๋ ๊ฐ์ ๋์ ์ด๋ฅผ ๋ฐ๋ณตํ๋ค.
โฑ ์ฑ๋ฅ ์ธก์ (์๊ฐ๋ณต์ก๋/๊ณต๊ฐ๋ณต์ก๋)
nums1 ๋ฐฐ์ด์ n + m ๊ฐ์ ์์๋ฅผ ์ฝ์ ํ๋ฏ๋ก ์๊ฐ ๋ณต์ก๋๋ O(n + m)์ด๋ค.
๊ณต๊ฐ์ ์ถ๊ฐ๋ก ํ ๋น๋ฐ์ง ์์์ผ๋ฏ๋ก ๊ณต๊ฐ ๋ณต์ก๋๋ O(1)์ด๋ค.
๐ ๋ด ์ฝ๋์ ์ ์ฉํด๋ณด๊ธฐ
๋๋ ์
๊ตฌํ์ ๊ฐ๋จํ์ง๋ง ์๊ฐ ๋ณต์ก๋์ ๊ณต๊ฐ ๋ณต์ก๋๋ฅผ ๊ณ์ฐํด๋ณธ ๊ฒฝํ์ด ๋ง์ด ์์ด ๋ฏ์ค์๋ค.
์์ผ๋ก ๊ตฌํ ๋ฟ๋ง ์๋๋ผ ์ฑ๋ฅ๊น์ง ๊ณ ๋ คํ์ฌ ์ฝ๋๋ฅผ ์ง๋ ์ฐ์ต์ ํด์ผํ ๊ฒ ๊ฐ๋ค.
'๐ซ ETC > Leetcode' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
| Week 1-1. [Array / String] 189. Rotate Array (0) | 2023.08.24 |
|---|---|
| Week 1-1. [Array / String] 169. Majority Element (0) | 2023.08.23 |
| Week 1-1. [Array / String] 80. Remove Duplicates from Sorted Array II (0) | 2023.08.23 |
| Week 1-1. [Array / String] 26. Remove Duplicates from Sorted Array (0) | 2023.08.23 |
| Week 1-1. [Array / String] 27. Remove Element (0) | 2023.08.23 |