Public
Edited
Jun 13, 2024
Insert cell
Insert cell
function numOfWays2(n) {
if (n < 0) return 0;
if (n == 0) return 1;
return numOfWays2(n-1) + numOfWays2(n-2);
}
Insert cell
numOfWays2(1)
Insert cell
numOfWays2(2)
Insert cell
numOfWays2(3)
Insert cell
numOfWays2(4)
Insert cell
numOfWays2(5)
Insert cell
Insert cell
function numOfWays3(n) {
if (n == 0) return 1;

let res = 0;
for (let i = 1; i <= 3; i++) {
if (n - i < 0) continue;
res = res + numOfWays3(n - i);
}

return res
}
Insert cell
numOfWays3(3)
Insert cell
numOfWays3(4)
Insert cell
numOfWays3(5)
Insert cell
Insert cell
function numOfWays(maxSteps, totalStairs) {
function climb(n) {
if (n == 0) return 1;

let res = 0;
for (let i = 1; i <= maxSteps; i++) {
if (n - i < 0) continue;
res = res + climb(n - i);
}

return res;
}

return climb(totalStairs);
}
Insert cell
numOfWays(2,3)
Insert cell
numOfWays(3,4)
Insert cell
Insert cell
function numOfWaysM(maxSteps, totalStairs) {
let mem = {};

function climb(n) {
if (n == 0) return 1;
if (n in mem) return mem[n];

let res = 0;
for (let i = 1; i <= maxSteps; i++) {
if (n - i < 0) continue;
res = res + climb(n - i);
}

mem[n] = res;
return res;
}

return climb(totalStairs);
}
Insert cell
numOfWaysM(2,40)
Insert cell
numOfWaysM(2,50)
Insert cell
numOfWaysM(3,40)
Insert cell
numOfWaysM(10,50)
Insert cell
Insert cell
function numOfWaysN(maxSteps, totalStairs) {
let mem = {};

function climb(n) {
if (n == 0) return 1n;
if (n in mem) return mem[n];

let sum = 0n;
for (let i = 1; i <= maxSteps; i++) {
if (n < i) continue;
sum += climb(n - i);
}

mem[n] = sum;
return sum;
}

return climb(totalStairs);
}
Insert cell
numOfWaysN(2,100)
Insert cell
numOfWaysN(3,100)
Insert cell
numOfWaysN(4,100)
Insert cell
Insert cell
function numOfWaysL(maxSteps, totalStairs) {
let climbs = [1n];
for (let n = 1; n <= totalStairs; n++) {
let res = 0n;
for (let i = 1; i <= maxSteps; i++) {
if (n - i < 0) continue;
res = res + climbs[n - i];
}
climbs.push(res);
}

return climbs.at(-1);
}
Insert cell
numOfWaysL(10,10000)
Insert cell
Insert cell
function numOfWaysP(maxSteps, totalStairs) {
let m = [1];

function climb(n) {
if (n == 0) return 1;
if (n in m) return m[n];

let res = 0;
for (let i = 1; i <= maxSteps; i++) {
if (n - i < 0) continue;
res = res + climb(n - i);
}

m[n] = res;
return res;
}

climb(totalStairs);
return m
}
Insert cell
numOfWaysP(2,40)
Insert cell
numOfWaysP(3,70)
Insert cell
numOfWaysP(4,60)
Insert cell

One platform to build and deploy the best data apps

Experiment and prototype by building visualizations in live JavaScript notebooks. Collaborate with your team and decide which concepts to build out.
Use Observable Framework to build data apps locally. Use data loaders to build in any language or library, including Python, SQL, and R.
Seamlessly deploy to Observable. Test before you ship, use automatic deploy-on-commit, and ensure your projects are always up-to-date.
Learn more