leetcode4_452 and 763

leetcode学习算法之贪心算法:

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

763.划分字母区间

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

有一些球形气球贴在一堵用 XY 平面表示的墙面上。墙面上的气球记录在整数数组 points ,其中points[i] = [xstart, xend] 表示水平直径在 xstart 和 xend之间的气球。你不知道气球的确切 y 坐标。

一支弓箭可以沿着 x 轴从不同点 完全垂直 地射出。在坐标 x 处射出一支箭,若有一个气球的直径的开始和结束坐标为 xstart,xend, 且满足 xstart ≤ x ≤ xend,则该气球会被 引爆 。可以射出的弓箭的数量 没有限制 。 弓箭一旦被射出之后,可以无限地前进。

给你一个数组 points ,返回引爆所有气球所必须射出的 最小 弓箭数 。

示例 :

输入:points = [[10,16],[2,8],[1,6],[7,12]]

输出:2

解释:气球可以用2支箭来爆破:

-在x = 6处射出箭,击破气球[2,8]和[1,6]。

-在x = 11处发射箭,击破气球[10,16]和[7,12]。

代码

cpp 复制代码
class Solution {
public:
    int findMinArrowShots(vector<vector<int>>& points) {
        //计算合并后的区间有多少个,先进行排序
        sort(points.begin(), points.end(), [](vector<int>& a, vector<int>& b) {return a[1] < b[1]; });
        int n = points.size(), pre_end = points[0][1];
        for (int i = 1; i < points.size(); i++)
        {
            if (points[i][0] <= pre_end)
            {
                n--;
            }
            else pre_end = points[i][1];
        }
        return n;
    }
};

调用测试

cpp 复制代码
int main() {
    int n;
    cout << "请输入区间的数量: ";
    cin >> n;  // 输入区间个数
    vector<vector<int>> intervals;  // 用于存储所有区间
    cout << "请依次输入每个区间的起始和结束位置(格式:start end):" << endl;
    for (int i = 0; i < n; i++) {
        int start, end;
        cin >> start >> end;  // 输入每个区间的两个数
        intervals.push_back({ start, end });  // 直接构造 vector<int> 并加入
    }
    Solution solution;
    int result = solution.findMinArrowShots(intervals);
    printf("%d", result);
    return 0;
}

这一题参考作者书里题435.无重叠区间的思想进行解题。

贪心策略是:按照右端点从小到大排序,每次判断当前区间与前一个区间是否重叠,有重叠则减少射出剑的数量。

763. 划分字母区间

给你一个字符串 s 。我们要把这个字符串划分为尽可能多的片段,同一字母最多出现在一个片段中。例如,字符串 "ababcc" 能够被分为 ["abab", "cc"],但类似 ["aba", "bcc"] 或 ["ab", "ab", "cc"] 的划分是非法的。

注意,划分结果需要满足:将所有划分结果按顺序连接,得到的字符串仍然是 s 。

返回一个表示每个字符串片段的长度的列表。

示例 :

输入:s = "ababcbacadefegdehijhklij"

输出:[9,7,8]

解释:

划分结果为 "ababcbaca"、"defegde"、"hijhklij" 。

每个字母最多出现在一个片段中。

像 "ababcbacadefegde", "hijhklij" 这样的划分是错误的,因为划分的片段数较少。

代码

cpp 复制代码
class Solution {
public:
    vector<int> partitionLabels(string s) {
        // 步骤1:记录每个字符的首尾位置
        vector<vector<int>> intervals;
        int first[26], last[26];

        // 初始化为 -1
        for (int i = 0; i < 26; i++) {
            first[i] = last[i] = -1;
        }
        // 遍历字符串,记录每个字符的首尾位置
        for (int i = 0; i < s.size(); i++) {
            int idx = s[i] - 'a';
            if (first[idx] == -1) {
                first[idx] = i;
            }
            last[idx] = i;
        }
        // 收集存在的字符区间
        for (int i = 0; i < 26; i++) {
            if (first[i] != -1) {
                intervals.push_back({ first[i], last[i] });
            }
        }
        // 步骤2:按起始位置排序
        sort(intervals.begin(), intervals.end());
        // 步骤3:合并区间
        vector<vector<int>> merged;
        for (auto& interval : intervals) {
            if (merged.empty() || merged.back()[1] < interval[0]) {
                merged.push_back(interval);  // 不重叠,新开区间
            }
            else {
                merged.back()[1] = max(merged.back()[1], interval[1]);  // 重叠,合并
            }
        }
        // 步骤4:计算每个区间的长度
        vector<int> result;
        for (auto& m : merged) {
            result.push_back(m[1] - m[0] + 1);
        }
        return result;
    }
};

调用测试

cpp 复制代码
int main() {
    string s;
    cout << "请输入字符串: ";
    cin >> s;

    Solution solution;
    vector<int> result = solution.partitionLabels(s);
    cout << "每个分区的长度: ";
    for (int len : result) {
        cout << len << " ";
    }
    cout << endl;
    return 0;
}

贪心策略思想步骤:讲题目抽象成区间的问题,思想类似于区间的重叠。

1.首先获取每个字母的首尾位置

2.获取每个字母所在的区间

3.合并区间

4.计算每个区间的长度

题目还得多练,大部分题目可以抽象成一类题目的模板,接下来我要每天更新,冲。