455.分发饼干
你有三个孩子和两块小饼干,3个孩子的胃口值分别是:1,2,3。 虽然你有两块小饼干,由于他们的尺寸都是1,你只能让胃口值是1的孩子满足,所以你应该输出1。
输入: g = [1,2,3], s = [1,1]
输出: 1
class Solution {
public int findContentChildren(int[] g, int[] s) {
Arrays.sort(g);
Arrays.sort(s);
int start = 0;
int count = 0;
//优先考虑饼干,小饼干先喂饱小胃口
for (int i = 0; i < s.length && start < g.length; i++){
//饼干 > 胃口
if(s[i] >= g[start]) {
start++;
count++;
}
}
return count;
}
}
53.最大子数相加
输入: nums = [-2,1,-3,4,-1,2,1,-5,4]
输出: 6
解释: 连续子数组 [4,-1,2,1] 的和最大,为 6
class Solution {
public int maxSubArray(int[] nums) {
if(nums.length == 1) return nums[0];
int result = Integer.MIN_VALUE;
int count = 0;
for(int i = 0; i < nums.length; i++){
count += nums[i];
result = Math.max(result,count);
if(count <= 0) count = 0;
}
return result;
}
}