Daily Question: Leetcode53 Maximum Subarray Sum

Article directory

Series: Greedy Algorithm Training
Language: Java
Topic Source: Leetcode53 Maximum Subarray Sum
Difficulty: Medium
Test Point: Greedy Algorithm Ideas and Understanding && Violent Algorithms


topic description

Given an integer array nums, please find a continuous subarray with the largest sum (the subarray contains at least one element), and return its largest sum. A subarray is a contiguous part of an array.

Example 1: > Input: nums = [-2,1,-3,4,-1,2,1,-5,4] Output: 6 Explanation: Consecutive subarrays of [4,-1,2,1] and a maximum of 6.

Example 2: > Input: nums = [1] Output: 1**** Prompt: > 1 <= nums.length <= 10 5-10 4 <= nums[i] <= 10^4

1. Two ways of thinking:

Violent method and greedy algorithm (friends who can use dp can try to use dp to solve it, here are only two ideas) Violent method : When we get this question, we can use violence when we see it. This question can use two layers The for loop solves it perfectly, but according to the limit of the question, the time complexity does not meet the conditions, and it will definitely time out; the analysis is as follows: the limit length in the question: 1 <= nums.length <= 10 5, two layers of for loop data come to 10 8. The two-layer for loop can only process about 5000 data, so it must time out, but we can exercise our thinking with violence. The reference code is as follows; Greedy algorithm: The greedy algorithm has no template, and the main test is our problem-solving ideas; for In this question, how to achieve local optimum, use a layer of for loop to traverse, that is, the continuous sum is greater than 0, then when the continuous sum is less than 0, we should record the interval sum before 0, and then at i+1, re- Start to calculate the interval, and finally compare the size of multiple intervals to find the largest interval sum; for example: 3 -5 3 1 -3 -1 When i=0, num =3; when i=1, num =-2 ; At this time the continuous sum is less than 0, then we use result to record the interval sum before the continuous sum is 0 at this time is 3; at this time, the head starts from i=0,3, end this interval, and move the head from -5 Start from the next i=2, recalculate num, and then compare the price with the result.

2. Reference Code Violence Law:

class Solution {
    
     
暴力法:  
  public int maxSubArray(int[] nums) {
    
        
      int num =Integer.MIN_VALUE;     
         for(int i =0;i<nums.length;i++){
    
                
         int result =0;         
            int j =i;            
            while(j<nums.length){
    
               
                 result += nums[j];          
                       num=result>num?result:num;                j++;                }        }       
                        return num;    }}```
贪心算法
```java
class Solution {
    
    
//         贪心算法
//  如果连续和为负数了 那么记下此时的为0前的值 从下一个开始从新计数,通过比较下一个区间的和,然后判断    public int maxSubArray(int[] nums) {     
   int result = Integer.MIN_VALUE;     
      int num = 0;      
        for(int i =0;i<nums.length;i++){
    
              
          num += nums[i];         
             if(num>result){
    
                
                 result = num;            }         
                    if(num<0){
    
                    num =0;            }        }        return result;    }}```
以上分析,希望对您有所帮助,您的支持是我最大的动力,感谢一键三连!!

Guess you like

Origin blog.csdn.net/weixin_54174102/article/details/130049616