添加 0763.划分字母区间.md 新增类似引爆气球的思路以及代码

This commit is contained in:
speed
2022-04-07 00:03:02 +08:00
parent e790032c95
commit f70acb963a

View File

@ -77,6 +77,53 @@ public:
但这道题目的思路是很巧妙的,所以有必要介绍给大家做一做,感受一下。
## 补充
这里提供一种与[452.用最少数量的箭引爆气球](https://programmercarl.com/0452.用最少数量的箭引爆气球.html)、[435.无重叠区间](https://programmercarl.com/0435.无重叠区间.html)相同的思路。
统计字符串中所有字符的起始和结束位置,记录这些区间(实际上也就是[435.无重叠区间](https://programmercarl.com/0435.无重叠区间.html)题目里的输入)**将区间按左边界从小到大排序,找到边界将区间划分成组,互不重叠。找到的边界就是答案。**
```CPP
class Solution {
public:
static bool cmp(vector<int> &a, vector<int> &b) {
return a[0] < b[0];
}
// 记录每个字母出现的区间
void countLabels(string s, vector<vector<int>> &hash) {
for (int i = 0; i < s.size(); ++i) {
if (hash[s[i] - 'a'][0] == INT_MIN) {
hash[s[i] - 'a'][0] = i;
}
hash[s[i] - 'a'][1] = i;
}
}
vector<int> partitionLabels(string s) {
vector<int> res;
vector<vector<int>> hash(26, vector<int>(2, INT_MIN));
countLabels(s, hash);
// 按照左边界从小到大排序
sort(hash.begin(), hash.end(), cmp);
// 记录最大右边界
int rightBoard = INT_MIN;
int leftBoard = 0;
for (int i = 0; i < hash.size(); ++i) {
// 过滤掉字符串中没有的字母
if (hash[i][0] == INT_MIN) {
continue;
}
// 一旦下一区间左边界大于当前右边界,即可认为出现分割点
if (rightBoard != INT_MIN && hash[i][0] > rightBoard) {
res.push_back(rightBoard - leftBoard + 1);
leftBoard = hash[i][0];
}
rightBoard = max(rightBoard, hash[i][1]);
}
res.push_back(rightBoard - leftBoard + 1);
return res;
}
};
```
## 其他语言版本