[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. CSS中强大的EM(转)

    转自:https://www.w3cplus.com/css/px-to-em CSS中强大的EM 作者:大漠 日期:2011-10-27 点击:97370 em 长度单位 编辑推荐:3月31日前,点 ...

  2. Sql语法整理-图片版....

  3. 【NOIP2019模拟2019.10.07】果实摘取 (约瑟夫环、Mobius反演、类欧、Stern-Brocot Tree)

    Description: 小 D 的家门口有一片果树林,果树上果实成熟了,小 D 想要摘下它们. 为了便于描述问题,我们假设小 D 的家在二维平面上的 (0, 0) 点,所有坐标范围的绝对值不超过 N ...

  4. Struts2 简单的增删改查

    1:主页面 <a href="emp-list">emp-list</a> <br> 然后到struts.xml文件中找到对应的emp-list ...

  5. UNP学习第13章 守护进程和inetd超级服务器

    Unix系统中的syslogd守护进程通常由某个系统初始化脚本启动,而且在系统工作期间一直运行. 源自Berkeley的syslogd实现在启动时执行以下步骤. (1)读取配置文件.通常为/etc/s ...

  6. hdu 4705 Y (树形dp)

    Description Input 4 1 2 1 3 1 4 题目的意思是给你一棵树,让你找到所有不在一条路径上的三个点的情况个数.乍一看正向处理比较麻烦,我们从反方向考虑,如果是取在一条路径上的3 ...

  7. 「NOI2016」区间 解题报告

    「NOI2016」区间 最近思维好僵硬啊... 一上来就觉得先把区间拆成两个端点进行差分,然后扫描位置序列,在每个位置维护答案,用数据结构维护当前位置的区间序列,但是不会维护. 于是想研究性质,想到为 ...

  8. 72、salesforce call RESTful 的方式

    通过Chrome的Postman 来call salesforce的restful api https://login.salesforce.com/services/oauth2/token?gra ...

  9. D3DPT_TRIANGLESTRIP 与 D3DPT_TRIANGLEFAN 的区别

    D3DPT_TRIANGLESTRIP D3DPT_TRIANGLEFAN http://community.fortunecity.ws/skyscraper/sun/575/Programs/D3 ...

  10. Linux下tomcat启动慢,阻塞

    声明:本文为转载,请尊重版权,原文地址: https://www.cnblogs.com/songjinju/p/7505564.html 这两天在linux部署完tomcat以后,发现每次启动都非常 ...