day29 贪心算法-gasStation+candy+lemonadeChange+queueReconstruction

8.9 134. Gas Station

There are n gas stations along a circular route, where the amount of gas at the ith station is gas`i.

You have a car with an unlimited gas tank and it costs cost`i of gas to travel from the ith station to its next (i + 1)th station. You begin the journey with an empty tank at one of the gas stations.

Given two integer arrays gas and cost, return the starting gas station's index if you can travel around the circuit once in the clockwise direction, otherwise return -1. If there exists a solution, it is guaranteed to be unique

https://leetcode.cn/problems/gas-station/description/

https://programmercarl.com/0134.加油站.html

有补充,有消耗,统计每个节点的剩余油量即可。

  1. 全局考虑:

情况一:如果gas的总和小于cost总和,那么无论从哪里出发,一定是跑不了一圈的

情况二:rest`i = gasi-costi为一天剩下的油,i从0开始计算累加到最后一站,如果累加没有出现负数,说明从0出发,油就没有断过,那么0就是起点。

情况三:如果累加的最小值是负数,汽车就要从非0节点出发,从后向前,看哪个节点能把这个负数填平,能把这个负数填平的节点就是出发节点。

  1. 局部考虑:

可以换一个思路,首先如果总油量减去总消耗大于等于零那么一定可以跑完一圈,说明 各个站点的加油站 剩油量resti相加一定是大于等于零的。

每个加油站的剩余量`resti为gasi - costi

i从0开始累加resti,和记为curSum,一旦curSum小于零,说明0, i区间都不能作为起始位置,因为这个区间选择任何一个位置作为起点,到i这里都会断油,那么起始位置从i+1算起,再从0计算curSum。

```java

public class gasStation {

public int canCompleteCircuit(int\[\] gas, int\[\] cost) {

int curSum = 0;//当前节点油耗情况

int totalSum = 0;//总油耗情况

int start = 0;

for (int i = 0; i < gas.length; i++) {

curSum += gasi - costi;

totalSum += gasi - costi;

//当前节点加油量不足以支撑车走到这个节点

if(curSum < 0){

//重新计算油箱剩余油量

curSum = 0;

//从当前节点的后一个再开始计算

start = i + 1;

}

}

if(totalSum < 0) return -1;

return start;

}

}

```

8.10 135. Candy

There are n children standing in a line. Each child is assigned a rating value given in the integer array ratings.

You are giving candies to these children subjected to the following requirements:

Each child must have at least one candy.

Children with a higher rating get more candies than their neighbors.

Return the minimum number of candies you need to have to distribute the candies to the children.

https://leetcode.cn/problems/candy/description/

  1. 分发糖果

本题涉及到一个思想,就是想处理好一边再处理另一边,不要两边想着一起兼顾,后面还会有题目用到这个思路

https://programmercarl.com/0135.分发糖果.html

1.先从左到右遍历:右边孩子得分比左边高

2.再从右到左遍历

```java

public class candy {

public candy(){};

public int candy(int\[\] ratings) {

int\[\] candies = new intratings.length;

candies0 = 1;

//从左到右遍历,如果右边的孩子比左边的孩子得分高,则让右边的孩子多吃一块糖

for (int i = 1; i < ratings.length; i++) {

candiesi = (ratingsi > ratingsi-1) ? candiesi-1 + 1 : 1;

}

//从右向左遍历

for (int i = ratings.length-2; i >= 0; i--) {

if(ratingsi > ratingsi+1){

candiesi = Math.max(candiesi+1+1,candiesi);

}

}

int sum = 0;

for (int i = 0; i < candies.length; i++) {

sum += candiesi;

}

return sum;

}

}

class candyTest {

public static void main(String\[\] args) {

candy example = new candy();

int\[\] rating = {1,2,2,1,3,2};

System.out.println(example.candy(rating));

}

}

```

8.11 860. Lemonade Change

At a lemonade stand, each lemonade costs 5. Customers are standing in a queue to buy from you and order one at a time (in the order specified by bills). Each customer will only buy one lemonade and pay with either a 5, 10, or 20 bill. You must provide the correct change to each customer so that the net transaction is that the customer pays $5.

Note that you do not have any change in hand at first.

Given an integer array bills where billsi is the bill the ith customer pays, return true if you can provide every customer with the correct change, or false otherwise.

https://leetcode.cn/problems/lemonade-change/description/

860.柠檬水找零

本题看上好像挺难,其实很简单,大家先尝试自己做一做。

https://programmercarl.com/0860.柠檬水找零.html

确实不难,自己写出来了。一开始写的有点问题,就是20的找钱可以找三个五块,这个情况漏了。

```java

public class lemonadeChange {

public lemonadeChange() { }

public boolean lemonadeChange(int\[\] bills) {

if(bills0 != 5) return false;

int chargeFive = 0;

int chargeTen = 0;

for (int i = 0; i < bills.length; i++) {

if(billsi == 10){

chargeFive--;

chargeTen++;

}else if(billsi == 20){

if(chargeTen == 0){

chargeFive -= 3;

}else{

chargeFive--;

chargeTen--;

}

}else{

chargeFive++;

}

if(chargeFive < 0 || chargeTen < 0) return false;

}

return true;

}

}

```

8.12 406. Queue Reconstruction by Height

You are given an array of people, people, which are the attributes of some people in a queue (not necessarily in order). Each people`i = hi, ki represents the ith person of height hi with exactly ki other people in front who have a height greater than or equal to hi.

Reconstruct and return the queue that is represented by the input array people. The returned queue should be formatted as an array queue, where queue`j = hj, kj is the attributes of the jth person in the queue (queue0 is the person at the front of the queue).

https://leetcode.cn/problems/queue-reconstruction-by-height/description/

406.根据身高重建队列

本题有点难度,和分发糖果类似,不要两头兼顾,处理好一边再处理另一边。

https://programmercarl.com/0406.根据身高重建队列.html

先把身高从高到低排序,根据k的大小将它插入队列。

不会的地方:

  1. 二维数组排序巧妙解法

  2. 根据k调整数组位置

```java

public class queueReconstruction {

public int\[\]\[\] reconstructQueue(int\[\]\[\] people) {

//sort people array via height in descend order firstly. if two people share the same height, I will sort them via k in ascend order.

//sort(T\[\] a, Comparator<? super T> c) //Sorts the specified array of objects according to the order induced by the specified comparator. Arrays.sort(people, (a, b) -> {

if(a0 == b0) return a1 - b1;

return b0 - a0;}

);

LinkedList<int\[\]> result = new LinkedList<>();

for(int\[\] p : people){

//add(int index, E element): Inserts the specified element at the specified position in this list.

result.add(p1,p);

}

return result.toArray(new intresult.size()\[\]);

}

}

```

相关推荐
小程故事多_806 小时前
从A2C、TRPO、PPO到GRPO,强化学习策略梯度算法完整演进与大模型落地实战解析
人工智能·算法
2601_956121977 小时前
二分算法(知识点+题目)
c++·算法
AndrewHZ10 小时前
【LLM技术全景】阶段总结:技术原理篇核心知识回顾
人工智能·深度学习·算法·语言模型·大模型·llm·芯片开发
小星星闪亮登场11 小时前
2026萌新联赛第三场-- (郑州轻工业大学)
数据结构·c++·经验分享·算法·贪心算法·排序算法·深度优先
冻柠檬飞冰走茶11 小时前
PTA基础编程题目集 7-8超速判断(C++语言实现)
开发语言·数据结构·c++·算法
玖玥拾12 小时前
LeetCode 88 合并两个有序数组
算法·leetcode
数据皮皮侠AI12 小时前
上市公司数字供应链金融指数(2010-2024)
大数据·人工智能·算法
小程故事多_8012 小时前
用GRPO算法重塑多智能体系统,从原理落地到复杂任务规划实战
人工智能·算法
Hi李耶13 小时前
【LeetCode】541.反转字符串 II
算法·leetcode·职场和发展
shylyly_13 小时前
104.二叉树的最大深度
数据结构·算法·104.二叉树的最大深度