[452] Minimum Number of Arrows to Burst Balloons [Medium]

给一堆线段,使用最少的arrow,穿过所有的线段。陈题,第一条线段的终点。

Input:
[[10,16], [2,8], [1,6], [7,12]] Output:
2 Explanation:
One way is to shoot one arrow for example at x = 6 (bursting the balloons [2,8] and [1,6]) and another arrow at x = 11 (bursting the other two balloons).
 // 陈题。 第一条线段的终点。
//wyzhang
class Solution {
public:
static bool cmp(pair<int, int>& a, pair<int, int>& b) {
return a.second < b.second;
} int findMinArrowShots(vector<pair<int, int>>& points) {
sort(points.begin(), points.end(), cmp);
vector<bool> valid_segments(points.size(), true); int ans = ;
for (size_t i = ; i < points.size(); ++i) {
if(!valid_segments[i]) { // balloon has been shot
continue;
}
const int end = points[i].second;
for (size_t j = i + ; j < points.size(); ++j) {
if (!valid_segments[j]) {
continue;
}
if (end >= points[j].first) {
valid_segments[j] = false;
}
}
ans++;
}
return ans;
}
};

[455] Assign Cookies [Easy]

一堆孩子,一堆饼,每个孩子分一块饼,每个孩子对饼的质量有要求,只有达到孩子的要求,他们才会满足,设计一个策略,使得最多的孩子得到满足。

思路:两个数组排个序,然后两个指针直接给。

 //wyzhang
class Solution {
public:
int findContentChildren(vector<int>& g, vector<int>& s) {
sort(g.begin(), g.end());
sort(s.begin(), s.end());
int ans = ;
size_t i = , j = ;
while (i < g.size() && j < s.size()) {
if (g[i] <= s[j]) {
++i, ++j;
ans++;
} else {
++j;
}
}
return ans;
}
};

[406] Queue Reconstruction by Height [Medium]

给一群人按身高排序,每个人都有两个属性(h,k),排序的要求是这个人身高是h, 前面有k个人大于等于这个人的身高。

Input:
[[7,0], [4,4], [7,1], [5,0], [6,1], [5,2]] Output:
[[5,0], [7,0], [5,2], [6,1], [4,4], [7,1]]

思路:按照身高分组,然后做插入排序

 //贪心
//按照身高分组,然后做插入排序
//Space: O(n), time:O(n)
class Solution {
public:
static bool cmp(pair<int, int> a, pair<int, int> b) {
if(a.first == b.first) {
return a.second < b.second;
}
return a.first > b.first;
} vector<pair<int, int>> reconstructQueue(vector<pair<int, int>>& people) {
sort(people.begin(), people.end(), cmp);
vector<pair<int, int>> ans;
for(vector<pair<int, int>>::size_type i = ; i < people.size(); ++i) {
if (people[i] == people[]) {
ans.push_back(people[i]);
continue;
}
if (people[i].second == ans.size()) {
ans.push_back(people[i]);
} else {
auto pos = ans.begin() + people[i].second;
ans.insert(pos, people[i]);
}
}
return ans;
}
}; /*
*先对已有的数组进行排序。按照高度降序排列,如果高度一样,按照k的值升序排列。这样比如一开始7,0 7,1 7,2就会排好,然后比如说后面有一个6,1, 说明只有一个大于或等于它,又因为比6大的已经全部取出。所以把它放在位置1,这样就变成7,0 6,1 7,1 7,2.然后比如又有一个5,0.就放在位置0,以此类推
*/

[134] Gas Station [Medium]

环形路上有很多加油站,油箱容量无上限,每个加油站有gas[i]的汽油,从i到i+1个加油站需要花费cost[i]的汽油。问能不能找到个起点使得汽车跑完全程。

思路见代码注释

 //O(n^2) 超时
//遍历一轮加油站, 如果当前这个station不行的话,从起点到当前station都不能做起点,因为他们都到不了i+1个加油站
//所以,只能从i+1开始选起点。 还有一个限制条件就是汽油的总和小于花费的总和的话,永远跑不完。
class Solution {
public:
int canCompleteCircuit(vector<int>& gas, vector<int>& cost) {
int car = ;
int start = ;
int total = ;
const int N = gas.size();
for (int i = ; i < gas.size(); ++i) {
car += gas[i] - cost[i];
if(car < ) {
car = ;
start = i + ;
}
total += gas[i] - cost[i];
}
return (total >= ) ? start : -;
}
};

[435] Non-overlapping Intervals [Medium]

有若干条线段,可能相互重叠,求删除最少的线段条数,使得每条线段都不重叠。

经典题是给个时间区间让安排活动,在活动时间不冲突的情况下安排尽可能多的活动。

 //其实是如何在不重叠的情况下放下更多的线段。
/**
* Definition for an interval.
* struct Interval {
* int start;
* int end;
* Interval() : start(0), end(0) {}
* Interval(int s, int e) : start(s), end(e) {}
* };
*/
class Solution {
public:
static bool cmp(Interval a, Interval b) {
if (a.end == b.end) {
return a.start < b.start;
}
return a.end < b.end;
}
int eraseOverlapIntervals(vector<Interval>& intervals) {
if (intervals.empty()) {
return ;
}
sort(intervals.begin(), intervals.end(), cmp);
int end = intervals[].end;
int cnt = ;
for (size_t i = ; i < intervals.size(); ++i) {
if (intervals[i].start >= end) {
++cnt;
end = intervals[i].end;
}
}
return intervals.size() - cnt;
}
};

[321] Create Maximum Number [Hard]

给了两个数组,内容是0-9的数字,长度分别是m,n,和一个数字k。 有 m+n>=k。在保持两个数组每个数组的相对位置不变的情况下, 从两个数组中一共选出k个数,使得这个数最大。

从num1里面选出len1个能组成最大数的元素, 从num2里面选出len2个能组成最大数的元素,然后合并。(合并有坑)

合并不能直接归并排序,当两个数相等时,不能直接随便给一个,而是比较哪个数组组成的数字大就选那个数组。。。 

比如说[6,7], [6,  0, 4] ,6相同的情况下应该比较7和 0, 7比0大, 所以选第一个数组中的6. 要是一直都一样,就选长的那个。

 class Solution {
public:
vector<int> maxNumber(vector<int>& nums1, vector<int>& nums2, int k) {
vector<int> ans;
const int m = nums1.size(), n = nums2.size();
int len1 = , len2 = k; //len1 增长, len2 减小
while (len1 <= m && len2 >= ) {
if (len2 > n) {
len2--; ++len1;
continue;
}
//cout << "len1= " << len1 << " len2=" << len2 << endl;
vector<int> a = findMax(nums1, len1);
vector<int> b = findMax(nums2, len2);
cout << vector2string(a) << endl;
cout << vector2string(b) << endl;
vector<int> res = merge(a, b);
compare(ans, res);
len1++, len2--;
}
return ans;
} vector<int> findMax(vector<int> num, int len) {
vector<int> ans(len);
const int n = num.size();
int j = ;
int start = ;
while (j <= len) {
int temp = INT_MIN;
for (int i = start; i < n-(len-j); ++i) {
if (num[i] > temp) {
temp = num[i];
start = i + ;
}
}
ans[j-] = temp;
++j;
}
//cout << __FUNCTION__ << " ans: " << vector2string(ans) << endl;
return ans;
} //merge的时候不能只看某一位是否大,而是应该看组成的数是否大
vector<int> merge (vector<int>& a, vector<int>& b) {
if (a.empty()) return b;
if (b.empty()) return a;
vector<int> ans;
int idx1 = , idx2 = ;
while (idx1 < a.size() && idx2 < b.size()) {
if (a[idx1] < b[idx2]) {
ans.push_back(b[idx2++]);
} else if (a[idx1] > b[idx2]){
ans.push_back(a[idx1++]);
} else {
if (isGreater(a, idx1, b, idx2) == true) { //a greater
ans.push_back(a[idx1++]);
} else {
ans.push_back(b[idx2++]);
}
}
} if (idx1 == a.size()) {
while(idx2 < b.size()) {
ans.push_back(b[idx2++]);
} } else {
while (idx1 < a.size()) {
ans.push_back(a[idx1++]);
}
}
return ans;
} void compare (vector<int>& ans, vector<int>& res) {
if(ans.empty()) {
ans = res;
return;
}
if (ans.size() != res.size()) {
cout << "sth wrong" << endl;
}
for (size_t i = ; i < ans.size(); ++i) {
if (ans[i] == res[i]) {
continue;
} else if (res[i] > ans[i]) {
ans = res;
break;
} else {
break;
}
}
return;
} bool isGreater(const vector<int>& a, int i, const vector<int>& b, int j) {
for ( ; i < a.size() && j < b.size(); ++i, ++j) {
if (a[i] < b[j]) {
return false;
} else if (a[i] > b[j]) {
return true;
}
}
return i != a.size();
} string vector2string(vector<int> a) {
string s = "";
for(auto ele : a) {
s += to_string(ele);
}
return s;
} };

【LeetCode】贪心的更多相关文章

  1. Gas Station|leetcode 贪心

    贪心:尽量将gas[i]-cost[i]>0的放在前面,gas[i]-cost[i]<0的放在后面.(路程的前面有汽油剩下,耗汽油的放在路程的后面). 能否全程通过的 条件 是:sum(g ...

  2. Leetcode 贪心 Best Time to Buy and Sell Stock

    本文为senlie原创.转载请保留此地址:http://blog.csdn.net/zhengsenlie Best Time to Buy and Sell Stock Total Accepted ...

  3. leetcode 贪心算法

    贪心算法中,是以自顶向下的方式使用最优子结构,贪心算法会先做选择,在当时看起来是最优的选择,然后再求解一个结果的子问题. 贪心算法是使所做的选择看起来都是当前最佳的,期望通过所做的局部最优选择来产生一 ...

  4. leetcode: 贪心

    1. jump game Given an array of non-negative integers, you are initially positioned at the first inde ...

  5. LEETCODE —— Best Time to Buy and Sell Stock II [贪心算法]

    Best Time to Buy and Sell Stock II Say you have an array for which the ith element is the price of a ...

  6. Algorithm - 贪心算法使用场景 ( LEETCODE —— Best Time to Buy and Sell Stock II)

    先看一道leetcode题: Best Time to Buy and Sell Stock II Say you have an array for which the ith element is ...

  7. Leetcode 45. Jump Game II(贪心)

    45. Jump Game II 题目链接:https://leetcode.com/problems/jump-game-ii/ Description: Given an array of non ...

  8. [LeetCode] Jump Game II 贪心

    Given an array of non-negative integers, you are initially positioned at the first index of the arra ...

  9. 【LeetCode】盛最多水的容器【双指针+贪心 寻找最大面积】

    给定 n 个非负整数 a1,a2,...,an,每个数代表坐标中的一个点 (i, ai) .在坐标内画 n 条垂直线,垂直线 i 的两个端点分别为 (i, ai) 和 (i, 0).找出其中的两条线, ...

  10. leetcode刷题-- 4. 贪心

    贪心 455分发饼干 假设你是一位很棒的家长,想要给你的孩子们一些小饼干.但是,每个孩子最多只能给一块饼干.对每个孩子 i ,都有一个胃口值 gi ,这是能让孩子们满足胃口的饼干的最小尺寸:并且每块饼 ...

随机推荐

  1. 一 shell编程

    好啦.从今天开始我们转入shell编程的行列.从鸟哥私房菜中,已经学到了一些shell编程的皮毛,这两个月打算系统的学习,学会,学熟练.加油吧 bash shell [root@localhost s ...

  2. python安装pika模块rabbitmq

    1.pip install pika 2.如找不到 拷贝 D:\python\testmq\venv\Lib\site-packages  \pika目录

  3. 5分钟搞定android混淆(转)

    转自:https://www.jianshu.com/p/f3455ecaa56e 前言 混淆是上线前挺重要的一个环节.android使用的ProGuard,可以起到压缩,混淆,预检,优化的作用.但是 ...

  4. jenkins发送邮箱配置,出现Error sending to the following VALID addresses,解决方案

    Jenkins发送邮箱配置,需要的插件:Extended E-mail Notification,邮件通知 1.Manage Jenkins -> Configure System 2.下图是“ ...

  5. 什么是http协议(二)

    一.概念 协议是指计算机通信网络中两台计算机之间进行通信所必须共同遵守的规定或规则,超文本传输协议(HTTP)是一种通信协议,它允许将超文本标记语言(HTML)文档从Web服务器传送到客户端的浏览器. ...

  6. Mac上无法使用蓝牙时的7个修复程序

    蓝牙是一个很普遍的技术,除非它出现问题,你才会发现它.例如,你听歌很顺利时,直到AirPods突然断连.大多数时候,这是一个相对容易的修复,但有时可能会有些棘手. https://www.macdow ...

  7. 在 IntelliJ IDEA 中这样使用 Git,效率提升2倍以上

    1.Git简介 Git是目前流行的分布式版本管理系统.它拥有两套版本库,本地库和远程库,在不进行合并和删除之类的操作时这两套版本库互不影响.也因此其近乎所有的操作都是本地执行,所以在断网的情况下任然可 ...

  8. Vue学习笔记【33】——相关文章

    vue.js 1.x 文档 vue.js 2.x 文档 String.prototype.padStart(maxLength, fillString) js 里面的键盘事件对应的键码 Vue.js双 ...

  9. mysql 查询表的最大时间 的数据

    SELECT * from (SELECT MAX(a.update_date) as q ,a.monitoring_point_id from biz_monitoring_point_recor ...

  10. 《ArcGIS Runtime SDK for .NET开发笔记》 --Hello Word

    这里我们将创建第一个用于显示地图的APP. 1.新建一个WPF程序 首先我们打开Visual Studio,选择新建项目. 选择已安装——模板——Windows桌面——WPF应用程序  2.添加Run ...