代码随想录算法训练营day30

1.用最少数量的箭引爆气球

1.1 题目

. - 力扣(LeetCode)

1.2 题解

class Solution {
public:
    int findMinArrowShots(vector>& points) 
    {
        sort(points.begin(), points.end(), [](vector& a, vector& b) {return a[0] < b[0]; });
        int result = 1;
        for (int i = 1; i < points.size(); i++)
        {
            //两个不重叠
            if (points[i][0] > points[i - 1][1])
            {
                //那必须得要一只箭
                result++;
            }
            //挨着的话
            else
            {
                //更新最小右边界
                points[i][1] = min(points[i - 1][1], points[i][1]);
            }
        }
        return result;
    }
};

2.无重叠区间

2.1 题目

. - 力扣(LeetCode)、

2.2 题解

class Solution {
public:
    int eraseOverlapIntervals(vector>& intervals) 
    {
        sort(intervals.begin(), intervals.end(), [](vector& a, vector& b) {return a[0] < b[0]; });

        int result = 1;
        for (int i = 1; i < intervals.size(); i++)
        {
            //不重叠
            if (intervals[i][0] >= intervals[i - 1][1])
            {
                result++;
            }
            else
            {
                //有重叠,更新最小边界
                intervals[i][1] = min(intervals[i - 1][1], intervals[i][1]);
            }
        }

        return intervals.size() - result;
    }
};

3.划分字母区间

3.1 题目

. - 力扣(LeetCode)

3.2 题解

class Solution 
{
public:
    vector partitionLabels(string s) 
    {
        int hash[27] = { 0 };
        for (int i = 0; i < s.size(); i++)
        {
            hash[s[i] - 'a'] = i;
        }
        int left = 0;
        int right = 0;
        vector result;
        for (int i = 0; i < s.size(); i++)
        {
            right = max(right, hash[s[i] - 'a']);
            if (i == right)
            {
                result.push_back(right - left + 1);
                left = right + 1;
            }
        }

        return result;
    }
};

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