Add ES6 Examples to all chapters (#38)
* add ES6 example for binary search * add ES6 example for selection sort * add ES6 example for countdown * add ES6 example for greet * add ES6 example for factorial * edit ES6 example for quicksort * add ES6 example for loop sum * add ES6 example for recursive sum * add ES6 example for recursive count * add ES6 example for recursive max * add ES6 example for price of groceries * add ES6 example for check voter * add ES6 example for breadth-first search * add ES6 example for dijkstras algorithm * edit ES6 example for dijkstras algorithm * edit ES6 example for set covering * add ES6 example for longest common subsequence
This commit is contained in:
committed by
Aditya Bhargava
parent
1a81d3e6b2
commit
ec2890a93d
@@ -0,0 +1,7 @@
|
||||
if (word_a[i] === word_b[j]) {
|
||||
// The letters match
|
||||
cell[i][j] = cell[i - 1][j - 1] + 1;
|
||||
} else {
|
||||
// The letters don't match
|
||||
cell[i][j] = Math.max(cell[i - 1][j], cell[i][j - 1]);
|
||||
}
|
||||
9
09_dynamic_programming/ES6/examples/base.js
Normal file
9
09_dynamic_programming/ES6/examples/base.js
Normal file
@@ -0,0 +1,9 @@
|
||||
const initializeMatrix = (rows, cols) => {
|
||||
const matrix = [];
|
||||
for (let i = 0; i < rows.length; i += 1) {
|
||||
matrix.push(Array(cols.length).fill(0));
|
||||
}
|
||||
return matrix;
|
||||
};
|
||||
|
||||
export default initializeMatrix;
|
||||
27
09_dynamic_programming/ES6/examples/diff_two_words.js
Normal file
27
09_dynamic_programming/ES6/examples/diff_two_words.js
Normal file
@@ -0,0 +1,27 @@
|
||||
import base from './base';
|
||||
|
||||
const diff = (firstWord, secondWord) => {
|
||||
const arr1 = firstWord.split('');
|
||||
const arr2 = secondWord.split('');
|
||||
const matrix = initializeMatrix(arr1, arr2);
|
||||
for (let i = 0; i < arr1.length; i += 1) {
|
||||
for (let j = 0; j < arr2.length; j += 1) {
|
||||
if (arr1[i] === arr2[j]) {
|
||||
if (i > 0 && j > 0) {
|
||||
matrix[i][j] = matrix[i - 1][j - 1] + 1;
|
||||
} else {
|
||||
matrix[i][j] = 1;
|
||||
}
|
||||
} else {
|
||||
if (i > 0 && j > 0) {
|
||||
matrix[i][j] = Math.max(matrix[i - 1][j], matrix[i][j - 1]);
|
||||
} else {
|
||||
matrix[i][j] = 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return matrix[arr1.length - 1][arr2.length - 1];
|
||||
};
|
||||
|
||||
export default diff;
|
||||
Reference in New Issue
Block a user