Skip to content

Commit 07f095a

Browse files
author
Gonzalo Diaz
committed
[Hacker Rank] Interview Preparation Kit: Hash Tables: Ice Cream Parlor. Optimized solution ✅. Reducing complexity.
Cognitive Complexity of functions should not be too high typescript:S3776: https://sonarcloud.io/project/issues?pullRequest=437&open=AZFZr6GONPSO_orcN749&id=sir-gon_algorithm-exercises-ts
1 parent 6153623 commit 07f095a

File tree

1 file changed

+12
-15
lines changed

1 file changed

+12
-15
lines changed

src/hackerrank/interview_preparation_kit/search/ctci_ice_cream_parlor_optimized.ts

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33
*/
44

55
export function whatFlavorsCompute(cost: number[], money: number): number[] {
6-
let ans1;
7-
let ans2;
6+
let ans1: number | null = null;
7+
let ans2: number | null = null;
88

99
const CACHE: Record<number, number> = {};
1010

@@ -16,19 +16,16 @@ export function whatFlavorsCompute(cost: number[], money: number): number[] {
1616
const v1 = cost[i];
1717
const v2 = money - v1;
1818

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-
}
19+
if (v1 != v2 && CACHE?.[v1] && CACHE?.[v2]) {
20+
ans1 = v1;
21+
ans2 = v2;
22+
break;
23+
}
24+
25+
if (v1 == v2 && CACHE?.[v1] && CACHE[v1] > 1) {
26+
ans1 = v1;
27+
ans2 = v1;
28+
break;
3229
}
3330
}
3431

0 commit comments

Comments
 (0)