这两天刷了几道贪心算法的题目,稍微记录一下,思路后面在补充。
455. Assign Cookies
class Solution {
public:
int findContentChildren(vector<int>& g, vector<int>& s) {
if (s.size() == 0) {
return 0;
}
sort(g.begin(), g.end());
sort(s.begin(), s.end());
int result = 0, i = 0, j = 0;
for (i=0; i<g.size(); i++) {
for (; j<s.size(); j++) {
if (g[i] <= s[j]) {
result++;
j++;
break;
}
}
}
return result;
}
};
135. Candy
class Solution {
public:
int count(int n){
return n*(n+1)/2;
}
int candy(vector<int>& ratings) {
int n=ratings.size();
if(n<=1) return n;
int result=0, up=0, down=0, old_slope=0;
for(int i=1; i<n; i++){
int new_slope = (ratings[i]>ratings[i-1])? 1: (ratings[i]<ratings[i-1] ? -1:0);
if(old_slope>0 && new_slope==0 || old_slope<0 && new_slope>=0){
result += count(up) + count(down) + max(up, down);
up = 0;
down = 0;
}
if(new_slope>0) up++;
if(new_slope<0) down++;
if(new_slope==0) result++;
old_slope = new_slope;
}
result += count(up) + count(down) + max(up, down) + 1;
return result;
}
};
435. Assign Cookies
class Solution {
public:
static bool compare(const vector<int> &a, const vector<int> &b) {
return a[1] < b[1];
}
int eraseOverlapIntervals(vector<vector<int>>& intervals) {
if (intervals.size() == 0) {
return 0;
}
int choose = 1;
sort(intervals.begin(), intervals.end(), compare);
int right = intervals[0][1];
for (int i=1; i<intervals.size(); i++) {
if (intervals[i][0] >= right) {
choose++;
right = intervals[i][1];
}
}
return (intervals.size() - choose);
}
};
605. Can Place Flowers
class Solution {
public:
bool canPlaceFlowers(vector<int>& flowerbed, int n) {
if (n == 0) {
return true;
}
flowerbed.push_back(0);
flowerbed.insert(flowerbed.begin(), 0);
for (int i=1; i<flowerbed.size()-1; i++) {
if (flowerbed[i] == 0 && flowerbed[i-1] == 0 && flowerbed[i+1] == 0) {
n--;
flowerbed[i] = 1;
}
if (n <= 0) {
return true;
}
}
return false;
}
};
452. Minimum Number of Arrows to Burst Balloons
class Solution {
public:
static bool compare(const vector<int> &a, const vector<int> &b) {
return a[1]<b[1];
}
int findMinArrowShots(vector<vector<int>>& points) {
if (points.size() == 0) {
return 0;
}
int result = 1;
sort(points.begin(), points.end(), compare);
int right = points[0][1];
for (int i=1; i<points.size(); i++) {
if (points[i][0]>right) {
result++;
right = points[i][1];
}
}
return result;
}
};
122. Best Time to Buy and Sell Stock II
class Solution {
public:
int maxProfit(vector<int>& prices) {
if(prices.size()<2) return 0;
int n = prices.size(), result = 0, tmp = prices[0];
for(int i=1; i<n; i++){
if(prices[i]<prices[i-1]){
result += prices[i-1] - tmp;
tmp = prices[i];
}
else if(i==n-1 && prices[i]>tmp) result += prices[i] - tmp;
}
return result;
}
};
406. Queue Reconstruction by Height
解法1:
class Solution {
public:
static bool compare(const vector<int> &a, const vector<int> &b) {
if (a[1] != b[1]) {
return a[1] < b[1];
} else {
return a[0] < b[0];
}
}
vector<vector<int>> reconstructQueue(vector<vector<int>>& people) {
vector<vector<int>> result;
sort(people.begin(), people.end(), compare);
for (int i=0; i<people.size(); i++) {
if (people[i][1] == 0) {
result.push_back(people[i]);
} else {
int tmp = people[i][1];
int tmp_length = result.size();
for (int j=0; j<tmp_length; j++) {
if (result[j][0] >= people[i][0]) {
tmp--;
if (tmp == 0) {
while(++j<tmp_length && result[j][0] < people[i][0]);
result.insert((result.begin()+j), people[i]);
break;
}
}
}
}
}
return result;
}
};
解法2:
class Solution {
public:
static bool compare(const vector<int> &a, const vector<int> &b) {
if (a[0] != b[0]) {
return a[0] > b[0];
} else {
return a[1] < b[1];
}
}
vector<vector<int>> reconstructQueue(vector<vector<int>>& people) {
vector<vector<int>> result;
sort(people.begin(), people.end(), compare);
for (int i=0; i<people.size(); i++) {
int pos = people[i][1];
vector<vector<int>>::iterator it = result.begin();
while (pos--) {
it++;
}
result.insert(it, people[i]);
}
return result;
}
};
665. 非递减数列(改到中文版)
class Solution {
public:
bool checkPossibility(vector<int>& nums) {
bool flag = true;
for (int i=0; i<nums.size()-1; i++) {
if (nums[i] > nums[i+1]) {
if (flag) {
if (i==0 || (i>0 && nums[i+1]>=nums[i-1])) {
nums[i] = nums[i+1];
} else {
nums[i+1] = nums[i];
}
flag = false;
} else {
return false;
}
}
}
return true ;
}
};