O'Reilly logo

Hands-On Data Structures and Algorithms with JavaScript by Kashyap Mukkamala

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Implementing the dynamic programming algorithm

Now that we understand the approach, let's first create the class for our algorithm and add the analyze() method, which will first create the 2D array before generating the algorithm.

When the class is initialized, we will construct our 2D array with all the values in it set to false. We will then use this 2D array and update some of the values within it based on our conditions, which we will discuss shortly:

var _ = require('lodash');class Planner {   constructor(rows, cols) {      // create a 2d array of rows x cols      // all with value false      this.planner = _.range(rows).map(() => {         return _.range(cols + 1).map(()=> false);      });            // holds the response      this.outcomes = [];   }}module.exports = Planner;

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required