Skip to content

Commit 0bf5dc2

Browse files
author
Gonzalo Diaz
committed
[BUGFIX] [REFACTOR] [Hacker Rank] Interview Preparation Kit: Recursion: Davis' Staircase.
Eslint issue eslint(no-param-reassign) fixed.
1 parent 0a8802f commit 0bf5dc2

File tree

2 files changed

+8
-13
lines changed

2 files changed

+8
-13
lines changed

src/hackerrank/interview_preparation_kit/recursion_and_backtracking/ctci_recursive_staircase.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,30 +5,30 @@
55

66
const TOP_LIMIT = 10 ** 10 + 7;
77
const STEPS_LIMIT = 3;
8+
const CACHE = {};
89

9-
export function stepPermsComputWithCache(nSteps, cache, stepsLimit) {
10+
export function stepPermsComputWithCache(nSteps, stepsLimit) {
1011
if (nSteps >= 0 && nSteps <= 2) {
1112
return nSteps;
1213
}
1314

14-
const keys = new Set(Object.values(cache));
15+
const keys = new Set(Object.keys(CACHE));
1516
let result = 0;
1617

1718
for (let i = 1; i <= Math.min(stepsLimit, nSteps); i++) {
1819
const searchKey = nSteps - i;
1920
if (!keys.has(searchKey)) {
20-
cache[searchKey] = stepPermsComputWithCache(searchKey, cache, stepsLimit);
21+
CACHE[searchKey] = stepPermsComputWithCache(searchKey, stepsLimit);
2122
}
2223

23-
result += cache[searchKey];
24+
result += CACHE[searchKey];
2425
}
2526

2627
return result + (nSteps <= stepsLimit ? 1 : 0);
2728
}
2829

2930
export function stepPerms(n) {
30-
const initialCache = {};
31-
return stepPermsComputWithCache(n, initialCache, STEPS_LIMIT) % TOP_LIMIT;
31+
return stepPermsComputWithCache(n, STEPS_LIMIT) % TOP_LIMIT;
3232
}
3333

3434
export default { stepPerms, stepPermsComputWithCache };

src/hackerrank/interview_preparation_kit/recursion_and_backtracking/ctci_recursive_staircase.test.js

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -28,15 +28,10 @@ describe('ctci_recursive_staircase', () => {
2828

2929
TEST_CASES_GENERALIZED.forEach((testSet) => {
3030
testSet?.tests.forEach((test) => {
31-
const initialCache = {};
32-
const answer = stepPermsComputWithCache(
33-
test.input,
34-
initialCache,
35-
test.limit
36-
);
31+
const answer = stepPermsComputWithCache(test.input, test.limit);
3732

3833
console.debug(
39-
`stepPermsComputWithCache(${test.input}, ${initialCache}, ${test.limit}) solution found: ${answer}`
34+
`stepPermsComputWithCache(${test.input}, ${test.limit}) solution found: ${answer}`
4035
);
4136

4237
expect(answer).toStrictEqual(test.expected);

0 commit comments

Comments
 (0)