代码随想录算法训练营第三十一天| 455.分发饼干, 376. 摆动序列,53. 最大子序和

455. Assign Cookies

  • 思路
    • java
      //用最大的饼去喂胃口最大的,然后顺下去
      
      
      class Solution {
          public int findContentChildren(int[] g, int[] s) {
              Arrays.sort(g);
              Arrays.sort(s);
              int count =0;
              int start = s.length -1;
              for(int i = g.length - 1; i>=0; i--){
                  if(start >= 0 && g[i] = 0必须在前面
                      start --;
                      count ++;
                  }
              }
              return count;
          }
      }

 376. Wiggle Subsequence

  • 思路53. Maximum Subarray
    • java
      //其实就是看有几个峰
      class Solution {
          public int wiggleMaxLength(int[] nums) {
              if(nums.length <= 1 ){
                  return nums.length;
              }
              int preDiff = 0;
              int curDiff = 0;
              int count = 1;
              for(int i = 1; i < nums.length; i++){
                  curDiff = nums[i] - nums[i-1];
                  if((curDiff > 0 && preDiff <= 0) || (curDiff < 0 && preDiff >= 0)){
                      count += 1;
                      preDiff = curDiff;
                  }
              }
              return count;
          }
      }

 53. Maximum Subarray

  • 思路
    • brute force解法,先确定 sum和max两个参数,然后外循环遍历数组确定subarray的第一个数,内循环依次求和并比较决定是否赋值给max参数,最后返回max
    • DP方法
      • java
        //设定两个变量 preMax用于存当前数结尾,且必须包括当前数的最大子序列和
        //再用curMax存最大值,因为元素可能是负数,可能不+i反而更大所以不能直接返回preMax
        class Solution {
            public int maxSubArray(int[] nums) {
                int preMax = 0;
                int curMax = Integer.MIN_VALUE;
                for(int i : nums){
                    preMax = Math.max(preMax+i, i);
                    curMax = Math.max(curMax, preMax);
                }
                return curMax;
            }
        }
        
        
        /贪心比较容易懂
        class Solution {
            public int maxSubArray(int[] nums) {
                int preSum = 0;
              //这里必须设成Integer.MIN,保证至少后面被更新一次
              //如果初始化为0,那如果数组全为负数,return就是0
                int max = Integer.MIN_VALUE;
                for(int i = 0; i < nums.length; i++){
                  //求累加和  
                    preSum += nums[i];
                    if (preSum > max){
                        max = preSum;
                    }
                  //如果累计和为负数,那对下一个累加的贡献为负
                  //也就是说还不如丢了这个数直接拿下个数来的大,所以重置为0
                    if (preSum <=0){
                        preSum =0;
                    }
                }
                return max;
            }
        }
        
        
        //Time Complexity On
        //Sapce Complexity O1
    • 目前分治法有点难
      • java
        class Solution {
            private int[] numsArray;
            
            public int maxSubArray(int[] nums) {
                numsArray = nums;
                
                // Our helper function is designed to solve this problem for
                // any array - so just call it using the entire input!
                return findBestSubarray(0, numsArray.length - 1);
            }
            
            private int findBestSubarray(int left, int right) {
                // Base case - empty array.
                if (left > right) {
                    return Integer.MIN_VALUE;
                }
                
                int mid = Math.floorDiv(left + right, 2);
                int curr = 0;
                int bestLeftSum = 0;
                int bestRightSum = 0;
                
                // Iterate from the middle to the beginning.
                for (int i = mid - 1; i >= left; i--) {
                    curr += numsArray[i];
                    bestLeftSum = Math.max(bestLeftSum, curr);
                }
                
                // Reset curr and iterate from the middle to the end.
                curr = 0;
                for (int i = mid + 1; i 
    • Time Complexity On*Logn
      • 第一个N是算中间corss的最大值,左右最大值算的时候类似binary sort, logn, 所以一起是n*logn
    • Space Complexity O lognN, recursion stack logn

你可能感兴趣的:(算法,leetcode,数据结构)