//c++代码示例
class Solution {
public:
int findMinArrowShots(vector<vector<int>>& points) {
if (points.size() == 0)
{
return 0 ;
}
sort(points.begin(),points.end(),[](const vector<int>& a,const vector<int>& b)
{
return a[0] < b[0] ;
}) ;
int ans = 1 ;
for (int i = 1 ; i < points.size() ; i++)
{
if (points[i][0] <= points[i-1][1])
{
points[i][1] = min(points[i][1],points[i-1][1]) ;
}
else
{
ans++ ;
}
}
return ans ;
}
};
python复制代码
//python代码示例
class Solution:
def findMinArrowShots(self, points: List[List[int]]) -> int:
points.sort(key = lambda x : x[0] ,reverse =False)
if len(points) == 0 :
return 0
ans = 1
for i in range(1,len(points)) :
if (points[i][0] <= points[i-1][1]) :
points[i][1] = min(points[i][1],points[i-1][1])
else :
ans += 1
return ans
//c++代码示例
class Solution {
public:
int eraseOverlapIntervals(vector<vector<int>>& intervals) {
sort(intervals.begin(),intervals.end(),[](const vector<int>& a,const vector<int>& b)
{
return a[0] < b[0] ;
}) ;
int ans = 0 ;
for (int i = 1 ; i < intervals.size() ; i++)
{
if (intervals[i][0] < intervals[i-1][1])
{
intervals[i][1] = min(intervals[i-1][1],intervals[i][1]) ;
ans++ ;
}
}
return ans ;
}
};
python复制代码
//python代码示例
class Solution:
def eraseOverlapIntervals(self, intervals: List[List[int]]) -> int:
intervals.sort(key = lambda x : x[0] ,reverse = False)
if (len(intervals) == 0) :
return 0
ans = 0
for i in range(1,len(intervals)) :
if (intervals[i][0] < intervals[i-1][1]) :
intervals[i][1] = min(intervals[i][1],intervals[i-1][1])
ans += 1
return ans
//c++代码示例
class Solution {
public:
vector<vector<int>> merge(vector<vector<int>>& intervals) {
vector<vector<int>> ans ;
sort(intervals.begin(),intervals.end(),[](const vector<int>& a,const vector<int>& b)
{
return a[0] < b[0] ;
}) ;
for (int i = 0 ; i < intervals.size() ; i++)
{
if (!ans.empty() && intervals[i][0] <= ans[ans.size()-1][1])
{
ans[ans.size()-1] = {min(ans[ans.size()-1][0],intervals[i][0]),max(ans[ans.size()-1][1],intervals[i][1])} ;
}
else
{
ans.push_back({intervals[i][0],intervals[i][1]}) ;
}
}
return ans ;
}
};
python复制代码
//python代码示例
class Solution:
def merge(self, intervals: List[List[int]]) -> List[List[int]]:
ans = []
intervals.sort(key=lambda x: x[0])
for i in range(len(intervals)):
if ans and intervals[i][0] <= ans[-1][1]:
ans[-1] = [min(ans[-1][0], intervals[i][0]), max(ans[-1][1], intervals[i][1])]
else:
ans.append([intervals[i][0], intervals[i][1]])
return ans