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

news/2024/9/22 20:21:27/

## 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.%E5%88%86%E5%8F%91%E9%A5%BC%E5%B9%B2.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.%E6%91%86%E5%8A%A8%E5%BA%8F%E5%88%97.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.%E6%9C%80%E5%A4%A7%E5%AD%90%E5%BA%8F%E5%92%8C.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;  
    }  
}
```


http://www.ppmy.cn/news/1509150.html

相关文章

数学建模——评价决策类算法(层次分析法、Topsis)

一、层次分析法 概念原理 通过相互比较确定各准则对于目标的权重, 及各方案对于每一准则的权重&#xff0c;这些权重在人的思维过程中通常是定性的, 而在层次分析法中则要给出得到权重的定量方法. 将方案层对准则层的权重及准则层对目标层的权重进行综合, 最终确定方案层对目标…

【工作经验】关于远程软件,网络联通方面的异常

NoMachine&#xff0c;ssh&#xff0c;xterm等远程软件 现象1&#xff1a;NoMachine是开机自启的&#xff0c;但是开机后&#xff0c;传输文件失效。 原因&#xff1a;可能是开机时的网络条件不好导致&#xff0c;等网络稳定时&#xff0c;重启NoMachine往往可以解决。 另外&a…

PDF转图片新潮流,4款神器告别手动截图

在这个信息爆炸的时代&#xff0c;PDF文件因为能在各种设备上保持格式不变&#xff0c;成了我们学习和工作中的好帮手。今天&#xff0c;我就诚心诚意地给你推荐几款现在特别流行的PDF转图片工具。这些工具操作起来非常简单&#xff0c;转换速度快&#xff0c;而且转换出来的图…

Python实战项目:天气数据爬取+数据可视化(完整代码)

一、选题的背景 随着人们对天气的关注逐渐增加&#xff0c;天气预报数据的获取与可视化成为了当今的热门话题&#xff0c;天气预报我们每天都会关注&#xff0c;天气情况会影响到我们日常的增减衣物、出行安排等。每天的气温、相对湿度、降水量以及风向风速是关注的焦点。通过…

如何利用数字孪生技术实现可视化

数字孪生技术是指通过将实体系统、设备或流程的数字模型与实时数据和传感器信息相结合&#xff0c;实现对实体系统的仿真、监测、优化和预测。在制造、工业、物流等领域&#xff0c;数字孪生技术被广泛应用来提高效率、降低成本和改善决策质量。以下是如何利用数字孪生技术实现…

视频汇聚/安防综合管理系统EasyCVR非管理员账户能调用分配给其他用户的通道是什么原因?

视频汇聚/安防综合管理系统EasyCVR视频监控平台&#xff0c;作为一款智能视频监控综合管理平台&#xff0c;凭借其强大的视频融合汇聚能力和灵活的视频能力&#xff0c;在各行各业的应用中发挥着越来越重要的作用。平台不仅具备视频资源管理、设备管理、用户管理、网络管理和安…

vue3使用pnpm运行项目但是运行不起来

运行项目的时候发现根本运行不起来了 尝试过创建.npmr文件 删除node_modules重新下 但是都出现问题了 创建.npmr&#xff1a;不管用 删除node_modules重新下&#xff1a;文字编译乱码&#xff0c;utf-8可能解析处理问题 最后解决方法&#xff1a; 重新创建项目&#xff0…

数字信号处理2: 离散信号与系统的频谱分析

文章目录 前言一、实验目的二、实验设备三、实验内容四、实验原理五、实验步骤1.序列的离散傅里叶变换及分析2.利用共轭对称性&#xff0c;设计高效算法计算2个N点实序列的DFT。3.线性卷积及循环卷积的实现及二者关系分析4.比较DFT和FFT的运算时间5.利用FFT求信号频谱及分析采样…