-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCats and shelves.js
35 lines (31 loc) · 1.38 KB
/
Cats and shelves.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
// An infinite number of shelves are arranged one above the other in a staggered fashion.
// The cat can jump either one or three shelves at a time: from shelf i to shelf i+1 or i+3 (the cat cannot climb on the shelf directly above its head), according to the illustration:
// ┌────────┐
// │-6------│
// └────────┘
// ┌────────┐
// │------5-│
// └────────┘ ┌─────► OK!
// │ ┌────────┐
// │ │-4------│
// │ └────────┘
// ┌────────┐ │
// │------3-│ │
// BANG!────┘ ├─────► OK!
// ▲ |\_/| │ ┌────────┐
// │ ("^-^) │ │-2------│
// │ ) ( │ └────────┘
// ┌─┴─┴───┴┬──┘
// │------1-│
// └────────┘
// Input
// Start and finish shelf numbers (always positive integers, finish no smaller than start)
// Task
// Find the minimum number of jumps to go from start to finish
// Example
// Start 1, finish 5, then answer is 2 (1 => 4 => 5 or 1 => 2 => 5)
function solution(start, finish)
{
let moves= Math.floor((finish - start)/3)
return moves + ((finish-start)%3)
}