当前位置: 首页 > news >正文

代码随想录刷题第34天

第一题是柠檬水找零https://leetcode.cn/problems/lemonade-change/,感觉并没有特别靠近贪心算法,可供讨论的情况非常少,5元收下,10元返5元,20元返15元,对各种找零情况讨论一下即可。

class Solution {
public:bool lemonadeChange(vector<int>& bills) {int five = 0;int ten = 0;for (int bill : bills) {if (bill == 5)five++;if (bill == 10) {if (five == 0)return false;ten++;five--;}if (bill == 20) {if (ten > 0 && five > 0) {ten--;five--;} else if (ten == 0 && five >= 3) {five -= 3;} elsereturn false;}}return true;}
};

第二题是根据身高重建队列https://leetcode.cn/problems/queue-reconstruction-by-height/description/,两个维度,先确定身高,再确定人数,身高从大到小排列后对人数放心插入即可,因为前方都是大数,小数的插入并不影响第二维度。

class Solution {
public:
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>> queue;sort(people.begin(), people.end(), cmp);for (int i = 0; i < people.size(); i++){int position = people[i][1];queue.insert(queue.begin() + position, people[i]); }return queue;}
};

第三题是射气球https://leetcode.cn/problems/minimum-number-of-arrows-to-burst-balloons/description/,想用最小的弓箭数射最多的气球,使气球尽可能重叠就可以了。所以需要将气球左区间进行排列,判断相邻气球的左右区间情况,若当前气球右区间大于上一气球左区间,则需要弓箭数加一。若不大于,则将两气球视为重叠气球,同时更新一下重叠气球的右区间,一遍判断与下一气球的重叠情况。

class Solution {
public:static bool cmp(const vector<int>& a, const vector<int>& b) {return a[0] < b[0];}int findMinArrowShots(vector<vector<int>>& points) {if (points.size() == 0)return 0;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++;elsepoints[i][1] = min(points[i - 1][1], points[i][1]);}return result;}
};

http://www.lryc.cn/news/302273.html

相关文章:

  • AMD FPGA设计优化宝典笔记(5)低频全局复位与高扇出
  • 14. Qt 程序菜单实现,基于QMainWindow
  • 如何利用SpringSecurity进行认证与授权
  • 如何简单上手清华AutoGPT并搭建到本地环境
  • 【漏洞复现-通达OA】通达OA share存在前台SQL注入漏洞
  • HTML5 Canvas与JavaScript携手绘制动态星空背景
  • 如何优雅地与ChatGPT对话?
  • AI提示工程实战:从零开始利用提示工程学习应用大语言模型【文末送书-19】
  • 量子算法入门——3.狄拉克符号与量子态(3)
  • c++ STL系列——(三)list
  • 软考29-上午题-排序
  • 【详细流程】vue+Element UI项目中使用echarts绘制圆环图 折线图 饼图 柱状图
  • Unity之XR Interaction Toolkit如何在VR中实现一个可以拖拽的UI
  • 开源项目热度榜单
  • Ubuntu系统搭建HadSky论坛并结合内网穿透实现无公网ip远程访问
  • gowin GW1N4 LED
  • Linux ipvlan详解(l2、l3、l3s和bridge、private和vepa模式)
  • 理解并实现OpenCV中的图像平滑技术
  • ChatGPT高效提问—prompt实践(白领助手)
  • Code Composer Studio (CCS) - Comment (注释)
  • springboot/ssm校园菜鸟驿站管理系统Java校园快递取件管理系统
  • 【Mybatis】TypeHandler使用
  • [计算机网络]---网络编程套接字
  • 分布式文件系统 SpringBoot+FastDFS+Vue.js【二】
  • 开源软件:推动软件行业繁荣的力量
  • [杂记]mmdetection3.x中的数据流与基本流程详解(数据集读取, 数据增强, 训练)
  • 阿里云香港轻量应用服务器怎么样,建站速度快吗?
  • 事务及在SpringBoot项目中使用的两种方式
  • stm32--笔记
  • 2024前端面试准备之CSS篇(二)