-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathclimb.js
More file actions
71 lines (34 loc) · 962 Bytes
/
Copy pathclimb.js
File metadata and controls
71 lines (34 loc) · 962 Bytes
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
function detectCycle(head){
let slow = head;
let fast = head;
while(fast && fast.next){
slow = slow.next;
fast = fast.next.next;
if(slow === fast){
removeCycle(slow,head);
return true;
}
}
return false;
}
function removeCycle(loopNode,head){
let ptr1 = head;
let ptr2 = loopNode;
while(ptr1.next !== ptr2.next){
ptr1 = ptr1.next;
ptr2 = ptr2.next;
}
ptr2.next = null;
}
// Problem: You are climbing a staircase. It takes n steps to reach the top. Each time you can either climb 1 or 2 steps. How many distinct ways can you reach the top?
function climbStairs(n){
if(n === 0 || n === 1) return 1;
let first = 1, second = 2;
for(let i = 3; i <= n; i++){
let third = first + second;
first = second;
second = third;
}
return second
}
console.log(climbStairs(4)) //Output: 5