代码随想录算法训练营第31天| 455.分发饼干、376. 摆动序列、53. 最大子序和

455.分发饼干

https://leetcode.cn/problems/assign-cookies/

今天怪忙的,没时间写思路了,直接放代码吧

从大到小对齐:

class Solution {
public:
    int findContentChildren(vector& g, vector& s) {
        if (g.empty() || s.empty()) return 0;
        sort(g.begin(), g.end());
        sort(s.begin(), s.end());
        int result = 0;
        int idx1 = g.size() - 1;
        int idx2 = s.size() - 1;
        while (idx1 >= 0 && idx2 >= 0) {
            if (s[idx2] >= g[idx1]) {
                result++;
                idx1--;
                idx2--;
            }
            else {
                idx1--;
            }
        }
        return result;
    }
};

也可以从小到大对齐:

class Solution {
public:
    int findContentChildren(vector& g, vector& s) {
        if (g.empty() || s.empty()) return 0;
        sort(g.begin(), g.end());
        sort(s.begin(), s.end());
        int result = 0;
        int idx1 = 0;
        int idx2 = 0;
        while (idx1 < g.size() && idx2 < s.size()) {
            if (g[idx1] <= s[idx2]) {
                result++;
                idx1++;
                idx2++;
            }
            else {
                idx2++;
            }
        }
        return result;
    }
};

376. 摆动序列 

 https://leetcode.cn/problems/wiggle-subsequence/

自己写的:

class Solution {
public:
    int wiggleMaxLength(vector& nums) {
        if (nums.size() == 0 || nums.size() == 1) return nums.size();
        int idx = 1;
        int diff;
        while(idx < nums.size()) {
            if (nums[idx] == nums[idx - 1]) {
                nums.erase(nums.begin() + idx);
                continue;
            }
            if (idx >= 2 && diff * (nums[idx] - nums[idx - 1]) > 0) {
                diff = nums[idx] - nums[idx - 2];
                nums.erase(nums.begin() + idx - 1);
                continue;
            }
            diff = nums[idx] - nums[idx - 1];
            idx++;
        }
        return nums.size();
    }
};

后面优化的:

class Solution {
public:
    int wiggleMaxLength(vector& nums) {
        if (nums.size() == 0 || nums.size() == 1) return nums.size();
        int idx = 1;
        int diff;
        while(idx < nums.size()) {
            if (nums[idx] == nums[idx - 1]) {
                nums.erase(nums.begin() + idx);
                continue;
            }
            if (idx >= 2 && diff * (nums[idx] - nums[idx - 1]) > 0) {
                diff = nums[idx] - nums[idx - 2];
                nums.erase(nums.begin() + idx - 1);
                continue;
            }
            diff = nums[idx] - nums[idx - 1];
            idx++;
        }
        return nums.size();
    }
};

53. 最大子序和

https://leetcode.cn/problems/maximum-subarray/

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

动态规划:

class Solution {
public:
    int maxSubArray(vector& nums) {
        vector dp(nums.size());
        dp[0] = nums[0];
        int result = dp[0];
        for (int i = 1; i < nums.size(); i++) {
            dp[i] = nums[i];
            if (dp[i - 1] > 0) dp[i] += dp[i - 1];
            if (dp[i] > result) result = dp[i];
        }
        return result;
    }
};

 

 

 

你可能感兴趣的:(算法)