day25打卡

ikun1111發表於2024-10-12

452 用最少數量的箭引爆氣球

class Solution {
public:
static bool cmp(const vector &left, const vector &right)
{
return left[0] < right[0];
}
int findMinArrowShots(vector<vector>& points) {
sort(points.begin(), points.end(), cmp);
int ret = 1;
for(int i = 1; i < points.size(); ++i)
{
if(points[i][0] > points[i-1][1])
{
++ret;
}
else
{
points[i][1] = min(points[i][1], points[i-1][1]);
}
}
return ret;
}
};

435 無重疊區間

class Solution {
public:
static bool cmp(const vector &left, const vector &right)
{
return left[0] < right[0];
}
int eraseOverlapIntervals(vector<vector>& intervals) {
sort(intervals.begin(), intervals.end(), cmp);
int count = 0;
for(int i = 1; i < intervals.size(); ++i)
{
if(intervals[i][0] >= intervals[i-1][1])
{
}
else
{
count++;
intervals[i][1] = min(intervals[i][1], intervals[i-1][1]);
}
}
return count;
}
};

763 劃分字母區間

class Solution {
public:
vector partitionLabels(string s) {
int hash[27] = {0};
for(int i = 0; i < s.size(); ++i)
{
hash[s[i] - 'a'] = i;
}
int left = 0;
int right = 0;
vector ret;
for(int i = 0; i < s.size(); ++i)
{
right = max(right, hash[s[i] - 'a']);
if(i == right)
{
ret.push_back(right - left + 1);
left = i + 1;
}
}
return ret;
}
};

  1. 合併區間

class Solution {
public:
static bool cmp(const vector &left, const vector &right)
{
return left[0] < right[0];
}
vector<vector> merge(vector<vector>& intervals) {
sort(intervals.begin(), intervals.end(), cmp);
vector<vector> ret;
for(int i = 1; i < intervals.size(); ++i)
{
if(intervals[i][0] > intervals[i-1][1])
{
ret.push_back(intervals[i-1]);
}
else
{
intervals[i][1] = max(intervals[i-1][1], intervals[i][1]);
intervals[i][0] = min(intervals[i-1][0], intervals[i][0]);
}
}
ret.push_back(intervals[intervals.size()-1]);
return ret;
}
};