Leetcode: Merge sorted array
Given two lists or arrays,
nums2 as well as two integers
n which give the length of the arrays
nums1 should be modified in place.
The first idea that came to my mind was a merge-sort. Given that those initial arrays are already sorted, I could have two pointers and iterate over both arrays simulatenously, copying the values into a third one. The problem here is that the array should be modified in place. The way to achieve this in
O(2(n+m)) => O(n+m) is to iterate over the temporary array and write it back to the initial array.
The code is listed below.
Runtime: 68 ms, faster than 28.82% of Python3 online submissions for Merge Sorted Array. Memory Usage: 13.9 MB, less than 85.86% of Python3 online submissions for Merge Sorted Array.Looking at the solutions from others I saw that they simply called the
sortfunction. This is obviously less code to write but more time-consuming since the complexity is