Prerequisite: Deno v2
deno run https://gist.github.com/jihchi/49f89035b39445e99e2a7473e754665a/raw/solve.tsPrerequisite: Deno v2
deno run https://gist.github.com/jihchi/49f89035b39445e99e2a7473e754665a/raw/solve.ts| // three-pointer and merging from the end | |
| function merge(a: number[], b: number[]): number[] { | |
| let i = a.length - b.length - 1; // point to last "real" element of `a` | |
| let j = b.length - 1; // point to last element of `b` | |
| let k = a.length - 1; // point to last element of `a` | |
| while (j >= 0) { | |
| if (a[i] > b[j]) { | |
| a[k] = a[i]; | |
| i--; | |
| } else { | |
| a[k] = b[j]; | |
| j--; | |
| } | |
| k--; | |
| } | |
| return a; | |
| } | |
| console.log(merge([1, 3, 5, 0, 0, 0], [2, 4, 6])); | |
| // output: [1, 2, 3, 4, 5, 6] |