Algorithm/Online judge

[LeetCode] Sorting and Searching > Merge Sorted Array

민철킹 2021. 6. 2. 17:20

https://leetcode.com/explore/interview/card/top-interview-questions-easy/96/sorting-and-searching/587/

 

Explore - LeetCode

LeetCode Explore is the best place for everyone to start practicing and learning on LeetCode. No matter if you are a beginner or a master, there are always new topics waiting for you to explore.

leetcode.com

 


풀이

 

in-place 방식으로 문제를 해결해야하므로 슬라이싱을 사용할 수 없다.

나는 병합한 배열의 총 길이가 m + n 임을 이용해 조건을 나누고 del함수를 사용하여 쓸모없는 원소를 지우고 두 배열을 합치는 방식으로 진행하였다.

 

class Solution:
    def merge(self, nums1: List[int], m: int, nums2: List[int], n: int) -> None:
        """
        Do not return anything, modify nums1 in-place instead.
        """
        if len(nums1) > m:
            for _ in range(m, len(nums1)):
                del nums1[m]
            nums1 += nums2
            
        nums1.sort()
        
반응형

'Algorithm > Online judge' 카테고리의 다른 글

[백준] 15989번 > 1,2,3 더하기4  (0) 2021.06.06
[백준] 15486번 > 퇴사 2  (0) 2021.06.06
[백준] 1806번 > 부분합  (0) 2021.05.30
[백준] 1700번 > 멀티탭 스케줄링  (0) 2021.05.30
[백준] 14719번 > 빗물  (0) 2021.05.28