[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. httptesting HTTP(s)接口自动化测试框架

    坐标: https://github.com/HttpTesting/pyhttp # HttpTesting ![PyPI](https://img.shields.io/pypi/v/HttpTe ...

  2. oracle11g rename user导致物化视图失效的处理

    在上一篇文章中,已经点到了数据库改名时,引起该schema下物化视图会失效的问题.从表面上看,该物化视图是删也删不掉,那当然就无法重建了.以下是实验过程: Oracle Database 11g En ...

  3. 【leetcode】688. Knight Probability in Chessboard

    题目如下: On an NxN chessboard, a knight starts at the r-th row and c-th column and attempts to make exa ...

  4. Redis端口配置

    redis.host=192.168.200.128redis.port=6379redis.pass=redis.database=0redis.maxIdle=300redis.maxWait=3 ...

  5. DMA实验总结

    一.RCC设置 没什么好写的之前USART的基本一样 /************************************************************************ ...

  6. 关于Could not open/read file:///etc/pki/rpm-gpg/RPM-GPG-KEY-EPEL-7

    GPG key retrieval failed: [Errno 14] Could not open/read file:///etc/pki/rpm-gpg/RPM-GPG-KEY-EPEL-7 ...

  7. AcWing 241. 楼兰图腾 (树状数组)打卡

    题目:https://www.acwing.com/problem/content/description/243/ 题意:给你n个点,问你 V 和  ^的图腾有多少个 思路:比如V 其实就是找当前点 ...

  8. [Repost] 悬线法

    <浅谈用极大化思想解决最大子矩形问题>作者:王知昆 首先,根据定理1:最大有效子矩形一定是一个极大子矩形.不过与前一种算法不同的是,我们不再要求每一次枚举的一定是极大子矩形而只要求所有的极 ...

  9. 【进阶技术】一篇文章搞掂:Spring高级编程

    本文篇幅较长,建议合理利用右上角目录进行查看(如果没有目录请刷新). 本文基于<Spring5高级编程>一书进行总结和扩展,大家也可以自行研读此书. 十一.任务调度 任务调度主要由三部分组 ...

  10. Nginx网络架构实战学习笔记(六):服务器集群搭建、集群性能测试

    文章目录 服务器集群搭建 Nginx---->php-fpm之间的优化 302机器 202机器 压力测试 搭建memcached.mysql(数据准备) 今晚就动手-.- 集群性能测试 服务器集 ...