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 <=
<= nums[i] <=
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
- Initialize the maximum value max, because from scratch, the default maximum value is the value corresponding to the array subscript 0.
- Loop traversal, the number of times to traverse the length of the array
- Carry out the logical judgment in the topic analysis in the for loop:
- 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.
- Judging in the for loop, if the added current node value is greater than the current maximum value max, update max
- Continue to loop judgment until the end
- 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
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)! ! !