目录
- 455、分发饼干
- 376、摆动序列
- 53、最大子序和
455、分发饼干
讲解:https://programmercarl.com/0455.%E5%88%86%E5%8F%91%E9%A5%BC%E5%B9%B2.html
class Solution {
public:int findContentChildren(vector<int>& g, vector<int>& s) {sort(g.begin(),g.end());sort(s.begin(),s.end());int index = 0;for(int i=0;i<s.size();i++){if (index<g.size() && g[index] <= s[i]){index++;}}return index;}
};
376、摆动序列
讲解:https://programmercarl.com/0376.%E6%91%86%E5%8A%A8%E5%BA%8F%E5%88%97.html
class Solution {
public:int wiggleMaxLength(vector<int>& nums) {if(nums.size() <= 1 ) return nums.size();int curdiff = 0;int prediff = 0;int result = 1;for (int i = 0; i< nums.size() - 1;i++){curdiff = nums[i+1] - nums[i];if ((prediff <= 0 && curdiff >0) || (prediff >=0 && curdiff < 0)){result ++;prediff = curdiff;}}return result; }
};
53、最大子序和
讲解:https://programmercarl.com/0053.%E6%9C%80%E5%A4%A7%E5%AD%90%E5%BA%8F%E5%92%8C.html
class Solution {
public:int maxSubArray(vector<int>& nums) {int result = INT32_MIN;int count = 0;for (int i =0; i < nums.size(); i++){count += nums[i];if (count > result) result = count;if (count <=0 ) count = 0;}return result;}
};