Last active
March 30, 2021 17:43
-
-
Save murraco/8463d8dcd7748ef7b558788292558b6e to your computer and use it in GitHub Desktop.
Hard Code Challenge: https://www.codingame.com/training/hard/bender-episode-2 | Mauricio Urraco
This file contains hidden or 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
const rooms = {}; | |
const roomMaxValue = []; | |
const N = parseInt(readline()); | |
for (let i = 0; i < N; i++) { | |
const room = readline(); | |
// Destructure room information | |
const [id, money, door1, door2] = room.split(' '); | |
// Add values to the rooms data structure | |
rooms[id] = { money, door1, door2 }; | |
} | |
/** | |
* Returns the maximum amount of money Bender can collect by taking | |
* a series of doors to reach the outside of the building | |
* | |
* @param {string} id - ID of the current room | |
* @return {number} Maximum amount of money | |
*/ | |
const maximizeMoney = (id) => { | |
if (!roomMaxValue[id]) { | |
const curRoom = rooms[id]; | |
let door1MaxValue = Number(curRoom['money']); | |
let door2MaxValue = Number(curRoom['money']); | |
if (curRoom['door1'] !== 'E') door1MaxValue += maximizeMoney(curRoom['door1']); | |
if (curRoom['door2'] !== 'E') door2MaxValue += maximizeMoney(curRoom['door2']); | |
roomMaxValue[id] = Math.max(door1MaxValue, door2MaxValue); | |
} | |
return roomMaxValue[id]; | |
} | |
console.log(maximizeMoney(0)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment