diff --git a/src/hackerrank/interview_preparation_kit/recursion_and_backtracking/ctci_recursive_staircase.test.ts b/src/hackerrank/interview_preparation_kit/recursion_and_backtracking/ctci_recursive_staircase.test.ts index af535637..3892f56e 100644 --- a/src/hackerrank/interview_preparation_kit/recursion_and_backtracking/ctci_recursive_staircase.test.ts +++ b/src/hackerrank/interview_preparation_kit/recursion_and_backtracking/ctci_recursive_staircase.test.ts @@ -1,10 +1,7 @@ import { describe, expect, it } from '@jest/globals'; import { logger as console } from '../../../logger'; -import { - stepPerms, - step_perms_comput_with_cache -} from './ctci_recursive_staircase'; +import { stepPerms, StepPerms } from './ctci_recursive_staircase'; import TEST_CASES from './ctci_recursive_staircase.testcases.json'; import TEST_CASES_GENERALIZED from './ctci_recursive_staircase_generalized.testcases.json'; @@ -14,9 +11,9 @@ describe('ctci_recursive_staircase', () => { TEST_CASES.forEach((testSet) => { testSet?.tests.forEach((test) => { - const answer = stepPerms(test.input); + const answer = stepPerms(test.n_steps); - console.debug(`stepPerms(${test.input}) solution found: ${answer}`); + console.debug(`stepPerms(${test.n_steps}) solution found: ${answer}`); expect(answer).toStrictEqual(test.expected); }); @@ -26,17 +23,16 @@ describe('ctci_recursive_staircase', () => { it('step_perms_comput_with_cache test cases', () => { expect.assertions(3); + const TOP_LIMIT = 10 ** 10 + 7; + TEST_CASES_GENERALIZED.forEach((testSet) => { testSet?.tests.forEach((test) => { - const initial_cache: Record = {}; - const answer = step_perms_comput_with_cache( - test.input, - initial_cache, - test.limit - ); + const stairs = new StepPerms(TOP_LIMIT, test.steps_limit); + + const answer = stairs.step_perms_comput_with_cache(test.n_steps); console.debug( - `step_perms_comput_with_cache(${test.input}, ${initial_cache}, ${test.limit}) solution found: ${answer}` + `step_perms_comput_with_cache(${test.n_steps}, ${test.steps_limit}) solution found: ${answer}` ); expect(answer).toStrictEqual(test.expected); diff --git a/src/hackerrank/interview_preparation_kit/recursion_and_backtracking/ctci_recursive_staircase.testcases.json b/src/hackerrank/interview_preparation_kit/recursion_and_backtracking/ctci_recursive_staircase.testcases.json index 120ef099..90f5cdf7 100644 --- a/src/hackerrank/interview_preparation_kit/recursion_and_backtracking/ctci_recursive_staircase.testcases.json +++ b/src/hackerrank/interview_preparation_kit/recursion_and_backtracking/ctci_recursive_staircase.testcases.json @@ -1,49 +1,49 @@ [ - { - "title": "Sample Test case 0", - "tests": [ - { - "input": 1, - "expected": 1 - }, - { - "input": 3, - "expected": 4 - }, - { - "input": 7, - "expected": 44 - } - ] - }, - { - "title": "Sample Test case 9", - "tests": [ - { - "input": 5, - "expected": 13 - }, - { - "input": 8, - "expected": 81 - } - ] - }, - { - "title": "Sample Test case 10", - "tests": [ - { - "input": 15, - "expected": 5768 - }, - { - "input": 20, - "expected": 121415 - }, - { - "input": 27, - "expected": 8646064 - } - ] - } + { + "title": "Sample Test case 0", + "tests": [ + { + "n_steps": 1, + "expected": 1 + }, + { + "n_steps": 3, + "expected": 4 + }, + { + "n_steps": 7, + "expected": 44 + } + ] + }, + { + "title": "Sample Test case 9", + "tests": [ + { + "n_steps": 5, + "expected": 13 + }, + { + "n_steps": 8, + "expected": 81 + } + ] + }, + { + "title": "Sample Test case 10", + "tests": [ + { + "n_steps": 15, + "expected": 5768 + }, + { + "n_steps": 20, + "expected": 121415 + }, + { + "n_steps": 27, + "expected": 8646064 + } + ] + } ] diff --git a/src/hackerrank/interview_preparation_kit/recursion_and_backtracking/ctci_recursive_staircase.ts b/src/hackerrank/interview_preparation_kit/recursion_and_backtracking/ctci_recursive_staircase.ts index af6f4da6..efd8e9dc 100644 --- a/src/hackerrank/interview_preparation_kit/recursion_and_backtracking/ctci_recursive_staircase.ts +++ b/src/hackerrank/interview_preparation_kit/recursion_and_backtracking/ctci_recursive_staircase.ts @@ -6,39 +6,41 @@ const TOP_LIMIT = 10 ** 10 + 7; const STEPS_LIMIT = 3; -export function step_perms_comput_with_cache( - n_steps: number, - cache: Record, - steps_limit: number -): number { - if (0 <= n_steps && n_steps <= 2) { - return n_steps; +export class StepPerms { + TOP_LIMIT = 1; + STEPS_LIMIT = 1; + CACHE: Record = {}; + + constructor(top_limit: number, steps_limit: number) { + this.TOP_LIMIT = top_limit; + this.STEPS_LIMIT = steps_limit; } - const keys = new Set(Object.values(cache)); - let result = 0; - - for (let i = 1; i <= Math.min(steps_limit, n_steps); i++) { - const searchKey = n_steps - i; - if (!keys.has(searchKey)) { - cache[searchKey] = step_perms_comput_with_cache( - searchKey, - cache, - steps_limit - ); + step_perms_comput_with_cache(n_steps: number): number { + if (0 <= n_steps && n_steps <= 2) { + return n_steps; } - result += cache[searchKey]; - } + const keys = new Set(Object.keys(this.CACHE)); + let result = 0; + + for (let i = 1; i <= Math.min(this.STEPS_LIMIT, n_steps); i++) { + const searchKey = n_steps - i; + if (!keys.has(searchKey.toString())) { + this.CACHE[searchKey] = this.step_perms_comput_with_cache(searchKey); + } + + result += this.CACHE[searchKey]; + } - return result + (n_steps <= steps_limit ? 1 : 0); + return result + (n_steps <= this.STEPS_LIMIT ? 1 : 0); + } } export function stepPerms(n: number): number { - const initial_cache: Record = {}; - return ( - step_perms_comput_with_cache(n, initial_cache, STEPS_LIMIT) % TOP_LIMIT - ); + const stairs = new StepPerms(TOP_LIMIT, STEPS_LIMIT); + + return stairs.step_perms_comput_with_cache(n) % TOP_LIMIT; } -export default { stepPerms, step_perms_comput_with_cache }; +export default { stepPerms, StepPerms }; diff --git a/src/hackerrank/interview_preparation_kit/recursion_and_backtracking/ctci_recursive_staircase_generalized.testcases.json b/src/hackerrank/interview_preparation_kit/recursion_and_backtracking/ctci_recursive_staircase_generalized.testcases.json index 2a18c778..7b79344b 100644 --- a/src/hackerrank/interview_preparation_kit/recursion_and_backtracking/ctci_recursive_staircase_generalized.testcases.json +++ b/src/hackerrank/interview_preparation_kit/recursion_and_backtracking/ctci_recursive_staircase_generalized.testcases.json @@ -3,8 +3,8 @@ "title": "Own sample 1", "tests": [ { - "input": 4, - "limit": 3, + "n_steps": 4, + "steps_limit": 3, "expected": 7 } ] @@ -13,8 +13,8 @@ "title": "Own sample 2", "tests": [ { - "input": 5, - "limit": 4, + "n_steps": 5, + "steps_limit": 4, "expected": 15 } ] @@ -23,8 +23,8 @@ "title": "Own sample 3", "tests": [ { - "input": 6, - "limit": 2, + "n_steps": 6, + "steps_limit": 2, "expected": 13 } ]