-
Notifications
You must be signed in to change notification settings - Fork 0
/
lesson13.js
67 lines (52 loc) · 1.58 KB
/
lesson13.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
60
61
62
63
64
65
66
67
//ladder
function getKFibo(K) {
var first = Math.pow(1.618034, K+1)
var second = Math.pow(-0.618034, K+1)
var third = Math.sqrt(5)
return Math.round((first - second) / third)
}
function solution(A, B) {
var result = []
for (j = 0; j < A.length; j++) {
result[j] = getKFibo(A[j]) % Math.pow(2, B[j])
}
return result
}
//frog fibo
function getNextFibo(K) {
var first = Math.pow(1.618034, K)
var second = Math.pow(-0.618034, K)
var third = Math.sqrt(5)
return Math.round((first - second) / third)
}
function solution(A) {
var currentPos = -1
var count = 0
var fibo
var possibleJump
while (currentPos < A.length) {
fibo = 1
possibleJump = -1
while ((currentPos + getNextFibo(fibo)) < (A.length + 1)) {
var isMiddleArray = A[currentPos + getNextFibo(fibo)] === 1
var isLastPosition = (currentPos + getNextFibo(fibo)) === A.length
if (isMiddleArray || isLastPosition) {
//console.log('fibo: ', getNextFibo(fibo))
//console.log('currentPos: ', currentPos)
possibleJump = currentPos + getNextFibo(fibo)
//console.log('possibleJump: ', possibleJump)
}
fibo++
}
if (possibleJump !== -1) {
currentPos = possibleJump
count++
} else {
break;
}
//console.log('******** currentPos: ', currentPos)
//console.log('******** count: ', count)
}
if (currentPos === A.length) return count
return - 1
}