1
/** https://leetcode.com/problems/guess-number-higher-or-lower-ii/5
var getMoneyAmount = function (n) {12
var dp = function (n, start, end) {13
let key = `${start}_${end}`;15
// Base, there is only 1 node on this side of the leg, which mean our guess is always correct and it cost nothing so return 016
if (end - start < 2) {20
// Base, there are only 2 nodes on this side of the leg, which mean we only need to pick cheapest guess21
if (end - start === 2) {22
// The `start` will always be smaller so pick `start`, add 1 to account for 0 index27
if (this.memo.has(key) === true) {28
return this.memo.get(key);32
let minCost = Infinity;34
// Try to arrange the tree's left and right leg and find the cost of each leg35
for (let i = start; i < end; i++) {36
let left = dp(n, start, i);37
let right = dp(n, i + 1, end);39
// Cost of current guess, add 1 to account for 0 index42
// Update cost of current guess, which is the max of left or right leg43
curr = Math.max(left + curr, right + curr);45
// Then update the minimum cost for entire tree46
minCost = Math.min(minCost, curr);50
this.memo.set(key, minCost);