leetcode【455,135,435,605,452,122,406,665】【贪心类小合集】【c++版本】

这两天刷了几道贪心算法的题目,稍微记录一下,思路后面在补充。

455. Assign Cookies

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

135. Candy

class Solution {
public:
    int count(int n){
        return n*(n+1)/2;
    }
    int candy(vector<int>& ratings) {
        int n=ratings.size();
        if(n<=1)    return n;
        int result=0, up=0, down=0, old_slope=0;
        for(int i=1; i<n; i++){
            int new_slope = (ratings[i]>ratings[i-1])? 1: (ratings[i]<ratings[i-1] ? -1:0);
            if(old_slope>0 && new_slope==0 || old_slope<0 && new_slope>=0){
                result += count(up) + count(down) + max(up, down);
                up = 0;
                down = 0;
            }
            if(new_slope>0)     up++;
            if(new_slope<0)     down++;
            if(new_slope==0)    result++;
            old_slope = new_slope;
        }
        result += count(up) + count(down) + max(up, down) + 1;
        return result;
    }
};

435. Assign Cookies

class Solution {
public:
    static bool compare(const vector<int> &a, const vector<int> &b) {
        return a[1] < b[1];
    }
    
    int eraseOverlapIntervals(vector<vector<int>>& intervals) {
        if (intervals.size() == 0) {
            return 0;
        }
        int choose = 1;
        sort(intervals.begin(), intervals.end(), compare);
        int right = intervals[0][1];
        for (int i=1; i<intervals.size(); i++) {
            if (intervals[i][0] >= right) {
                choose++;
                right = intervals[i][1];
            }
        }
        return (intervals.size() - choose);
    }
};

605. Can Place Flowers

class Solution {
public:
    bool canPlaceFlowers(vector<int>& flowerbed, int n) {
        if (n == 0) {
            return true;
        }
        flowerbed.push_back(0);
        flowerbed.insert(flowerbed.begin(), 0);
        for (int i=1; i<flowerbed.size()-1; i++) {
            if (flowerbed[i] == 0 && flowerbed[i-1] == 0 && flowerbed[i+1] == 0) {
                n--;
                flowerbed[i] = 1;
            }
            if (n <= 0) {
                return true;
            }
        }
        return false;
    }
};

452. Minimum Number of Arrows to Burst Balloons

class Solution {
public:
    static bool compare(const vector<int> &a, const vector<int> &b) {
        return a[1]<b[1];
    }
    
    int findMinArrowShots(vector<vector<int>>& points) {
        if (points.size() == 0) {
            return 0;
        }
        int result = 1;
        sort(points.begin(), points.end(), compare);
        int right = points[0][1];
        for (int i=1; i<points.size(); i++) {
            if (points[i][0]>right) {
                result++;
                right = points[i][1];
            }
        }
        return result;
    }
};

122. Best Time to Buy and Sell Stock II

class Solution {
public:
    int maxProfit(vector<int>& prices) {
        if(prices.size()<2)     return 0;
        int n = prices.size(), result = 0, tmp = prices[0];
        for(int i=1; i<n; i++){
            if(prices[i]<prices[i-1]){
                result += prices[i-1] - tmp;
                tmp = prices[i];
            }
            else if(i==n-1 && prices[i]>tmp)    result += prices[i] - tmp;
        }
        return result;
    }
};

406. Queue Reconstruction by Height

解法1:

class Solution {
public:
    static bool compare(const vector<int> &a, const vector<int> &b) {
        if (a[1] != b[1]) {
            return a[1] < b[1];
        } else {
            return a[0] < b[0];
        }
    }
    
    vector<vector<int>> reconstructQueue(vector<vector<int>>& people) {
        vector<vector<int>> result;
        sort(people.begin(), people.end(), compare);
        for (int i=0; i<people.size(); i++) {
            if (people[i][1] == 0) {
                result.push_back(people[i]);
            } else {
                int tmp = people[i][1];
                int tmp_length = result.size();
                for (int j=0; j<tmp_length; j++) {
                    if (result[j][0] >= people[i][0]) {
                        tmp--;
                        if (tmp == 0) {
                            while(++j<tmp_length && result[j][0] < people[i][0]);
                            result.insert((result.begin()+j), people[i]);
                            break;
                        }
                    }
                }
            }
        }
        return result;
    }
};

解法2:

class Solution {
public:
    static bool compare(const vector<int> &a, const vector<int> &b) {
        if (a[0] != b[0]) {
            return a[0] > b[0];
        } else {
            return a[1] < b[1];
        }
    }
    
    vector<vector<int>> reconstructQueue(vector<vector<int>>& people) {
        vector<vector<int>> result;
        sort(people.begin(), people.end(), compare);
        for (int i=0; i<people.size(); i++) {
            int pos = people[i][1];
            vector<vector<int>>::iterator it = result.begin();
            while (pos--) {
                it++;
            }
            result.insert(it, people[i]);
        }
        return result;
    }
};

665. 非递减数列(改到中文版)

class Solution {
public:
    bool checkPossibility(vector<int>& nums) {
        bool flag = true;
        for (int i=0; i<nums.size()-1; i++) {
            if (nums[i] > nums[i+1]) {
                if (flag) {
                    if (i==0 || (i>0 && nums[i+1]>=nums[i-1])) {
                        nums[i] = nums[i+1];
                    } else {
                        nums[i+1] = nums[i];
                    }
                    flag = false;
                } else {
                    return false;
                }
            }
        }
        return true ;
    }
};

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值