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 arraystones
Indicates.stones[i]
Indicatesi
The weight of a stone.
Each round, chooseAny two stones, and then crush them together. Assume that the weights of the stones arex
andy
,andx <= y
. Then the possible results of crushing are as follows:
x == y
, then both stones will be completely crushed;x != y
, then the weight isx
The stone will be completely crushed, and the weight isy
The 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.
- class Solution {
- public:
- int lastStoneWeightII(vector<int>& stones)
- {
- vector<int> dp(3001,0);
- int sum = 0;
- for(int i = 0; i < stones.size() ; i ++)
- {
- sum += stones[i];
- }
- // if(sum % 2 == 0) return 0; 注意与偶数无关
- int target = sum/2;
- dp[0] = 0;
- for(int i = 0; i < stones.size() ; i++)
- {
- for(int j = target; j >= stones[i] ; j--)
- {
- dp[j] = max(dp[j],dp[j - stones[i]]+ stones[i]);
- }
- }
- return sum - dp[target] - dp[target];
- }
- };
Given an array of non-negative integersnums
and an integertarget
。
Add to the beginning of each integer in the array'+'
or'-'
, and then concatenate all the integers to construct aexpression :
nums = [2, 1]
,allowable2
Added before'+'
,exist1
Added 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 totarget
s 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
- class Solution {
- public:
- int findTargetSumWays(vector<int>& nums, int target)
- {
- vector<int> dp(1500,0);
- int sum = 0;
- for(int i = 0; i < nums.size() ; i++)
- {
- sum += nums[i];
- }
- if(abs(target) > sum) return 0;
- if((sum + target)%2 == 1) return 0;
-
- int mid = (sum + target)/2;
- dp[0] = 1;
- for(int i = 0; i < nums.size( ) ; i++)
- {
- for(int j = mid ; j >= nums[i] ; j--)
- {
- dp[j] += dp[j - nums[i]];
- }
- }
- return dp[mid];
- }
- };
Given an array of binary stringsstrs
and two integersm
andn
。
Please find and returnstrs
The length of the largest subset ofmosthavem
indivual0
andn
indivual1
。
ifx
All elements ofy
Elements, collectionsx
Is a collectiony
ofSubset 。
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 。
- class Solution {
- public:
- int findMaxForm(vector<string>& strs, int m, int n)
- {
- vector<vector<int>> dp(m+1,vector<int>(n+1,0));
- for(string str : strs)
- {
- int zero = 0;
- int one = 0;
- for(char c : str)
- {
- if(c == '0') zero++;
- else one++;
-
- }
- for(int i = m; i >= zero ; i--)
- {
- for(int j = n; j >= one; j--)
- {
- dp[i][j] = max(dp[i][j],dp[i - zero][j - one] + 1);
- }
- }
- }
- return dp[m][n];
- }
- };