Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
15 changed files
with
73 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -14,7 +14,7 @@ function inverse(num) { | |
(num + '') | ||
.split('') | ||
.reverse() | ||
.join(''), | ||
.join('') | ||
) | ||
} | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
// https://www.hackerrank.com/challenges/ctci-ice-cream-parlor/problem | ||
function iceCreamParlor(cost, money) { | ||
const hashTable = {} | ||
|
||
for (let i = 0; i < cost.length; i++) { | ||
if (cost[i] < money) { | ||
if (hashTable[money - cost[i]] !== undefined) { | ||
return [hashTable[money - cost[i]] + 1, i + 1] | ||
} | ||
hashTable[cost[i]] = i | ||
} | ||
} | ||
} | ||
|
||
/* | ||
* Slow implementation | ||
* | ||
function iceCreamParlor(cost, money) { | ||
const hashTable = {} | ||
for (let i = 0; i < cost.length; i++) { | ||
if (cost[i] < money) { | ||
hashTable[i] = cost[i] | ||
} | ||
} | ||
const keys = Object.keys(hashTable) | ||
for(let i = 0; i < keys.length - 1; i++) { | ||
for (let j = i + 1; j < keys.length; j++) { | ||
if (hashTable[keys[i]] + hashTable[keys[j]] === money) { | ||
return [parseInt(keys[i])+1, parseInt(keys[j])+1] | ||
} | ||
} | ||
} | ||
} | ||
*/ | ||
|
||
module.exports = iceCreamParlor |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
const iceCreamParlor = require('../iceCreamParlor') | ||
|
||
test('An iceCreamParlor test', () => { | ||
const cost = [2, 1, 3, 5, 6] | ||
const money = 5 | ||
const expectedResult = [1, 3] | ||
|
||
expect(iceCreamParlor(cost, money)).toEqual(expectedResult) | ||
}) | ||
|
||
test('Another iceCreamParlor test', () => { | ||
const cost = [1, 4, 5, 3, 2] | ||
const money = 4 | ||
const expectedResult = [1, 4] | ||
expect(iceCreamParlor(cost, money)).toEqual(expectedResult) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters