💡
Seb's Whiteboard
  • 👨‍💻Welcome, I'm Sebastien St Vil
  • Extras
    • Gradient Descent
    • How I learned Java
    • Machine Learning by Andrew Ng
  • Projects
    • 📉Backtest Equity Trading with SMA Strategy
    • Wellington GA Lab
    • Stock analysis
    • 📈Time Series Regression-based Trading Strategy
  • Arrays & Strings
    • Best Time to Buy and Sell Stock II
    • Online Stock Span
    • Implement strStr()
    • 2Sum
    • 3Sum
    • 3Sum Closest
    • 4Sum II
    • Set Matrix Zeroes
    • Group Anagrams
    • Longest Substring Without Repeating Characters
    • Remove Duplicates from Sorted Array
    • Move Zeroes
    • Valid Sudoku
    • Rotate Image
    • First Unique Character in a String
    • Design a Circular Queue
    • Longest Common Prefix
  • Binary Tree
  • Second Minimum Node In a Binary Tree (671)
  • Design
  • LRU Cache
  • Min Stack (155)
  • Sorting & Searching
    • Merge Sorted Array (88)
    • First Bad Version
  • Math
    • Power of Three (326)
    • Power of Two (231)
    • Count Prime (204)
    • Roman to Integer (13)
    • Fizz Buzz (412)
    • Count-and-Say
  • Dynamic Programming
    • Pascal's Triangle (118)
  • Linked List
    • Copy List with Random Pointer
    • Remove Nth Node From End of List
    • Remove Duplicated from Sorted List II
  • Tips
    • Finding dups
  • Sliding Window
    • Subarray Product Less Than K
Powered by GitBook
On this page

Was this helpful?

  1. Sorting & Searching

Merge Sorted Array (88)

Given two sorted integer arrays nums1 and nums2, merge nums2 into nums1 as one sorted array.

Note:

  • The number of elements initialized in nums1 and nums2 are m and n respectively.

  • You may assume that nums1 has enough space (size that is equal to m + n) to hold additional elements from nums2.

Input:
nums1 = [5,13,17,0,0,0,0], m = 3
nums2 = [3,7,11,19],       n = 4

Approach:

We cannot iterate thorugh both array on tandem as if an entry in the second array is smaller than some entry in the first array, we would have to shift all the subsequent entries in the first array by 1, potentially rendering the worst came time to O(mn) if every entry in array2 is smaller than each entry in array1.

Since we have spare space at the end of the first array. We take advantage of this by filling the first array from its end. The last element in the result will be written to index m + n - 1. nums1 would be updated in the follwing manner

nums1 = [5,13,17,0,0,0,19,0]
nums1 = [5,13,17,0,0,17,19,0]
nums1 = [5,13,17,0,13,17,19,0]
nums1 = [5,13,17,11,13,17,19,0]
nums1 = [5,13,7,11,13,17,19,0]
nums1 = [5,5,7,11,13,17,19,0]
nums1 = [3,5,7,11,13,17,19,0]
class Solution {
    public void merge(int[] nums1, int m, int[] nums2, int n) {
        int k = m + n - 1;
        int i = m - 1, j = n - 1;
        
        while(i >= 0 && j >= 0){
            nums1[k --] = nums2[j] > nums1[i] ? nums2[j--] : nums1[i--];
        }
        while(j >= 0)
            nums1[k--] = nums2[j--];
    }
}

Time: O(m + n) Space: O(1)

PreviousMin Stack (155)NextFirst Bad Version

Last updated 4 years ago

Was this helpful?