代码随想录算法训练营Day31|贪心算法01
一、455. 分发饼干
class Solution {
public int findContentChildren ( int [ ] g, int [ ] s) {
Arrays . sort ( g) ;
Arrays . sort ( s) ;
int count= 0 ;
int len= s. length- 1 ;
for ( int i= g. length- 1 ; i>= 0 ; i-- ) {
if ( len>= 0 && g[ i] <= s[ len] ) {
len-- ;
count++ ;
}
}
return count;
}
}
二、376. 摆动序列
class Solution {
public int wiggleMaxLength ( int [ ] nums) {
int len= nums. length;
if ( len<= 1 ) {
return len;
}
int pre= 0 ;
int cur= 0 ;
int reslut= 1 ;
for ( int i= 1 ; i< len; i++ ) {
cur= nums[ i] - nums[ i- 1 ] ;
if ( cur> 0 && pre<= 0 || cur< 0 && pre>= 0 ) {
reslut++ ;
pre= cur;
}
}
return reslut;
}
}
三、53. 最大子数组和
class Solution {
public int maxSubArray ( int [ ] nums) {
int pre = 0 , max = nums[ 0 ] ;
for ( int x : nums) {
pre = Math . max ( pre + x, x) ;
max = Math . max ( max, pre) ;
}
return max;
}
}