Created
June 14, 2020 14:40
-
-
Save YaroslavShapoval/d698769a30b99c4cc2154af9550b9bda to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| const ways = { | |
| // number: 10 => steps: [A, B, C, A, B, C] | |
| }; | |
| const START = 3; | |
| const FINISH = 22; | |
| let minWayToFinish = undefined; | |
| function check(number, steps = []) { | |
| if (number > FINISH) { | |
| return; | |
| } | |
| if (number === FINISH) { | |
| if (!minWayToFinish || steps.length < minWayToFinish.length) { | |
| minWayToFinish = steps; | |
| console.log(steps.length + ": " + steps.join(', ')); | |
| return; | |
| } | |
| } | |
| if (ways.hasOwnProperty(number)) { | |
| if (steps.length <= ways[number].length) { | |
| ways[number] = steps; | |
| } | |
| } else { | |
| check(number+3, steps.concat(['A'])); | |
| check(number*2, steps.concat(['B'])); | |
| check(number+1, steps.concat(['C'])); | |
| } | |
| } | |
| check(START); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment