[Likou] 53. The largest sub-array and the bucket with the most water

Offer arrives, dig friends to pick up! I am participating in the 2022 Spring Recruitment Check-In Event, click to view the event details .

The 29th article on the 21st day of the spring recruit punch card.

Diligent study is like a seedling that springs up, it does not increase, but it grows every day; dropping out of school is like a stone for sharpening a knife, it does not see its loss, but it loses every day.

There are so many activities for the Nuggets. This month, I decided to use go to brush questions every day, on the one hand to improve the algorithm level, on the other hand to precipitate the learning of the go language.

Let's GO!

Topic description

Given an integer array nums, please find a contiguous 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 

Example 1:

Input: nums = [-2,1,-3,4,-1,2,1,-5,4]

Output: 6

Explanation: The maximum sum of consecutive subarrays [4,-1,2,1] is 6 .

Example 2:

Input: nums = [1]

output: 1

Example 3:

Input: nums = [5,4,-1,7,8]

output: 23  

hint:

1 <= nums.length <= 1 0 5 10^{5}

1 0 4 -10^{4} <= nums[i] <= 1 0 4 10^{4}

topic analysis

This question is a bit similar to how much water can a bucket hold before. The problem with the most water in a bucket 双指针is the optimal solution.

This problem has been tested and calculated, and 动态规划the idea found is the optimal solution:

We can split the problem-solving idea into, each node is compared with the previous node, that is: is the value of the current node larger? Or is the value of current node + previous node large?

Split the problem and keep doing this comparison until you get the largest value.

Explanation of ideas

  1. Initialize the maximum value max, because from scratch, the default maximum value is the value corresponding to the array subscript 0.
  2. Loop traversal, the number of times to traverse the length of the array
  3. Carry out the logical judgment in the topic analysis in the for loop:
  4. If the value of the current node + the value of the previous node > the value of the current node: we update the value of the current node to the value of the previous node + the current node value.
  5. Judging in the for loop, if the added current node value is greater than the current maximum value max, update max
  6. Continue to loop judgment until the end
  7. Just return max, which is the largest subarray we need and

AC code

func maxSubArray(nums []int) int {
    max := nums[0]
    for i := 1; i < len(nums); i++ {
        if nums[i] + nums[i-1] > nums[i] {
            nums[i] += nums[i-1]
        }
        if nums[i] > max {
            max = nums[i]
        }
    }
    return max
}
复制代码

operation result

image.png

Summarize

the complexity

Time Complexity: O(n), where n is the length of the input array.

Space complexity: O(1), because we only need constant space to store variables.

Source description

Source: LeetCode

Link: leetcode-cn.com/problems/ma…

The copyright belongs to Lingkou Network. For commercial reprints, please contact the official authorization, and for non-commercial reprints, please indicate the source.

finally

Thank you for reading, and welcome everyone: like, favorite,coin(focus on)! ! !

8e95dac1fd0b2b1ff51c08757667c47a.gif

Guess you like

Origin juejin.im/post/7078525364435157028