网站备案一次吗,百度手机提高关键词排名,广州app网站开发,手机app软件开发价格算法笔记|Day23贪心算法 ☆☆☆☆☆leetcode 455.分发饼干题目分析代码 ☆☆☆☆☆leetcode 376. 摆动序列题目分析代码 ☆☆☆☆☆leetcode 53. 最大子序和题目分析代码 ☆☆☆☆☆leetcode 455.分发饼干
题目链接#xff1a;leetcode 455.分发饼干
题目分析
优先考虑饼干… 算法笔记|Day23贪心算法 ☆☆☆☆☆leetcode 455.分发饼干题目分析代码 ☆☆☆☆☆leetcode 376. 摆动序列题目分析代码 ☆☆☆☆☆leetcode 53. 最大子序和题目分析代码 ☆☆☆☆☆leetcode 455.分发饼干
题目链接leetcode 455.分发饼干
题目分析
优先考虑饼干先喂饱小胃口若饼干不足以喂孩子则换下一块饼干若能喂则count加一换下一块饼干喂下一个孩子。
代码
class Solution {public int findContentChildren(int[] kid, int[] biscuit) {Arrays.sort(kid);Arrays.sort(biscuit);int count0;int i0;for(int j0;ikid.lengthjbiscuit.length;j){if(biscuit[j]kid[i]){count;i;}}return count;}
}☆☆☆☆☆leetcode 376. 摆动序列
题目链接leetcode 376. 摆动序列
题目分析
此题需要计算前一个差值preDiff和当前差值curDiff若一正一负preDiff0curDiff0||preDiff0curDiff0则出现摆动count加一。在此基础上还应考虑以下三种情况①上下坡中有平坡需要在一侧补充等于0的情况改为preDiff0和preDiff0②数组首尾两端考虑只有两个元素的情况结果应为2可以假设前一个preDiff为0过程中加一结尾自动加一也就是count初始值为1遍历是不考虑最后一个元素③单调坡中有平坡只需要在这个坡度摆动变化的时候更新preDiff就行。
代码
class Solution {public int wiggleMaxLength(int[] nums) {int count1;int preDiff0,curDiff0;for(int i0;inums.length-1;i){curDiffnums[i1]-nums[i];if(preDiff0curDiff0||preDiff0curDiff0){count;preDiffcurDiff;}}return count;}
}☆☆☆☆☆leetcode 53. 最大子序和
题目链接leetcode 53. 最大子序和
题目分析
依次遍历每个元素并求得连续元素和若比当前res值大则赋给res。若当前连续元素和小于0则重新计算元素和sum赋值为0。
代码
class Solution {public int maxSubArray(int[] nums) {int resInteger.MIN_VALUE,sum0;for(int i0;inums.length;i){sumnums[i];ressumres?sum:res;if(sum0)sum0;}return res;}
}