435. 无重叠区间
class Solution {
private:
static bool cmp(vector<int>& a,vector<int>& b){
return a[1]<b[1];
}
public:
int eraseOverlapIntervals(vector<vector<int>>& intervals) {
if(intervals.size()==0) return 0;
sort(intervals.begin(),intervals.end(),cmp);
int count=1;
int end=intervals[0][1];
for(int i=1;i<intervals.size();i++){
if(end<=intervals[i][0]){
end=intervals[i][1];
count++;
}
}
return intervals.size()-count;
}
};
763. 划分字母区间
class Solution {
public:
vector<int> partitionLabels(string s) {
vector<int> result;
int ch[27]={0};
int right=0;
int left=0;
if(s.size()==0) return result;
for(int i=0;i<s.size();i++){
ch[s[i]-'a']=i;
}
for(int i=0;i<s.size();i++){
right=max(right,ch[s[i]-'a']);
if(i==right){
result.push_back(right-left+1);
left=i+1;
}
}
return result;
}
};
56. 合并区间
class Solution {
private:
static bool cmp(vector<int>& a,vector<int>& b){
return a[0]<b[0];
}
public:
vector<vector<int>> merge(vector<vector<int>>& intervals) {
vector<vector<int>> result;
int flag=false;
if(intervals.size()==0) return result;
sort(intervals.begin(),intervals.end(),cmp);
for(int i=1;i<intervals.size();i++){
int start=intervals[i-1][0];
int end=intervals[i-1][1];
while(i<intervals.size()&&end>=intervals[i][0]){
end=max(end,intervals[i][1]);
if(i==intervals.size()-1) flag=true;
i++;
}
result.push_back({start,end});
}
if(flag==false){
result.push_back({intervals[intervals.size()-1][0],intervals[intervals.size()-1][1]});
}
return result;
}
};