东湖之滨 发表于 2024-8-14 22:40:17

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, which is the minimum size of a cookie that the child will be content with; and each cookie j has a size `s. If s >= g, 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, ` is a wiggle sequence because the differences (6, -3, 5, -7, 3) alternate between positive and negative.
In contrast, ` and 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 - nums;  
            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;  
            result = Math.max(cur,result);  
            if(cur < 0) cur = 0;  
        }  
        return result;  
    }  
}
```

免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。
页: [1]
查看完整版本: day27 贪默算法-底子+发饼干+摆动序列+最大子序和