Skip to content

Commit 13c42f8

Browse files
author
beck chen
committed
organize js files into directories. add implementations for DP
1 parent 45f04f1 commit 13c42f8

File tree

11 files changed

+97
-0
lines changed

11 files changed

+97
-0
lines changed
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
// solving this problem with DP bottom up approach
2+
function waysToClimb(totalNumOfSteps) {
3+
// the number of elements = totalNumberOfSteps + 1 because
4+
// you need to count in 0 too. 0 step is your base case, which represents do nothing
5+
const elements = Array(totalNumOfSteps + 1).fill(0);
6+
elements[0] = 1; // base case
7+
8+
for (let currentIndex = 0; currentIndex < elements.length; currentIndex++) {
9+
// say you have 8 steps total, then your elements will be 0-8
10+
// so we will process a[ii+1] if ii+1 < numberOfElements = 9
11+
if (currentIndex + 1 < elements.length) {
12+
elements[currentIndex + 1] += elements[currentIndex];
13+
}
14+
15+
if (currentIndex + 3 < elements.length) {
16+
elements[currentIndex + 3] += elements[currentIndex];
17+
}
18+
19+
if (currentIndex + 5 < elements.length) {
20+
elements[currentIndex + 5] += elements[currentIndex];
21+
}
22+
}
23+
24+
return elements[elements.length - 1];
25+
}
26+
27+
console.log(waysToClimb(8)); // this should return 19
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
function coinChange(targetAmount, coinValues) {
2+
// initialize an array from 0 to targetAmount
3+
// length is targetAmount + 1 to take 0 into account
4+
const elements = Array(targetAmount + 1).fill(0);
5+
elements[0] = 1;
6+
for (const coinValue of coinValues) {
7+
// notice that we start from ii=coinValue instead of ii=0 here
8+
// there's no way of making 0, 1, or 2 dollars with 3 dollar coins
9+
// also if ii < coinValue, then ii-coinValue < 0 and is out of range
10+
for (let ii = coinValue; ii < elements.length; ii++) {
11+
elements[ii] = elements[ii] + elements[ii - coinValue];
12+
}
13+
}
14+
15+
return elements[targetAmount];
16+
}
17+
18+
console.log(coinChange(5, [1, 2, 5]));
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
function longestIncreasingSubsequence(arr) {
2+
let currentLIS = 1;
3+
if (!arr || arr.length === 0) { return 0; }
4+
5+
// create an array to store the LIS for every element of arr
6+
const lisArr = Array(arr.length).fill(1);
7+
for (let ii = 0; ii < arr.length; ii++) {
8+
// calculate the LIS for every array element up till ii
9+
for (let jj = 0; jj < ii; jj++) {
10+
/*
11+
** if element jj is less than element ii, then
12+
** LIS[ii] could either be:
13+
** 1. LIS[jj] + 1, or
14+
** 2. LIS[ii]
15+
** Therefore we take the max of the two
16+
*/
17+
if (arr[jj] < arr[ii]) {
18+
lisArr[ii] = Math.max(lisArr[jj] + 1, lisArr[ii]);
19+
}
20+
}
21+
22+
/*
23+
** say we have the array [1, 3, 2, 5, 3, 5, 6] and we are at the first 3 (ii == 1).
24+
** at this point our currentLIS is still 1, and our lisArr[1] is 2, so we take the 2 as our lisArr[1].
25+
** however, say we're at the second 3 (ii=4). In this case lisArr[4] is only 2, but our currentLIS = 3
26+
** from the [1, 2, 5]. So we take the currentLIS over the lisArr[4]
27+
*/
28+
currentLIS = Math.max(currentLIS, lisArr[ii]);
29+
}
30+
31+
return currentLIS;
32+
}
33+
34+
console.log(longestIncreasingSubsequence([1, 3, 2, 5, 3, 5, 6])); // this should return 5, for [1, 2, 3, 5, 6]
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
// solving this problem with DP top down approach
2+
function waysToClimb(totalNumberOfSteps) {
3+
const elements = Array(totalNumberOfSteps + 1).fill(0);
4+
elements[0] = 1; // base case
5+
6+
// notice we iterate from 1, not 0. 0 is our base case
7+
for (ii = 1; ii < elements.length; ii++) {
8+
const valueAtIndexMinusOne = ii - 1 < 0 ? 0 : elements[ii - 1];
9+
const valueAtIndexMinusThree = ii - 3 < 0 ? 0 : elements[ii - 3];
10+
const valueAtIndexMinusFive = ii - 5 < 0 ? 0 : elements[ii - 5];
11+
12+
elements[ii] = valueAtIndexMinusOne + valueAtIndexMinusThree + valueAtIndexMinusFive;
13+
}
14+
15+
return elements[totalNumberOfSteps];
16+
}
17+
18+
console.log(waysToClimb(8)); // should return 19
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.

0 commit comments

Comments
 (0)