DP (2) -- Can I Win, Counting Bits,Integer Break

Can I Win

In the "100 game," two players take turns adding, to a running total, any integer from 1..10. The player who first causes the running total to reach or exceed 100 wins.

What if we change the game so that players cannot re-use integers?

For example, two players might take turns drawing from a common pool of numbers of 1..15 without replacement until they reach a total >= 100.

Given an integer maxChoosableInteger and another integer desiredTotal, determine if the first player to move can force a win, assuming both players play optimally.

You can always assume that maxChoosableInteger will not be larger than 20 and desiredTotal will not be larger than 300.


1. DP最重要是要找到状态如何去描述。该问题的状态需从两方面去描述 1)当前已使用的数  2)目前的desireTotal。这两个状态可以由当前已使用的数去描述

2. 利用DFS的思想搜索所有可能的情况,通过unordered_map记录已搜索过状态防止重复搜索。

3. 如果当前状态可获胜,则 1) 可使用的最大数大于等于desiredTotal   或者     2) 使用该数后的下一状态不可获胜,因此我们遍历所有可能的情况

3. 使用bit manipulation储存数的使用情况。因为maxChoosableInteger 不超过20,所以可以用int去存储。不可使用标0,可使用标1.

int:基本整型,当字节数为2时 取值范围为-32768~32767,当字节数为4时 取值范围 负的2的31次方  到  2的31次方减1
unsigned int:无符号基本整型,当字节数为2时 取值范围为0~6535,当字节数为4时 取值范围为0到2的32次方减1

4. 还可以使用vector代替hashmap来记录state,采用角标的方式,这样更快,但空间会一下变的很大

    bool tryState(int desiredTotal, int state, unordered_map<int, bool>& rst, int maxChoose){
        if(rst.find(state) != rst.end()) return rst[state];
        for(int i = maxChoose; i > 0; i--){
            int bit = 1 << i;  //only at i is 1
            //if this num is not used
            if(bit & state){
                if(i >= desiredTotal || !tryState(desiredTotal-i, state^bit, rst, maxChoose)){
                    rst[state] = true;
                    return true;
                }
            }
        }
        rst[state] = false;
        return false;
    }
    
    bool canIWin(int maxChoosableInteger, int desiredTotal) {
        if(maxChoosableInteger * (maxChoosableInteger + 1) / 2 < desiredTotal)
            return false;
        unordered_map<int, bool> rst;
        //fill bit of state with 1, not use least significance for convenience
        int state = (1 << (maxChoosableInteger + 1)) - 1;
        return tryState(desiredTotal, state, rst, maxChoosableInteger);
    }

Counting Bits

Given a non negative integer number num. For every numbers i in the range 0 ≤ i ≤ num calculate the number of 1's in their binary representation and return them as an array.

Example:
For num = 5 you should return [0,1,1,2,1,2].

常规解法:

    vector<int> countBits(int num) {
        int bitsLen = 1;
        int ones = 0;
        int boundNum = 0;   //that is 2^n
        vector<int> oneNums(num + 1,0);
        for(int i = 1; i <= num; i++){
            if(i % 2 == 1) ones++;
            else if(ones == bitsLen){    //more bit is needed
                ones = 1;
                bitsLen++;
                boundNum = i;
            }
            else ones = oneNums[boundNum] + oneNums[i-boundNum];
            oneNums[i] = ones;
        }
        return oneNums;
    }

解法2:i & i -1 将i从右到左的第一个1置0, i & i -1 必然是小于i,且i 的1的个数比i & i -1 多一

 vector<int> countBits(int num) {
    vector<int> bits(num+1, 0);
    for (int i = 1; i <= num; i++) bits[i] = bits[i & (i-1)] + 1;
    return bits;
 }



Integer Break

Given a positive integer n, break it into the sum of at least two positive integers and maximize the product of those integers. Return the maximum product you can get.

For example, given n = 2, return 1 (2 = 1 + 1); given n = 10, return 36 (10 = 3 + 3 + 4).

Note: You may assume that n is not less than 2 and not larger than 58

num应该被分裂为有尽可能多的3;当n<5时进行分别处理。

    int integerBreak(int n) {
        int product = 1;
        if(n < 5) return (n / 2) * (n - n / 2);
        while(n - 3 > 1){
            product *= 3;
            n -= 3;
        }
        return product *= n;
    }




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值