算法训练营笔记day31|455. 分发饼干、376. 摆动序列、53. 最大子数组和

455. 分发饼干

题目连接

笔记

class Solution {
public:
    int findContentChildren(vector<int>& g, vector<int>& s) {
        sort(g.begin(), g.end());
        sort(s.begin(), s.end());
        int count = 0;
        int i = s.size() - 1;
        int j = g.size() - 1;
        for (; j >= 0; j--) {
            if (i >= 0 && s[i] >= g[j]) {
                count++;
                i--;
            }
        }
        return count;

    }
};

376. 摆动序列

题目连接

笔记

class Solution {
public:
    int wiggleMaxLength(vector<int>& nums) {
        if (nums.size() <= 1) return nums.size();
        int count = 1;
        int curdiff = 0;
        int prediff = 0;

        for (int i = 0; i < nums.size() - 1; i++) {
            curdiff = nums[i + 1] - nums[i];
            if (prediff <= 0 && curdiff > 0 || prediff >= 0 && curdiff < 0) {
                count++;
                prediff = curdiff;
            }
        }
        return count;
    }
};

53. 最大子数组和

题目连接

笔记

class Solution {
public:
    int maxSubArray(vector<int>& nums) {
        int sum = 0;
        int max = INT_MIN;
        for (int i = 0; i < nums.size(); i++) {
            sum += nums[i];
            if (sum > max) max = sum;
            if (sum < 0) {
                sum = 0;
            }
        }
        return max;
    }
};

你可能感兴趣的:(算法,leetcode,c++)