Skip to content

Commit b966912

Browse files
author
Gonzalo Diaz
committed
[BUGFIX] [Hacker Rank] Interview Preparation Kit: Hash Tables: Ice Cream Parlor. Initial solution optimized to avoid "Set". Timeout fixed.
1 parent 07f095a commit b966912

File tree

2 files changed

+8
-4
lines changed

2 files changed

+8
-4
lines changed

docs/hackerrank/interview_preparation_kit/search/ctci-ice-cream-parlor-solution-notes.md

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,14 @@ The preliminary guess is that the "edges" may be poorly thought out,
2626
and it may also not properly account for cases where there may be
2727
"repeated" values in the search.
2828

29-
## Working solution
29+
## Editorial-based solution
3030

31-
This solution has a complexity of O(N).
31+
Using editorial C++ based solution, I can notice about that solution in O(n) space,
32+
realy do 3 times (3*O(n)) pass over data list.
33+
34+
## Final working solution
35+
36+
This solution has a complexity of O(N) in just one iteration.
3237
It is based on a very simple idea.
3338

3439
A dictionary (key-value object) is created.

src/hackerrank/interview_preparation_kit/search/ctci_ice_cream_parlor.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,8 @@ export function whatFlavorsCompute(
1111
for (const [key, price] of Object.entries(cost)) {
1212
const i = parseInt(key);
1313
const diff = money - price;
14-
const cacheKeys = new Set(Object.keys(cache));
1514

16-
if (cacheKeys.has(diff.toString())) {
15+
if (Number.isInteger(cache?.[diff])) {
1716
return [i + 1, cache[diff] + 1].sort((a, b) => a - b);
1817
}
1918

0 commit comments

Comments
 (0)