-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathproblem_180.js
40 lines (34 loc) Β· 1002 Bytes
/
problem_180.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
import Stack from './data-structures/Stack';
import Queue from './data-structures/Queue';
/**
* Interleave first half of stack with second half reversed
* @param {number[]} items
* @return {number[]}
*/
function interleaveItems(items) {
// base case
if (items.length === 0) return items;
// find midpoint of items
const midpoint = Math.floor(items.length / 2);
const stack = new Stack(items);
const queue = new Queue();
// Enqueue popped elements from stack until midpoint
while (stack.items.length !== midpoint) {
queue.enqueue(stack.pop());
}
// interleave elements until midpoint 1
let i = midpoint;
while (!queue.isEmpty()) {
if (i > 1) {
const temp = stack.pop();
stack.push(queue.dequeue());
stack.push(temp);
} else {
stack.push(queue.dequeue());
}
i -= 1;
}
return stack.items;
}
console.log(interleaveItems([1, 2, 3, 4, 5])); // [ 1, 5, 2, 4, 3 ]
console.log(interleaveItems([1, 2, 3, 4])); // [ 1, 4, 2, 3 ]