学习目标:
- 860.柠檬水找零
- 406.根据身高重建队列
- 452. 用最少数量的箭引爆气球
学习内容:
860.柠檬水找零
#include <iostream>
#include <vector>
using namespace std;
/*
* @lc app=leetcode.cn id=860 lang=cpp
*
* [860] 柠檬水找零
*
*/
// @lc code=start
class Solution {
public:
bool lemonadeChange(vector<int>& bills) {
//局部最优:遇到账单20,优先消耗美元10,完成本次找零。全局最优:完成全部账单的找零
vector<int> changes(3,0);
for(int i=0;i<bills.size();i++)
{
if(bills[i]==5)
changes[0]++;
else if(bills[i]==10)
{
changes[1]++;
changes[0]--;
}
else
{
changes[2]++;
if(changes[1]!=0)
{
changes[1]--;
changes[0]--;
}
else{
changes[0] -=3;
}
}
if(changes[0] < 0 || changes[1] < 0) {
return false;
}
}
return true;
}
};
// @lc code=end
int main() {
vector<int> bills = {5,5,10,10,20};
Solution solution;
bool test = solution.lemonadeChange(bills);
printf("%s\n", test ? "true" : "false"); // 使用 %s 格式符打印布尔值
return 0;
}
错误以及注意事项
- printf(“%s\n”, test ? “true” : “false”); // 使用 %s 格式符打印布尔值
- 局部最优:遇到20的时候优先使用10元找零,没有的时候再使用5元;全局最优:能完成整个找零工作。
209. 长度最小的子数组
方法1
错误以及注意事项
- 错误
//方法2
错误以及注意事项
学习时间:
2023.11.30 21:00-22:00