algo-part2

Part 2

Circular List

Judge a linked list is circular or not

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
function circular(list) {
let slow = list.getFirst();
let fast = list.getFirst();

while (fast.next && fast.next.next) {
slow = slow.next;
fast = fast.next.next;

if (slow === fast) {
return true;
}
}

return false;
}

Tree

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
class Node {
constructor(data) {
this.data = data;
this.children = [];
}

add(data) {
this.children.push(new Node(data));
}

remove(data) {
this.children = this.children.filter(node => {
return node.data !== data;
});
}
}

class Tree {
constructor() {
this.root = null;
}
// 广度优先
traverseBF(fn) {
// 初始化一个数组
const arr = [this.root];
// 循环判断
while (arr.length) {
// 取得头部的元素
const node = arr.shift();
// 把头部元素的children push到array里
arr.push(...node.children);
fn(node);
}
}
// 深度优先
traverseDF(fn) {
const arr = [this.root];
while (arr.length) {
const node = arr.shift();

arr.unshift(...node.children);
fn(node);
}
}
}

module.exports = { Tree, Node };

Tree level width

1
2
3
4
5
6
7
8
9
10
11
12
Given the root node of a tree, return
an array where each element is the width
of the tree at each level.
--- Example
Given:
0
/ | \
1 2 3
| |
4 5
Answer: [1, 3, 2]

solution:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
function levelWidth(root) {
const arr = [root, 's'];
const counters = [0];

while (arr.length > 1) {
const node = arr.shift();

if (node === 's') {
counters.push(0);
arr.push('s');
} else {
arr.push(...node.children);
counters[counters.length - 1]++;
}
}

return counters;
}

Events

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
// --- Directions
// Create an 'eventing' library out of the
// Events class. The Events class should
// have methods 'on', 'trigger', and 'off'.

class Events {
constructor() {
this.events = {};
}

// Register an event handler
on(eventName, callback) {
if (this.events[eventName]) {
this.events[eventName].push(callback);
} else {
this.events[eventName] = [callback];
}
}

// Trigger all callbacks associated
// with a given eventName
trigger(eventName) {
if (this.events[eventName]) {
for (let cb of this.events[eventName]) {
cb();
}
}
}

// Remove all event handlers associated
// with the given eventName
off(eventName) {
delete this.events[eventName];
}
}

Binary Search Tree

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
// 1) Implement the Node class to create
// a binary search tree. The constructor
// should initialize values 'data', 'left',
// and 'right'.
// 2) Implement the 'insert' method for the
// Node class. Insert should accept an argument
// 'data', then create an insert a new node
// at the appropriate location in the tree.
// 3) Implement the 'contains' method for the Node
// class. Contains should accept a 'data' argument
// and return the Node in the tree with the same value.
// If the value isn't in the tree return null.

class Node {
constructor(data) {
// 每个node三个属性
this.data = data;
this.left = null;
this.right = null;
}

insert(data) {
if (data < this.data && this.left) {
//对left子节点的递归insert
this.left.insert(data);
} else if (data < this.data) {
// 赋值给left子节点
this.left = new Node(data);
} else if (data > this.data && this.right) {
//对right子节点的递归insert
this.right.insert(data);
} else if (data > this.data) {
// 赋值给right子节点
this.right = new Node(data);
}
}

contains(data) {
if (this.data === data) {
return this;
}

if (this.data < data && this.right) {
// right子节点的contains操作
return this.right.contains(data);
} else if (this.data > data && this.left) {
// left子节点的contains操作
return this.left.contains(data);
}

return null;
}
}

Validate the Binary Search Tree

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
function validate(node, min = null, max = null) {
if (max !== null && node.data > max) {
return false;
}

if (min !== null && node.data < min) {
return false;
}

if (node.left && !validate(node.left, min, node.data)) {
return false;
}

if (node.right && !validate(node.right, node.data, max)) {
return false;
}

return true;
}

BubbleSort

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
function bubbleSort(arr) {
// Implement bubblesort
for (let i = 0; i < arr.length; i++) {
for (let j = 0; j < (arr.length - i - 1); j++) {
if (arr[j] > arr[j+1]) {
const lesser = arr[j+1];
arr[j+1] = arr[j];
arr[j] = lesser;
}
}
}

// return the sorted array
return arr;
}

Selection Sort

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
function selectionSort(arr) {
for (let i = 0; i < arr.length; i++) {
let indexOfMin = i;

for (let j = i+1; j <arr.length; j++) {
if (arr[j] < arr[indexOfMin]) {
indexOfMin = j;
}
}

if (indexOfMin !== i) {
let lesser = arr[indexOfMin];
arr[indexOfMin] = arr[i];
arr[i] = lesser;
}
}

return arr;
}

Merge Sort

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
function mergeSort(arr) {
if (arr.length === 1) {
return arr;
}

const center = Math.floor(arr.length / 2);
const left = arr.slice(0, center);
const right = arr.slice(center);
// 神奇的递归
return merge(mergeSort(left), mergeSort(right));
}

// 辅助函数merge,能够把两个已经排序的left和right数组,整合排序为一个array
function merge(left, right) {
const results = [];

while (left.length && right.length) {
if (left[0] < right[0]) {
results.push(left.shift());
} else {
results.push(right.shift());
}
}
// 到这行的时候,left和right,至少有一个是空的了,所以不用纠结left和right的前后位置了。
return [...results, ...left, ...right];
}

binary Gap

A binary gap within a positive integer N is any maximal sequence of consecutive zeros that is surrounded by ones at both ends in the binary representation of N.

For example, number 9 has binary representation 1001 and contains a binary gap of length 2. The number 529 has binary representation 1000010001 and contains two binary gaps: one of length 4 and one of length 3. The number 20 has binary representation 10100 and contains one binary gap of length 1. The number 15 has binary representation 1111 and has no binary gaps. The number 32 has binary representation 100000 and has no binary gaps.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
function binaryGap(num) {
let str = num.toString(2).split('')
console.log(str)
let counter = 0;
let max = 0;
str.forEach((char) => {
if (char === '0') {
counter ++;
} else {
if (counter > max) {
max = counter;
}
counter = 0;
}
// console.log(counter)
});
return max;
}

Bug and Sell Stock

You will be given a list of stock prices for a given day and your goal is to return the maximum profit that could have been made by buying a stock at the given price and then selling the stock later on. For example if the input is: [45, 24, 35, 31, 40, 38, 11] then your program should return 16 because if you bought the stock at $24 and sold it at $40, a profit of $16 was made and this is the largest profit that could be made. If no profit could have been made, return -1.

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
function getMaxProfit(arr) {
var minIdx = 0;
var maxIdx = 1;
var currMin = 0;
var maxProfit = 0;

if(arr.length < 2) {
throw new Error("Need atleast two time periods to be profitable!");
}

for(var i = 1; i < arr.length; i++) {

// new current min.
if(arr[i] < arr[currMin]) {
currMin = i;
}

// new best profit
if(arr[maxIdx] - arr[minIdx] < arr[i] - arr[currMin]) {
maxIdx = i;
minIdx = currMin;
}

}

maxProfit = arr[maxIdx] - arr[minIdx];
return maxProfit;
}