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

推荐订阅源

W
WeLiveSecurity
T
The Exploit Database - CXSecurity.com
C
CXSECURITY Database RSS Feed - CXSecurity.com
S
Security @ Cisco Blogs
T
Threat Research - Cisco Blogs
TaoSecurity Blog
TaoSecurity Blog
Recent Commits to openclaw:main
Recent Commits to openclaw:main
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
腾讯CDC
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
T
The Blog of Author Tim Ferriss
Microsoft Azure Blog
Microsoft Azure Blog
罗磊的独立博客
F
Full Disclosure
博客园 - 【当耐特】
C
CERT Recently Published Vulnerability Notes
Engineering at Meta
Engineering at Meta
Application and Cybersecurity Blog
Application and Cybersecurity Blog
T
Threatpost
I
Intezer
V2EX - 技术
V2EX - 技术
H
Hackread – Cybersecurity News, Data Breaches, AI and More
The Hacker News
The Hacker News
小众软件
小众软件
Google DeepMind News
Google DeepMind News
T
Tailwind CSS Blog
D
Darknet – Hacking Tools, Hacker News & Cyber Security
B
Blog RSS Feed
Microsoft Security Blog
Microsoft Security Blog
N
News | PayPal Newsroom
MyScale Blog
MyScale Blog
AI
AI
Vercel News
Vercel News
Spread Privacy
Spread Privacy
美团技术团队
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
The GitHub Blog
The GitHub Blog
V
Vulnerabilities – Threatpost
Schneier on Security
Schneier on Security
Cyberwarzone
Cyberwarzone
G
GRAHAM CLULEY
Help Net Security
Help Net Security
Hacker News: Ask HN
Hacker News: Ask HN
Google DeepMind News
Google DeepMind News
MongoDB | Blog
MongoDB | Blog
L
LINUX DO - 热门话题
U
Unit 42
L
LangChain Blog
Recent Announcements
Recent Announcements

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