代码随想录刷题第34天
柠檬水找零
/*
* @lc app=leetcode.cn id=860 lang=cpp
*
* [860] 柠檬水找零
*
* https://leetcode.cn/problems/lemonade-change/description/
*
* algorithms
* Easy (58.57%)
* Likes: 414
* Dislikes: 0
* Total Accepted: 154.4K
* Total Submissions: 263.7K
* Testcase Example: '[5,5,5,10,20]'
*
* 在柠檬水摊上,每一杯柠檬水的售价为 5 美元。顾客排队购买你的产品,(按账单 bills 支付的顺序)一次购买一杯。
*
* 每位顾客只买一杯柠檬水,然后向你付 5 美元、10 美元或 20 美元。你必须给每个顾客正确找零,也就是说净交易是每位顾客向你支付 5 美元。
*
* 注意,一开始你手头没有任何零钱。
*
* 给你一个整数数组 bills ,其中 bills[i] 是第 i 位顾客付的账。如果你能给每位顾客正确找零,返回 true ,否则返回 false
* 。
*
*
*
* 示例 1:
*
*
* 输入:bills = [5,5,5,10,20]
* 输出:true
* 解释:
* 前 3 位顾客那里,我们按顺序收取 3 张 5 美元的钞票。
* 第 4 位顾客那里,我们收取一张 10 美元的钞票,并返还 5 美元。
* 第 5 位顾客那里,我们找还一张 10 美元的钞票和一张 5 美元的钞票。
* 由于所有客户都得到了正确的找零,所以我们输出 true。
*
*
* 示例 2:
*
*
* 输入:bills = [5,5,10,10,20]
* 输出:false
* 解释:
* 前 2 位顾客那里,我们按顺序收取 2 张 5 美元的钞票。
* 对于接下来的 2 位顾客,我们收取一张 10 美元的钞票,然后返还 5 美元。
* 对于最后一位顾客,我们无法退回 15 美元,因为我们现在只有两张 10 美元的钞票。
* 由于不是每位顾客都得到了正确的找零,所以答案是 false。
*
*
*
*
* 提示:
*
*
* 1 <= bills.length <= 10^5
* bills[i] 不是 5 就是 10 或是 20
*
*
*/
// @lc code=start
class Solution {
public:
bool lemonadeChange(vector<int>& bills) {
int five = 0,ten = 0,twety = 0;
for(int value : bills){
if (value == 5)
{
five++;
}else if(value == 10){
ten++;
if (five == 0)
{
return false;
}
five--;
}else
{
twety++;
if (ten >= 1 && five >= 1)
{
ten--;
five--;
}else if (five >= 3)
{
five -=3;
}else{
return false;
}
}
}
return true;
}
};
// @lc code=end
406. 根据身高重建队列
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;
class Solution {
public:
//cmp function
static bool cmp(const vector<int>& a,const vector<int>& b){
if (a[0] == b[0])
{
return a[1] < b[1];
}
return a[0] > b[0];
}
vector<vector<int>> reconstructQueue(vector<vector<int>>& people) {
vector<vector<int>> result;
sort(people.begin(),people.end(),cmp);
for (int i = 0; i < people.size(); i++)
{
int position = people[i][1];
result.insert(result.begin() + position,people[i]);
}
return result;
}
};
用最少数量的箭引爆气球
/*
* @lc app=leetcode.cn id=452 lang=cpp
*
* [452] 用最少数量的箭引爆气球
*
* https://leetcode.cn/problems/minimum-number-of-arrows-to-burst-balloons/description/
*
* algorithms
* Medium (50.80%)
* Likes: 731
* Dislikes: 0
* Total Accepted: 183.8K
* Total Submissions: 362K
* Testcase Example: '[[10,16],[2,8],[1,6],[7,12]]'
*
* 有一些球形气球贴在一堵用 XY 平面表示的墙面上。墙面上的气球记录在整数数组 points ,其中points[i] = [xstart, xend]
* 表示水平直径在 xstart 和 xend之间的气球。你不知道气球的确切 y 坐标。
*
* 一支弓箭可以沿着 x 轴从不同点 完全垂直 地射出。在坐标 x 处射出一支箭,若有一个气球的直径的开始和结束坐标为 xstart,xend, 且满足
* xstart ≤ x ≤ xend,则该气球会被 引爆 。可以射出的弓箭的数量 没有限制 。 弓箭一旦被射出之后,可以无限地前进。
*
* 给你一个数组 points ,返回引爆所有气球所必须射出的 最小 弓箭数 。
*
*
* 示例 1:
*
*
* 输入:points = [[10,16],[2,8],[1,6],[7,12]]
* 输出:2
* 解释:气球可以用2支箭来爆破:
* -在x = 6处射出箭,击破气球[2,8]和[1,6]。
* -在x = 11处发射箭,击破气球[10,16]和[7,12]。
*
* 示例 2:
*
*
* 输入:points = [[1,2],[3,4],[5,6],[7,8]]
* 输出:4
* 解释:每个气球需要射出一支箭,总共需要4支箭。
*
* 示例 3:
*
*
* 输入:points = [[1,2],[2,3],[3,4],[4,5]]
* 输出:2
* 解释:气球可以用2支箭来爆破:
* - 在x = 2处发射箭,击破气球[1,2]和[2,3]。
* - 在x = 4处射出箭,击破气球[3,4]和[4,5]。
*
*
*
*
*
* 提示:
*
*
* 1 <= points.length <= 10^5
* points[i].length == 2
* -2^31 <= xstart < xend <= 2^31 - 1
*
*
*/
// @lc code=start
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;
class Solution {
public:
static bool cmp(vector<int> &a,vector<int> &b){
return a[0] < b[0];
}
int findMinArrowShots(vector<vector<int>>& points) {
sort(points.begin(),points.end(),cmp);
int result = 1;
for (int i = 1; i < points.size(); i++)
{
if (points[i][0] > points[i-1][1])
{
result++;
}else {
points[i][1] = min(points[i][1],points[i-1][1]);
}
}
return result;
}
};
// @lc code=end