알고리즘
leetcode 88 - Merge sorted Array
Jaymyong66
2023. 5. 28. 20:36
https://leetcode.com/problems/merge-sorted-array/
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
O(m+n)의 복잡도...
알고리즘 과목 들었던 개념을 다시 찾아보았다
class Solution:
def merge(self, nums1: List[int], m: int, nums2: List[int], n: int) -> None:
A = nums1[:]
L_idx, R_idx = 0,0
total_idx = m+n
for i in range(total_idx):
if L_idx == m:
nums1[i] = nums2[R_idx]
R_idx += 1
elif R_idx == n:
nums1[i] = A[L_idx]
L_idx += 1
elif L_idx < m and A[L_idx] <= nums2[R_idx]:
nums1[i] = A[L_idx]
L_idx += 1
elif R_idx < n and A[L_idx] > nums2[R_idx]:
nums1[i] = nums2[R_idx]
R_idx += 1