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 g[i], which is the minimum size of a cookie that the child will be content with; and each cookie j has a size `s[j]. If s[j] >= g[i], 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 = nums[i] - nums[i-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 += nums[i];

result = Math.max(cur,result);

if(cur < 0) cur = 0;

}

return result;

}

}

```

相关推荐
真实的菜5 分钟前
Spring Boot 升级全攻略:从 2.2 到 2.7 再到 3.x
java·spring boot·后端
freshman_y14 分钟前
经典的C语言题型
c语言·开发语言·算法
small_wh1te_coder14 分钟前
拷打字节技术总监: 详解c语言嵌入式多线程编程中的头文件 #总结 上下篇合 #
c语言·开发语言·算法·操作系统·嵌入式
凌盛羽14 分钟前
使用python绘图分析电池充电曲线
开发语言·python·stm32·单片机·fpga开发·51单片机
wangjialelele17 分钟前
现代C++:C++17新特性整理
c语言·开发语言·c++·visual studio code
啥都想学点17 分钟前
第17天:JDK、IDEA、MySQL 安装
java·intellij-idea
金銀銅鐵24 分钟前
[Java] Byte Buddy 和 InvocationHandler 的结合
java·后端
肖恭伟28 分钟前
Curso调试Qt:GDB + Qt 官方 qt5printers.py + .gdbinit
开发语言·qt
思茂信息29 分钟前
CST软件加载 Pin 二极管的可重构电桥仿真研究
服务器·开发语言·人工智能·php·cst·电磁仿真·电磁辐射
荔枝吻29 分钟前
【AI总结】C#与.NET:一段跨越20年的命名纠葛与共生传奇
开发语言·c#·.net