[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. 【记录】git 拉取远程分支代码,同步到另一个git上

    最近有需求从某git 上拉取所有分支代码同步到另一git上,现记录操作步骤,以便日后使用: 1:先克隆其中一个分支代码到本地环境 git clone -b test http://账号:密码@XXX. ...

  2. linux c 链接详解1-多目标文件链接

    1. 多目标文件的链接 摘自:linux c编程一站式学习 http://learn.akae.cn/media/index.html 可以学会在linux下将多个c语言文件一起编译. 现在我们把例  ...

  3. Vue的路有拦截与axios的封装

    一丶首先我们先创建api与utils两个文件夹 二丶api文件夹里面新建文件api.js import request from "../utils/http" import qs ...

  4. laravel5.6 基于redis,使用消息队列(邮件推送)

    邮件发送如何配置参考:https://www.cnblogs.com/clubs/p/10640682.html 用到的用户表: CREATE TABLE `recruit_users` ( `id` ...

  5. 【leetcode】907. Sum of Subarray Minimums

    题目如下: 解题思路:我的想法对于数组中任意一个元素,找出其左右两边最近的小于自己的元素.例如[1,3,2,4,5,1],元素2左边比自己小的元素是1,那么大于自己的区间就是[3],右边的区间就是[4 ...

  6. Java 基础 - 自动装箱,valueOf装箱,new -->使用 == 和 equals比较

    总结 关于equals 比较  记住:equals方法比较的是真正的值 两个包装类比较,比较的是包装的基本数据类型的值 基本数据类型和包装类型比较时,会先把基本数据类型包装后再比较 (但是因为equa ...

  7. PHP FILTER_CALLBACK 过滤器

    定义和用法 FILTER_CALLBACK 过滤器调用用户自定义函数来过滤数据. 该过滤器为我们提供了对数据过滤的完全控制. 指定的函数必须存入名为 "options" 的关联数组 ...

  8. fedora下手动编译安装vim

    据说手动编译安装可以更适合自己的电脑哦- 1.首先,我门要下载源文件,下载地址:ftp://ftp.vim.org/pub/vim/unix/ 选择一个最新版本,我这里选择的是 vim-7.4.tar ...

  9. 大碗宽面Beta阶段第十一周会议记录

    本周二晚上我们在宿舍楼的大厅进行了本周的小组会议,虽然天气很冷,但大家都还是如数到场,积极参加小组会议.对于上周的任务大家都努力完成,在前端方面,大家完善了主页面和一些分页面,主要有导航栏界面的完成. ...

  10. String StringBuffer BufferBuilder区别

    String 是一个字符串常量,即该对象一旦被创建之后是不可以进行更改的 StringBuffer StringBuilder 是一个字符串变量 StringBuffer 是非线程安全的 但是Stri ...