惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

博客园 - 叶小钗
云风的 BLOG
云风的 BLOG
G
Google Developers Blog
S
SegmentFault 最新的问题
罗磊的独立博客
Hugging Face - Blog
Hugging Face - Blog
美团技术团队
爱范儿
爱范儿
博客园 - 三生石上(FineUI控件)
H
Hackread – Cybersecurity News, Data Breaches, AI and More
D
DataBreaches.Net
F
Fortinet All Blogs
TaoSecurity Blog
TaoSecurity Blog
D
Docker
C
Cybersecurity and Infrastructure Security Agency CISA
K
Kaspersky official blog
宝玉的分享
宝玉的分享
腾讯CDC
Google Online Security Blog
Google Online Security Blog
Recorded Future
Recorded Future
T
The Exploit Database - CXSecurity.com
T
The Blog of Author Tim Ferriss
V
V2EX
S
Securelist
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
C
CERT Recently Published Vulnerability Notes
A
Arctic Wolf
Scott Helme
Scott Helme
L
LINUX DO - 热门话题
Y
Y Combinator Blog
P
Proofpoint News Feed
T
Tor Project blog
AWS News Blog
AWS News Blog
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
The Last Watchdog
The Last Watchdog
博客园 - 聂微东
T
Threat Research - Cisco Blogs
B
Blog
Attack and Defense Labs
Attack and Defense Labs
L
Lohrmann on Cybersecurity
C
CXSECURITY Database RSS Feed - CXSecurity.com
阮一峰的网络日志
阮一峰的网络日志
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
IT之家
IT之家
N
News and Events Feed by Topic
博客园 - 司徒正美
H
Help Net Security
C
Cisco Blogs
C
Check Point Blog
S
Secure Thoughts

Coder Gals

Solving Array Problems Largest Sum Contiguous Subarray Remembrance Day Hackathon React Admin Dashboard Coder Gals Hackathon
Merge Sorted Arrays
AlbionaHoti · 2020-01-10 · via Coder Gals

Merge Sorted Arrays

- 2 mins

88. Merge Sorted Array

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

Note:

  1. The number of elements initialized in nums1 and nums2 are m and n respectively.
  2. You may assume that nums1 has enough space (size that is greater or equal to m + n) to hold additional elements from nums2.
   Example:

   Input: 
   nums1 = [1,2,3,0,0,0], m = 3
   nums2 = [2,5,6],       n = 3

   Output: [1,2,2,3,5,6]

1. First Solution

  • Starting at the end of the first array -> nums1
    • compare the last initialized elements in each of the two arrays
    • which one is bigger we will put in the last element of the first array -> nums1[nums1.length-1]
    1. First we start by comparing 6 and 3 -> which one is bigger? 6 -> nums1[nums1.length-1] => [1, 2, 3, 0, 0, 6] 1.1 We continue to compare 5 and 3 -> which one is bigger? 5 -> nums1[nums1.length-2] => [1, 2, 3, 0, 5, 6] 1.2 We continue to compare 2 and 3 -> which one is bigger? 3 -> nums1[nums1.length-3] => [1, 2, 3, 3, 5, 6] 1.3 We continue to compare 2 and 2 -> which one is bigger? no one -> nums1[nums1.length-4] => [1, 2, 2, 3, 5, 6]
    • m is the number of the initialized variables in nums1
    • n is the number of the initialized variables in nums2
    const merge = function (nums1, m, nums2, n) {
        let first = m - 1;
        let second = n - 1;

        for (let i = m + n - 1; i >= 0; i--) {
            if (second < 0) {
                break;
            }

            if (nums1[first] > nums2[second]) {
                nums1[i] = nums1[first];
                first--;
            } else { 
                nums1[i] = nums2[second];
                second--;
            }
        }
    };

2. Second Solution

Approach:

  • Start from back of nums1 and iterate through both lists backwards, putting the larger of nums1[m] and nums2[n] into the last element of nums1 so far. Starting from the back allows us to do this in one loop.

  • Once an element is copied over, it is in it’s “final spot” in the sorted array, and will not move again.

  • The algorithm has a runtime of O(m+n) and space of O(1).

  • The latter is true because even though a variable is initialized, it is not a function of the inputs m or n.

  • Note: This could have been done without initializing ‘final’ at all, but it reads better with it.


    const merge = function (nums1, m, nums2, n) {
        let final = m + n - 1;
        m--;
        n--;

        while (m >= 0 || n >= 0) {
            // if no more nums2 then just copy remaining m elements
            if (n < 0 || nums1[m] > nums2[n]) {
                nums1[final] = nums1[m];
                m--;
            } else { // m < 0 || nums1[m] < nums2[n]
                nums1[final] = nums2[n];
                n--;
            }
            final--;
        }

        return nums1;
    };

Merge Sorted Arrays