Technology Sharing

Code Random Thoughts Algorithm Bootcamp Day 31 | 1049. The Weight of the Last Stone II 494. Target and 474. One and Zero

2024-07-12

한어Русский языкEnglishFrançaisIndonesianSanskrit日本語DeutschPortuguêsΕλληνικάespañolItalianoSuomalainenLatina

1049. The Weight of the Last Stone II

There is a pile of stones, using an integer arraystonesIndicates.stones[i]IndicatesiThe weight of a stone.

Each round, chooseAny two stones, and then crush them together. Assume that the weights of the stones arexandy,andx <= y. Then the possible results of crushing are as follows:

  • ifx == y, then both stones will be completely crushed;
  • ifx != y, then the weight isxThe stone will be completely crushed, and the weight isyThe new weight of the stone isy-x

at last,At most there will be one piece left.Stone. Return this stoneMinimum possible weightIf there are no stones left, return0

Example 1:

输入:stones = [2,7,4,1,8,1]
输出:1
解释:
组合 2 和 4,得到 2,所以数组转化为 [2,7,1,8,1],
组合 7 和 8,得到 1,所以数组转化为 [2,1,1,1],
组合 2 和 1,得到 1,所以数组转化为 [1,1,1],
组合 1 和 1,得到 0,所以数组转化为 [1],这就是最优值。

It should be noted that it has nothing to do with even numbers, so don’t be too clever.

  1. class Solution {
  2. public:
  3. int lastStoneWeightII(vector<int>& stones)
  4. {
  5. vector<int> dp(3001,0);
  6. int sum = 0;
  7. for(int i = 0; i < stones.size() ; i ++)
  8. {
  9. sum += stones[i];
  10. }
  11. // if(sum % 2 == 0) return 0; 注意与偶数无关
  12. int target = sum/2;
  13. dp[0] = 0;
  14. for(int i = 0; i < stones.size() ; i++)
  15. {
  16. for(int j = target; j >= stones[i] ; j--)
  17. {
  18. dp[j] = max(dp[j],dp[j - stones[i]]+ stones[i]);
  19. }
  20. }
  21. return sum - dp[target] - dp[target];
  22. }
  23. };

494. Goals and

Given an array of non-negative integersnumsand an integertarget 。

Add to the beginning of each integer in the array'+'or'-', and then concatenate all the integers to construct aexpression :

  • For example,nums = [2, 1],allowable2Added before'+',exist1Added before'-', and then concatenate them to get the expression"+2-1" 。

Returns a function that can be constructed using the above method and whose result is equal totargets differenceexpressionNumber of.

Example 1:

输入:nums = [1,1,1,1,1], target = 3
输出:5
解释:一共有 5 种方法让最终目标和为 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
  1. class Solution {
  2. public:
  3. int findTargetSumWays(vector<int>& nums, int target)
  4. {
  5. vector<int> dp(1500,0);
  6. int sum = 0;
  7. for(int i = 0; i < nums.size() ; i++)
  8. {
  9. sum += nums[i];
  10. }
  11. if(abs(target) > sum) return 0;
  12. if((sum + target)%2 == 1) return 0;
  13. int mid = (sum + target)/2;
  14. dp[0] = 1;
  15. for(int i = 0; i < nums.size( ) ; i++)
  16. {
  17. for(int j = mid ; j >= nums[i] ; j--)
  18. {
  19. dp[j] += dp[j - nums[i]];
  20. }
  21. }
  22. return dp[mid];
  23. }
  24. };

474. One and Zero

Given an array of binary stringsstrsand two integersmandn 。

Please find and returnstrsThe length of the largest subset ofmosthavemindivual0andnindivual1 。

ifxAll elements ofyElements, collectionsxIs a collectionyofSubset 。

Example 1:

输入:strs = ["10", "0001", "111001", "1", "0"], m = 5, n = 3
输出:4
解释:最多有 5 个 0 和 3 个 1 的最大子集是 {"10","0001","1","0"} ,因此答案是 4 。
其他满足题意但较小的子集包括 {"0001","1"} 和 {"10","1","0"} 。{"111001"} 不满足题意,因为它含 4 个 1 ,大于 n 的值 3 。
  1. class Solution {
  2. public:
  3. int findMaxForm(vector<string>& strs, int m, int n)
  4. {
  5. vector<vector<int>> dp(m+1,vector<int>(n+1,0));
  6. for(string str : strs)
  7. {
  8. int zero = 0;
  9. int one = 0;
  10. for(char c : str)
  11. {
  12. if(c == '0') zero++;
  13. else one++;
  14. }
  15. for(int i = m; i >= zero ; i--)
  16. {
  17. for(int j = n; j >= one; j--)
  18. {
  19. dp[i][j] = max(dp[i][j],dp[i - zero][j - one] + 1);
  20. }
  21. }
  22. }
  23. return dp[m][n];
  24. }
  25. };