Skip to content

Commit 6153623

Browse files
author
Gonzalo Diaz
committed
[Hacker Rank] Interview Preparation Kit: Hash Tables: Ice Cream Parlor. Optimized solution ✅.
1 parent d69213b commit 6153623

File tree

2 files changed

+101
-0
lines changed

2 files changed

+101
-0
lines changed
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
import { describe, expect, it } from '@jest/globals';
2+
import { logger as console } from '../../../logger';
3+
4+
import {
5+
whatFlavors,
6+
whatFlavorsCompute
7+
} from './ctci_ice_cream_parlor_optimized';
8+
import TEST_CASES from './ctci_ice_cream_parlor.testcases.json';
9+
import TEST_CASES_BORDER_CASES from './ctci_ice_cream_parlor.border_testcases.json';
10+
11+
describe('ctci_ice_cream_parlor_optimized', () => {
12+
it('whatFlavorsCompute test cases', () => {
13+
expect.assertions(10);
14+
15+
TEST_CASES.forEach((testSet) => {
16+
testSet?.tests.forEach((test) => {
17+
const answer = whatFlavorsCompute(test.costs, test.money);
18+
19+
console.debug(
20+
`${testSet.title} whatFlavorsCompute(${test.costs}, ${test.money}) solution found: ${answer}`
21+
);
22+
23+
expect(answer).toStrictEqual(test.expected);
24+
expect(whatFlavors(test.costs, test.money)).toBeUndefined();
25+
});
26+
});
27+
});
28+
29+
it('whatFlavors border test cases', () => {
30+
expect.assertions(2);
31+
32+
TEST_CASES_BORDER_CASES.forEach((testSet) => {
33+
testSet?.tests.forEach((test) => {
34+
expect(whatFlavorsCompute(test.costs, test.money)).toStrictEqual(
35+
test.expected
36+
);
37+
expect(whatFlavors(test.costs, test.money)).toBeUndefined();
38+
});
39+
});
40+
});
41+
42+
it('whatFlavors test caller function', () => {
43+
expect.assertions(1);
44+
45+
const cost: number[] = [];
46+
const money: number = 100;
47+
48+
expect(whatFlavors(cost, money)).toBeUndefined();
49+
});
50+
});
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
/**
2+
* @link Problem definition [[docs/hackerrank/interview_preparation_kit/search/ctci-ice-cream-parlor.md]]
3+
*/
4+
5+
export function whatFlavorsCompute(cost: number[], money: number): number[] {
6+
let ans1;
7+
let ans2;
8+
9+
const CACHE: Record<number, number> = {};
10+
11+
for (const price of cost) {
12+
CACHE[price] = Number.isInteger(CACHE[price]) ? CACHE[price] + 1 : 1;
13+
}
14+
15+
for (const i in cost) {
16+
const v1 = cost[i];
17+
const v2 = money - v1;
18+
19+
if (v1 != v2) {
20+
if (CACHE?.[v1] && CACHE?.[v2]) {
21+
ans1 = v1;
22+
ans2 = v2;
23+
break;
24+
}
25+
} else {
26+
// count of the element must be greater than 1 if they are same
27+
if (CACHE?.[v1] && CACHE[v1] > 1) {
28+
ans1 = v1;
29+
ans2 = v1;
30+
break;
31+
}
32+
}
33+
}
34+
35+
const result: Set<number> = new Set();
36+
for (const i in cost) {
37+
const x: number = parseInt(i);
38+
39+
if (cost[x] == ans1 || cost[x] == ans2) {
40+
result.add(x + 1);
41+
}
42+
}
43+
44+
return Array.from(result);
45+
}
46+
47+
export function whatFlavors(cost: number[], money: number): void {
48+
console.log(whatFlavorsCompute(cost, money)?.join(' '));
49+
}
50+
51+
export default { whatFlavorsCompute, whatFlavors };

0 commit comments

Comments
 (0)