LeetCode 375. Guess Number Higher or Lower II

本文介绍了一种使用动态规划解决猜数字游戏的策略,确保玩家在最坏情况下最小化支付金额。通过定义状态转移方程,文章详细解释了如何计算特定范围内必胜所需的最低成本。

We are playing the Guess Game. The game is as follows:

I pick a number from 1 to n. You have to guess which number I picked.

Every time you guess wrong, I'll tell you whether the number I picked is higher or lower.

However, when you guess a particular number x, and you guess wrong, you pay $x. You win the game when you guess the number I picked.

Example:

n = 10, I pick 8.

First round:  You guess 5, I tell you that it's higher. You pay $5.
Second round: You guess 7, I tell you that it's higher. You pay $7.
Third round:  You guess 9, I tell you that it's lower. You pay $9.

Game over. 8 is the number I picked.

You end up paying $5 + $7 + $9 = $21.

Given a particular n ≥ 1, find out how much money you need to have to guarantee a win.


This is to use min-max algorithm. Suppose, we use dp[i][j] to stand for Cost in range(i, j).

Thus, dp[i][j] = min(k + max(Cost(i, k - 1), Cost(k + 1, j))); // which means we always chose K and then compute the worst case. This value is the minimum cost for each guessed number. It can't guarantee the sequence of guessing.

#include "header.h"
using namespace std;

int getMoney(vector< vector<int> >& cost, int i, int j) {
  int res = INT_MAX;
  if(i >= j) return 0;
  if(cost[i][j] != 0) return cost[i][j];
  for(int k = i; k <= j; ++k) {
    int tmp =  k + max(getMoney(cost, i, k - 1), getMoney(cost, k + 1, j));
    res = min(res, tmp);
  }
  cost[i][j] = res;
  return res;
}

// dp[i][j] stands for the cost for (i, j)
// dp[i][j] = min(k + max(dp[i][k-1], dp[k+1][j])); when chose number k, the left choice becomes the worst.
int getMoneyAmount(int n) {
  vector< vector<int> > cost(n + 1, vector<int>(n + 1, 0));
  return getMoney(cost, 1, n);
}

int main(void) {
  cout << getMoneyAmount(3) << endl;
}


Min-Max algorithm

Pick up coins for maximum gain:

   In the pick-up-coins game, an even number of coins are placed in a line, Two players take turns at choosing one coin each - they can only choose from the two coins at the ends of the line. The game ends when all the coins have been picked up. The player whose coins have the higher total value wins. A player cannot pass his turn.

Design an efficient algorithm for computing the maximum total value for the starting player in the pick-up coins game.

// get the maximum coins the start can get.
/*
  The start has two choices. 
  One: pick at the start and  the opponent is clever enough to choose the maxmum in the leftover coins.
  Two: Pick at the end and the opponent is clever enough to choose the maxmum in the leftover coins.
*/

// dp[i][j] to stands for maxValue from (i, j), thus, 
// dp[i][j] = max(nums[i] + min(maxValue(i-1, j - 1), maxValue(i -2, j)), nums[j] + min(maxValue(i - 1, j - 1, maxValue(i, j - 2))));

int computeMaximumRevenueForRange(vector<int>& coins, int a, int b, vector< vector<int> >& maximum_revenue_for_range) {
  if(a > b) return 0;
  if(maximum_revenue_for_range == 0) {
    int max_revenue_a = coins[a] + min(computeMaximumRevenueForRange(coins, a + 2, b, maximum_revenue_for_range), computeMaximumRevenueForRange(coins, a + 1, b - 1, maximum_revenue_for_range));
  int max_revenue_b = coins[b] + min(computeMaximumRevenueForRange(coins, a + 1, b - 1, maximum_revenue_for_range), computeMaximumRevenueForRange(coins, a + 2, b - 1, maximum_revenue_for_range));
  maximum_revenue_for_range[a][b] = max(max_revenue_a, max_revenue_b);
  }
  return maximum_revenue_for_range[a][b];
}

int maximumRevenue(const vector<int>& coins) {
  vector< vector<int> > maximum_revenue_for_range(coins.size(), vector<int>(coins.size(), 0))
  return computeMaximumRevenueForRange(coins, 0, coins.size() - 1, maximum_revenue_for_range);
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值