-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdata-structures.html
132 lines (109 loc) · 2.84 KB
/
data-structures.html
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Data Structures</title>
</head>
<body>
<script>
/**
*
* Fibonacci
*/
var cache = {};
function fibonacciMemozation(number) {
if (number < 1)
return 0;
if (number <= 2)
return 1;
if (number in cache)
return cache[number];
var value = fibonacciMemozation(number - 1) + fibonacciMemozation(number - 2);
cache[number] = value;
return value;
console.log(value);
}
//console.clear();
//console.log(fibonacciMemozation(5));
// console.log(fibonacciMemozation(50));
coins = [1, 2, 5];
//amount = 15;
function minCoins(coinArray, amount) {
if (coinArray[amount] > 0) {
return coinArray[amount];
}
////console.log('minCoins');
let index = 1;
let minCount = amount;
while ((amount - index) > 1) {
if ((minCoins(coinArray, amount - index) + minCoins(coinArray, index)) < minCount) {
minCount = (minCoins(coinArray, amount - index) + minCoins(coinArray, index));
}
index++;
}
return minCount;
}
/**
* coin problem
* @param amount
* @returns {number|*}
*/
function coinProblem(amount) {
if (coins.indexOf(amount) >= 0) {
return 1;
}
if (amount > coins[coins.length - 1]) {
coinCount = new Array(amount + 1).fill(0);
} else {
coinCount = new Array(coins[coins.length - 1] + 1).fill(0);
}
// Set up
for (let i = 0; i < coins.length; i++) {
coinCount[coins[i]] = 1;
}
// Two coins
for (let i = 0; i < coins.length; i++) {
if (coinCount[coins[i] * 2] == 0) {
coinCount[coins[i] * 2] = 2;
}
}
return minCoins(coinCount, amount);
}
console.clear();
console.log(coinProblem(2));
console.log(coinProblem(3));
console.log(coinProblem(4));
console.log(coinProblem(15));
/**
* Selection sort
*/
function selectionSort(arrayNumbers) {
let n = arrayNumbers.length;
}
/**
* Happy numbers
*/
function isHappy(n) {
let unique_num = new Array();
let notExists = true;
while (notExists) {
let value = 0;
while (n > 0) {
value += Math.pow(n % 10, 2);
n /= 10;
}
n = value;
if (unique_num.indexOf(n) >= 0){
notExists = false;
break;
}
unique_num.push(n);
}
return n == 1;
}
console.log(isHappy(19));
console.log(isHappy(4));
console.log(isHappy(0));
</script>
</body>
</html>