Hell Triangle Challenge for B2W - SkyHub
Node.js 4.3.2+
For a small triangle, it's possible to iterate over all lines and calculate all posible routes to return the highest value. However, using this technique in triangles with many lines, the number of possible routes could take a long time to be calculated or even overload the memory. The solution was to calculate the triangle bottom-up.
Install dependencies:
npm install
Build the library:
npm run build
Now create an js file and import the library:
const Triangle = require('./dist')
Or using ES6:
import Triangle from './dist'
Initialize the library passing the matrix on its constructor, and calculates using the maxTotal method:
let triangle = new Triangle([
[6],
[3, 5],
[9, 7, 1],
[4, 6, 8, 4]
])
console.log(triangle.maxTotal())
To see this example in action run:
node example.js
To run the tests:
npm test
- 0.1.0 Initial release