435. 无重叠区间
class Solution {
public:
static bool cmp(const vector<int>& a,const vector<int>& b){
return a[0]<b[0];
}
int eraseOverlapIntervals(vector<vector<int>>& intervals) {
int k=0;
sort(intervals.begin(),intervals.end(),cmp);
for(int i=1;i<intervals.size();i++){
if(intervals[i-1][1]>intervals[i][0]){
k++;
intervals[i][1]=min(intervals[i-1][1],intervals[i][1]);
}
}
return k;
}
};
763.划分字母区间
class Solution {
public:
vector<int> partitionLabels(string s) {
vector<int> ret;
vector<int> a(27, 0);
for (int i = 0; i < s.size(); i++)
a[s[i] - 'a'] = i;
int f = -1;
int k = 0;
for (int i = 0; i < s.size(); i++) {
if (a[s[i] - 'a'] > f)
f = a[s[i] - 'a'];
if (f == i) {
ret.push_back(i - k + 1);
k = f+1;//左边界要往前移动一格
}
}
return ret;
}
};
56. 合并区间
class Solution {
public:
static bool cmp(const vector<int>& a, const vector<int>& b) {
return a[0] < b[0];
}
vector<vector<int>> merge(vector<vector<int>>& intervals) {
vector<vector<int>> a;
sort(intervals.begin(), intervals.end(), cmp);
a.push_back(intervals[0]);
for(int i=1;i<intervals.size();i++){
if(a.back()[1]>=intervals[i][0]){
a.back()[1]=max(a.back()[1],intervals[i][1]);
}
else{
a.push_back(intervals[i]);
}
}
return a;
}
};