LeetCode Practice: Greedy

Intentional practice is the key to success.


455. Assign Cookies

URL: https://leetcode-cn.com/problems/assign-cookies/
Description: Given two lists. The first one is the mininum sizes of a cookie that the child will be content with. The second one is the sizes of each cookie.

class Solution {
public:
    int findContentChildren(vector<int>& g, vector<int>& s) {
        std::sort(g.begin(), g.end());
        std::sort(s.begin(), s.end());
        int gp = 0, sp = 0;
        while (gp < g.size() && sp < s.size()) {
            if (g[gp] <= s[sp]) {
                // Mean this quantity of cookie is capable of making this kid content.
                ++gp;
            }
            ++sp;
        }
        return gp;
    }
};

135. Candy

URL: https://leetcode-cn.com/problems/candy/
Description: Given a vector of student scores. Assign them with appropriate candies. There are several rules: 1. each child must have at least 1 candy; 2. children with a higher score get more candies than their neighbors.

class Solution {
public:
    int candy(vector<int>& ratings) {
        int size = ratings.size();
        if (size < 2) return size;
        std::vector<int> candies(size, 1);

        // Loop1 from 1 -> size-1.
        // If the right is greater than the left, candies[right] = candies[left] + 1.
        for (int i = 1; i < size; ++i) {
            if (ratings[i-1] < ratings[i]) candies[i] = candies[i-1] + 1;
        }
        // Loop2 from size-2 -> 0.
        // If the left is greater than the right,
        //      if candies[i] <= candies[i+1], candies[i] = candies[i+1] + 1
        for (int i = size-2; i >= 0; --i) {
            if (ratings[i] > ratings[i+1])
                if (candies[i] <= candies[i+1])
                    candies[i] = candies[i+1] + 1;
        }

        return std::accumulate(candies.begin(), candies.end(), 0);
    }
};

Besides, there are another solution for loop2 through std function.

for (int i = size-2; i >= 0; --i)
  if (ratings[i] > ratings[i+1])
    candies[i] = std::max(candies[i], candies[i+1]+1);

435. Non-overlapping Intervals

URL: https://leetcode-cn.com/problems/non-overlapping-intervals/
Description: Given a vector contains 2-dimensional vectors which have [1]:start and [2]:end. Return the minimum number of intervals removed to make the rest of the intervals non-overlapping.

class Solution {
public:
    int eraseOverlapIntervals(vector<vector<int>>& intervals) {
        if (intervals.empty()) return 0;
        int size = intervals.size();
        // Sort intervals according to the end(i).
        std::sort(intervals.begin(), intervals.end(), [](vector<int>& a, vector<int>& b){
            return a[1] < b[1];
        });

        int removed = 0, prev = intervals[0][1];
        for (int i = 1; i < size; ++i) {
            if (prev > intervals[i][0])
                ++removed;
            else
                prev = intervals[i][1];
        }
        return removed;
    }
};

The lambda function in the solution can also be accurated as follows:

// Considered a special case: when a[i] == b[i], make the smaller one in position [0] to the front.
return (a[1] < b[1]) | (a[1] == b[1] && a[0] < b[0]);