day31 打卡455.分发饼干 376. 摆动序列 53. 最大子数组和
455.分发饼干
class Solution {
public int findContentChildren(int[] g, int[] s) {
int count = 0;
Arrays.sort(g);
Arrays.sort(s);
int gIndex = 0;
int sIndedx = 0;
while (gIndex<g.length && sIndedx<s.length) {
if (g[gIndex]<=s[sIndedx]) {
gIndex++;
sIndedx++;
count++;
} else {
sIndedx++;
}
}
return count;
}
}
376. 摆动序列
class Solution {
public int wiggleMaxLength(int[] nums) {
if (nums == null || nums.length == 0) return 0;
if (nums.length == 1) return 1;
int preDif = 0;
int curDif = 0;
int count = 1;
for (int i=0; i<nums.length-1 ; i++) {
curDif = nums[i+1] - nums[i];
if ((preDif<=0 && curDif>0) || (preDif>=0 && curDif<0)) {
count++;
preDif = curDif;
}
}
return count;
}
}
53. 最大子数组和
class Solution {
public int maxSubArray(int[] nums) {
int result = Integer.MIN_VALUE;
int count = 0;
for (int i = 0 ; i<nums.length ; i++) {
count += nums[i];
if (count>result) result = count;
if (count<0) count = 0;
}
return result;
}
}