Journey of LeetCode|DAY 37
Preface
This is a new day to continue my Dynamic Programming journey.
Learn something new and keep reviewing what I learnt before.
1. Last Stone Weight II
LeetCode Link: 1049. Last Stone Weight II
You are given an array of integers stones where stones[i] is the weight of the ith stone.
We are playing a game with the stones. On each turn, we choose any two stones and smash them together. Suppose the stones have weights x and y with x <= y. The result of this smash is:
If x == y, both stones are destroyed, and
If x != y, the stone of weight x is destroyed, and the stone of weight y has new weight y - x.
At the end of the game, there is at most one stone left.
Return the smallest possible weight of the left stone. If there are no stones left, return 0.
Example 1:
Input: stones = [2,7,4,1,8,1]
Output: 1
Explanation:
We can combine 2 and 4 to get 2, so the array converts to [2,7,1,8,1] then,
we can combine 7 and 8 to get 1, so the array converts to [2,1,1,1] then,
we can combine 2 and 1 to get 1, so the array converts to [1,1,1] then,
we can combine 1 and 1 to get 0, so the array converts to [1], then that’s the optimal value.
Example 2:
Input: stones = [31,26,33,21,40]
Output: 5
Constraints:
1 <= stones.length <= 30
1 <= stones[i] <= 100
Analysis and Solution
Dynamic Programming
LeetCode C++ as followings 01 backpack
class Solution {
public:
int lastStoneWeightII(vector<int>& stones) {
vector<int> dp(15001, 0);
int sum = 0;//initialization
for (int i = 0; i < stones.size(); i++) sum += stones[i];//get the sum
int target = sum / 2;//set the target
for (int i = 0; i < stones.size(); i++) { // traverse items
for (int j = target; j >= stones[i]; j--) { // traverse backpack
dp[j] = max(dp[j], dp[j - stones[i]] + stones[i]);//Recursive formula
}
}
return sum - dp[target] - dp[target];//result
}
};
2. Target Sum
LeetCode Link: 494. Target Sum
You are given an integer array nums and an integer target.
You want to build an expression out of nums by adding one of the symbols ‘+’ and ‘-’ before each integer in nums and then concatenate all the integers.
For example, if nums = [2, 1], you can add a ‘+’ before 2 and a ‘-’ before 1 and concatenate them to build the expression “+2-1”.
Return the number of different expressions that you can build, which evaluates to target.
Example 1:
Input: nums = [1,1,1,1,1], target = 3
Output: 5
Explanation: There are 5 ways to assign symbols to make the sum of nums be target 3.
-1 + 1 + 1 + 1 + 1 = 3
+1 - 1 + 1 + 1 + 1 = 3
+1 + 1 - 1 + 1 + 1 = 3
+1 + 1 + 1 - 1 + 1 = 3
+1 + 1 + 1 + 1 - 1 = 3
Example 2:
Input: nums = [1], target = 1
Output: 1
Constraints:
1 <= nums.length <= 20
0 <= nums[i] <= 1000
0 <= sum(nums[i]) <= 1000
-1000 <= target <= 1000
Analysis and Solution
Dynamic Programming
LeetCode C++ as followings 01 backpack
class Solution {
public:
int findTargetSumWays(vector<int>& nums, int S) {//S is target
int sum = 0;//initialization
for (int i = 0; i < nums.size(); i++) sum += nums[i];//get the sum
if (abs(S) > sum) return 0; // No answer
if ((S + sum) % 2 == 1) return 0; // No answer
int bagSize = (S + sum) / 2;//initialization
vector<int> dp(bagSize + 1, 0);
dp[0] = 1;
for (int i = 0; i < nums.size(); i++) {//external loop
for (int j = bagSize; j >= nums[i]; j--) {//internal loop
dp[j] += dp[j - nums[i]];
}
}
return dp[bagSize];
}
};
3. Ones and Zeroes
LeetCode Link: 474. Ones and Zeroes
You are given an array of binary strings strs and two integers m and n.
Return the size of the largest subset of strs such that there are at most m 0’s and n 1’s in the subset.
A set x is a subset of a set y if all elements of x are also elements of y.
Example 1:
Input: strs = [“10”,“0001”,“111001”,“1”,“0”], m = 5, n = 3
Output: 4
Explanation: The largest subset with at most 5 0’s and 3 1’s is {“10”, “0001”, “1”, “0”}, so the answer is 4.
Other valid but smaller subsets include {“0001”, “1”} and {“10”, “1”, “0”}.
{“111001”} is an invalid subset because it contains 4 1’s, greater than the maximum of 3.
Example 2:
Input: strs = [“10”,“0”,“1”], m = 1, n = 1
Output: 2
Explanation: The largest subset is {“0”, “1”}, so the answer is 2.
Constraints:
1 <= strs.length <= 600
1 <= strs[i].length <= 100
strs[i] consists only of digits ‘0’ and ‘1’.
1 <= m, n <= 100
Analysis and Solution
Dynamic Programming
LeetCode C++ as followings 01 backpack
class Solution {
public:
int findMaxForm(vector<string>& strs, int m, int n) {
vector<vector<int>> dp(m + 1, vector<int> (n + 1, 0)); // initialization
for (string str : strs) { // traverse items
int oneNum = 0, zeroNum = 0;
for (char c : str) {
if (c == '0') zeroNum++;
else oneNum++;
}
for (int i = m; i >= zeroNum; i--) { // traverse backpack from end to front遍
for (int j = n; j >= oneNum; j--) {
dp[i][j] = max(dp[i][j], dp[i - zeroNum][j - oneNum] + 1);//Recursive formula
}
}
}
return dp[m][n];
}
};