Skip to content

Commit 7ebc2ef

Browse files
committed
Add: Excercise 88. Merge Sorted Array
1 parent c3e4271 commit 7ebc2ef

File tree

3 files changed

+146
-0
lines changed

3 files changed

+146
-0
lines changed

88-merge-sorted-array/README.md

+71
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
# Merge Sorted Array
2+
3+
## Description
4+
5+
You are given two integer arrays `nums1` and `nums2`, sorted in **non-decreasing order**, and two integers `m` and `n`, representing the number of elements in `nums1` and `nums2` respectively.
6+
7+
**Merge** `nums1` and `nums2` into a single array sorted in **non-decreasing order**.
8+
9+
The final sorted array should not be returned by the function, but instead be **stored inside the array** `nums1`. To accommodate this, `nums1` has a length of `m + n`, where the first `m` elements denote the elements that should be merged, and the last `n` elements are set to `0` and should be ignored. `nums2` has a length of `n`.
10+
11+
---
12+
13+
## Examples
14+
15+
### Example 1:
16+
17+
**Input:**
18+
```
19+
nums1 = [1,2,3,0,0,0], m = 3
20+
nums2 = [2,5,6], n = 3
21+
```
22+
**Output:**
23+
```
24+
[1,2,2,3,5,6]
25+
```
26+
**Explanation:**
27+
The arrays we are merging are `[1,2,3]` and `[2,5,6]`.
28+
The result of the merge is `[1,2,2,3,5,6]` with the underlined elements coming from `nums1`.
29+
30+
---
31+
32+
### Example 2:
33+
34+
**Input:**
35+
```
36+
nums1 = [1], m = 1
37+
nums2 = [], n = 0
38+
```
39+
**Output:**
40+
```
41+
[1]
42+
```
43+
**Explanation:**
44+
The arrays we are merging are `[1]` and `[]`.
45+
The result of the merge is `[1]`.
46+
47+
---
48+
49+
### Example 3:
50+
51+
**Input:**
52+
```
53+
nums1 = [0], m = 0
54+
nums2 = [1], n = 1
55+
```
56+
**Output:**
57+
```
58+
[1]
59+
```
60+
**Explanation:**
61+
The arrays we are merging are `[]` and `[1]`.
62+
The result of the merge is `[1]`.
63+
64+
**Note:** Because `m = 0`, there are no elements in `nums1`. The `0` is only there to ensure the merge result can fit in `nums1`.
65+
66+
---
67+
68+
## Constraints
69+
70+
- `nums1.length == m + n`
71+
- `nums2.length == n`

88-merge-sorted-array/index.js

+62
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
const assert = require('assert');
2+
3+
/**
4+
* @param {number[]} nums1
5+
* @param {number} m
6+
* @param {number[]} nums2
7+
* @param {number} n
8+
* @return {void} Do not return anything, modify nums1 in-place instead.
9+
*/
10+
var merge = function (nums1, m, nums2, n) {
11+
// nums1 = [a, b, c, 0, 0, 0]
12+
// nums2 = [d, e, f]
13+
// Three pointers:
14+
// r1 (reader) = right of the relevant part of the first array (3)
15+
// r2 (reader) = right of the second array (3)
16+
// w (writter) = it will write at the end of the first array (5)
17+
let r1 = m - 1;
18+
let r2 = n - 1;
19+
20+
for (let w = m + n - 1; w >= 0; w--) {
21+
if (r1 >= 0 && r2 >= 0) {
22+
nums1[w] = nums1[r1] > nums2[r2] ? nums1[r1--] : nums2[r2--];
23+
} else if (r1 >= 0) {
24+
nums1[w] = nums1[r1--];
25+
} else if (r2 >= 0) {
26+
nums1[w] = nums2[r2--];
27+
}
28+
}
29+
};
30+
31+
function test(tt) {
32+
tt.forEach(t => {
33+
merge(t.nums1, t.m, t.nums2, t.n);
34+
assert.deepStrictEqual(t.nums1, t.expected);
35+
});
36+
}
37+
38+
const tt = [
39+
{
40+
nums1: [1, 2, 3, 0, 0, 0],
41+
m: 3,
42+
nums2: [2, 5, 6],
43+
n: 3,
44+
expected: [1, 2, 2, 3, 5, 6],
45+
},
46+
{
47+
nums1: [1],
48+
m: 1,
49+
nums2: [],
50+
n: 0,
51+
expected: [1],
52+
},
53+
{
54+
nums1: [0],
55+
m: 0,
56+
nums2: [1],
57+
n: 1,
58+
expected: [1],
59+
},
60+
]
61+
62+
test(tt);

88-merge-sorted-array/package.json

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
{
2+
"name": "88-merge-sorted-array",
3+
"version": "1.0.0",
4+
"description": "## Description",
5+
"main": "index.js",
6+
"scripts": {
7+
"start": "node index.js",
8+
"debug": "NODE_OPTIONS=--inspect-brk node index.js"
9+
},
10+
"keywords": [],
11+
"author": "",
12+
"license": "ISC"
13+
}

0 commit comments

Comments
 (0)