【LeetCode】贪心
[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】贪心的更多相关文章
- Gas Station|leetcode 贪心
贪心:尽量将gas[i]-cost[i]>0的放在前面,gas[i]-cost[i]<0的放在后面.(路程的前面有汽油剩下,耗汽油的放在路程的后面). 能否全程通过的 条件 是:sum(g ...
- Leetcode 贪心 Best Time to Buy and Sell Stock
本文为senlie原创.转载请保留此地址:http://blog.csdn.net/zhengsenlie Best Time to Buy and Sell Stock Total Accepted ...
- leetcode 贪心算法
贪心算法中,是以自顶向下的方式使用最优子结构,贪心算法会先做选择,在当时看起来是最优的选择,然后再求解一个结果的子问题. 贪心算法是使所做的选择看起来都是当前最佳的,期望通过所做的局部最优选择来产生一 ...
- leetcode: 贪心
1. jump game Given an array of non-negative integers, you are initially positioned at the first inde ...
- 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 ...
- 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 ...
- Leetcode 45. Jump Game II(贪心)
45. Jump Game II 题目链接:https://leetcode.com/problems/jump-game-ii/ Description: Given an array of non ...
- [LeetCode] Jump Game II 贪心
Given an array of non-negative integers, you are initially positioned at the first index of the arra ...
- 【LeetCode】盛最多水的容器【双指针+贪心 寻找最大面积】
给定 n 个非负整数 a1,a2,...,an,每个数代表坐标中的一个点 (i, ai) .在坐标内画 n 条垂直线,垂直线 i 的两个端点分别为 (i, ai) 和 (i, 0).找出其中的两条线, ...
- leetcode刷题-- 4. 贪心
贪心 455分发饼干 假设你是一位很棒的家长,想要给你的孩子们一些小饼干.但是,每个孩子最多只能给一块饼干.对每个孩子 i ,都有一个胃口值 gi ,这是能让孩子们满足胃口的饼干的最小尺寸:并且每块饼 ...
随机推荐
- shell函数的存储和显示
- Oracle 19C的下载和安装部署
1.官网下载zip包. 2.解压到/usr/local/oracle 目录. 3.创建用户和用户组 /usr/sbin/useradd -u oracle //用户组oracle /usr/sbin/ ...
- MYSQL中判断函数有哪些
新建一张客户表,如下:sex:1-男,2-女,3-未知:level是客户的级别:1-超级VIP客户,2-VIP客户,3-普通客户 方式一:case函数:流程控制函数 用法一: CASE express ...
- leetcode-第14周双周赛-1273-删除树节点
题目描述: 自己的提交:动态规划 class Solution: def deleteTreeNodes(self, nodes: int, parent: List[int], value: Lis ...
- HashMap常见面试题
1.HashMap底层是通过什么来实现的? 在JDK1.7中是通过数组+链表来实现的: 在JDK1.8中是通过数组+链表+红黑树来实现的 2.HashMap在JDK1.8中为什么使用红黑树? 为了弥补 ...
- Jetson Nano系列教程0:初识Jetson Nano
关于Jetson Nano Developer Kit Jetson nano搭载四核Cortex-A57 MPCore 处理器,采用128 核 Maxwell™ GPU.支持JetPack SDK ...
- Linux系统之-TCP-IP链路层
一.基本 网络层协议的数据单元是 IP 数据报 ,而数据链路层的工作就是把网络层交下来的 IP 数据报 封装为 帧(frame)发送到链路上,以及把接收到的帧中的数据取出并上交给网络层. 为达到这一目 ...
- ssm项目配置多个数据源
在项目中到一些问题,一些查询模块需要链接另一个数据库,这时,就可以配置两个数据源进行操作. 1.创建jdbc.properties jdbc.url = jdbc:mysql://localhost: ...
- docker-compose快速搭建 Prometheus+Grafana监控系统
一.说明Prometheus负责收集数据,Grafana负责展示数据.其中采用Prometheus 中的 Exporter含:1)Node Exporter,负责收集 host 硬件和操作系统数据.它 ...
- 20175203 2018-2019-2《Java程序设计》第四周学习总结
## 教材学习内容总结 第五章内容知识点总结: 子类父类的定义格式: class 子类名 extends 父类名 { } 定义子类时用extends Object类是所有类的祖先类,即最原始类. 子类 ...