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.
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;
}