class Solution {
public:
int findContentChildren(vector<int>& g, vector<int>& s) {
sort(g.begin(), g.end());
sort(s.begin(), s.end());
int index = s.size() - 1;
int result = 0;
for (int i = g.size() - 1; i >= 0; i--)
{
if (index >= 0 && s[index] >= g[i])
{
result++;
index--;
}
}
return result;
}
};
376摆动序列
class Solution {
public:
int dp[1005][2];
int wiggleMaxLength(vector<int>& nums)
{
memset(dp, 0, sizeof dp);
dp[0][0] = dp[0][1] = 1;
for (int i = 1; i < nums.size(); ++i)
{
dp[i][0] = dp[i][1] = 1;
for (int j = 0; j < i; ++j)
{
if (nums[j] > nums[i]) dp[i][1] = max(dp[i][1], dp[j][0] + 1);
}
for (int j = 0; j < i; ++j) {
if (nums[j] < nums[i]) dp[i][0] = max(dp[i][0], dp[j][1] + 1);
}
}
return max(dp[nums.size() - 1][0], dp[nums.size() - 1][1]);
}
};
53最大子数组和
class Solution {
public:
int maxSubArray(vector<int>& nums)
{
int n=nums.size();
vector<int>dp(n,0);
dp[0]=nums[0];
for(int i=1;i<nums.size();i++)
{
dp[i] = max(nums[i], nums[i]+dp[i-1]);
}
int res = INT_MIN;
for (int i = 0; i < nums.size(); i++) {
res = max(res, dp[i]);
}
return res;
}
};