-
Notifications
You must be signed in to change notification settings - Fork 6
/
reversePartial.js
59 lines (47 loc) · 1.48 KB
/
reversePartial.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
// Good morning! Here's your coding interview problem for today.
// Given a list, sort it using this method: reverse(lst, i, j), which reverses lst from i to j.
function reversePartialJS(arr, i, j) {
if (i > j) return arr;
return [...arr.slice(0, i), ...arr.slice(i, j + 1).reverse(), ...arr.slice(j + 1, arr.length)]
}
function reversePartialCopy(arr, i, j) {
if (i > j) return arr;
let swapCount = j;
const resultArr = new Array(arr.length);
for (let idx = 0; idx < arr.length; idx++) {
if (idx < i) {
resultArr[idx] = arr[idx];
} else if (idx > j) {
resultArr[idx] = arr[idx];
} else {
resultArr[idx] = arr[swapCount];
swapCount--;
}
}
return resultArr;
}
function reversePartialInPlace(arr, i, j) {
if (i > j) return arr;
function swap(arr, a, b) {
[arr[a], arr[b]] = [arr[b], arr[a]];
}
let swapCountJ = j;
let swapCountI = i;
for (let idx = 0; idx < arr.length; idx++) {
if (idx < i) {
arr[idx] = arr[idx];
} else if (idx > j) {
arr[idx] = arr[idx];
} else {
if (swapCountI < swapCountJ) {
swap(arr, swapCountJ, swapCountI);
swapCountJ--;
swapCountI++;
}
}
}
return arr;
}
console.log(reversePartialInPlace([1,2,3,4,5,6,7], 2, 5));
console.log(reversePartialJS([1,2,3,4,5,6,7], 2, 5));
console.log(reversePartialCopy([1,2,3,4,5,6,7], 2, 5));