day27 贪心算法-基础+发饼干+摆动序列+最大子序和

8. Greedy

8.1 introduction

核心:通过局部最优达到全局最优。

8.2 455. Assign Cookies

Assume you are an awesome parent and want to give your children some cookies. But, you should give each child at most one cookie.

Each child i has a greed factor gi, which is the minimum size of a cookie that the child will be content with; and each cookie j has a size `sj. If sj >= gi, we can assign the cookie j to the child i, and the child i will be content. Your goal is to maximize the number of your content children and output the maximum number.

https://leetcode.cn/problems/assign-cookies/description/

https://programmercarl.com/0455.分发饼干.html

用大饼干满足大馋孩子/用小饼干满足小孩子

8.3 376. Wiggle Subsequence

A wiggle sequence is a sequence where the differences between successive numbers strictly alternate between positive and negative. The first difference (if one exists) may be either positive or negative. A sequence with one element and a sequence with two non-equal elements are trivially wiggle sequences.

For example, `1, 7, 4, 9, 2, 5 is a wiggle sequence because the differences (6, -3, 5, -7, 3) alternate between positive and negative.

In contrast, `1, 4, 7, 2, 5 and 1, 7, 4, 5, 5 are not wiggle sequences. The first is not because its first two differences are positive, and the second is not because its last difference is zero.

A subsequence is obtained by deleting some elements (possibly zero) from the original sequence, leaving the remaining elements in their original order.

Given an integer array nums, return the length of the longest wiggle subsequence of nums.

https://leetcode.cn/problems/wiggle-subsequence/

https://programmercarl.com/0376.摆动序列.html

删除单调坡上的元素。操作上遇到摆动就++即可。

特殊情况:

1.上下坡中有平坡

2.首尾元素:虚拟头节点+默认尾部有摆动。

3.单调坡中有平坡

```java

public class wiggleSubsequence {

public int wiggleMaxLength(int\[\] nums) {

if(nums.length <= 1)return nums.length;

int preDifference = 0;

int curDifference = 0;

//头节点算上,不遍历它

int result = 1;

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

curDifference = numsi - numsi-1;

if((preDifference >= 0 && curDifference < 0)||(preDifference <= 0 && curDifference > 0)){

result++;

preDifference = curDifference;

}

}

return result;

}

}

class wiggleSubsequenceTest {

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

int\[\] nums = {1,17,5,10,13,13,13,15,10,5,16,8};

wiggleSubsequence example = new wiggleSubsequence();

System.out.println(example.wiggleMaxLength(nums));

}

}

```

8.4 53. Maximum Subarray

Given an integer array nums, find the subarray with the largest sum, and return its sum.

https://leetcode.cn/problems/maximum-subarray/

https://programmercarl.com/0053.最大子序和.html

当连续和是负数的时候,就抛弃

要随时保存最大值。

```java

public class maxSubarray {

public int maxSubArray(int\[\] nums) {

int cur = 0;

int result = Integer.MIN_VALUE;

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

cur += numsi;

result = Math.max(cur,result);

if(cur < 0) cur = 0;

}

return result;

}

}

```

相关推荐
智慧物业老杨1 分钟前
司法绿色通道下的物业纠纷数智化解决方案——基于“三优先“机制的全流程技术落地实践
java·django
2601_961194026 分钟前
2026初级会计实务公式总结大全|计算题公式手册PDF
java·spring·eclipse·pdf·tomcat·hibernate
做个文艺程序员11 分钟前
第1篇:K8s 核心概念精讲:Pod、Deployment、Service 与 Namespace——Java 开发者快速上手指南
java·云原生·容器·kubernetes·容器编排
小欣加油3 小时前
leetcode3751 范围内总波动值I
java·数据结构·c++·算法·leetcode
代码中介商3 小时前
C++左值与右值:核心判断法则详解
开发语言·c++
闪电悠米3 小时前
黑马点评-Redisson-01_why_redisson
java·服务器·网络·数据库·缓存·wpf
星轨zb3 小时前
LangChain4j 集成 Spring Boot:会话记忆 NPE 的根源与 ChatMemoryProvider 正确配置
java·spring boot·后端·langchain4j
JAVA9653 小时前
JAVA面试-并发篇 05-并发包AQS队列实现原理是什么
java·开发语言·面试
JAVA面经实录9173 小时前
RocketMQ全套学习知识手册
java·kafka·rabbitmq·rocketmq
phltxy3 小时前
Spring AI 从提示词到多模态
java·人工智能·spring