Algorithm_Memorization DFS - xwu36/LeetCode GitHub Wiki
Problem:
n 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.
Example
Input:
maxChoosableInteger = 10
desiredTotal = 11
Output:
false
Explanation:
No matter which integer the first player choose, the first player will lose.
The first player can choose an integer from 1 up to 10.
If the first player choose 1, the second player can only choose integers from 2 up to 10.
The second player will win by choosing 10 and get a total = 11, which is >= desiredTotal.
Same with other integers chosen by the first player, the second player will always win.
Code:
public class Solution {
Map<Integer, Boolean> map;
public boolean canIWin(int maxChoosableInteger, int desiredTotal) {
int sum = (1+maxChoosableInteger)*maxChoosableInteger/2;
if(sum < desiredTotal) return false;
if(desiredTotal <= 0) return true;
map = new HashMap();
boolean[] used = new boolean[maxChoosableInteger+1];
return helper(desiredTotal, used);
}
public boolean helper(int desiredTotal, boolean[] used){
if(desiredTotal <= 0) return false;
int key = format(used);
if(!map.containsKey(key)){
// try every unchosen number as next step
for(int i=1; i<used.length; i++){
if(!used[i]){
used[i] = true;
// check whether this lead to a win (i.e. the other player lose)
if(!helper(desiredTotal-i, used)){
map.put(key, true);
used[i] = false;
return true;
}
used[i] = false;
}
}
map.put(key, false);
}
return map.get(key);
}
// transfer boolean[] to an Integer
public int format(boolean[] used){
int num = 0;
for(boolean b: used){
num <<= 1;
if(b) num |= 1;
}
return num;
}
}
Problem:
Given an array of integers nums and a positive integer k, find whether it's possible to divide this array into k non-empty subsets whose sums are all equal.
Example 1:
Input: nums = [4, 3, 2, 3, 5, 2, 1], k = 4
Output: True
Explanation: It's possible to divide it into 4 subsets (5), (1, 4), (2,3), (2,3) with equal sums.
Note:
1 <= k <= len(nums) <= 16.
0 < nums[i] < 10000.
Code:
class Solution {
private Map<Integer, Boolean> lookup = new HashMap<>();
public boolean canPartitionKSubsets(int[] nums, int k) {
int n = nums.length;
if(n == 0) return false;
int sum = 0;
for(int num : nums) sum += num;
if(sum % k != 0) return false;
sum = sum / k;
boolean[] visited = new boolean[n + 1];
return dfs(sum, k, nums, visited, n, sum);
}
public boolean dfs(int sum, int k, int[] nums, boolean[] visited, int n, int ori){
int cur = format(visited);
if(k == 0) return true;
if(lookup.get(cur) != null) return lookup.get(cur);
for(int i = 0; i < n; i++){
if(!visited[i] && sum - nums[i] >= 0){
visited[i] = true;
if(sum - nums[i] == 0 && dfs(ori, k - 1, nums, visited, n, ori)){
lookup.put(cur, true);
return true;
}else if(sum - nums[i] > 0 && dfs(sum - nums[i], k - 1, nums, visited, n, ori)){
lookup.put(cur, true);
return true;
}
visited[i] = false;
}
}
lookup.put(cur, false);
return false;
}
public int format(boolean[] visited){
int res = 0;
for(int i = 0; i < visited.length; i++){
res <<= 1;
if(visited[i]) res |= 1;
}
return res;
}
}