Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

How to realize the maximum suborder sum in big data

Shulou Source: shulou.com Published: 2022-06-01 16:05:04 10月17日 Update

Many novices are not very clear about how to achieve the maximum subsequence sum in big data. In order to help you solve this problem, the following editor will explain it in detail. People with this need can come and learn. I hope you can get something.

one

Topic description

Given an integer array nums, find a continuous subarray with the largest sum (the subarray contains at least one element) and return its maximum sum. For example, enter [- 2, 1, 1, 4, 5, 3, 3] to return 1.

two

Answer to the question

Idea: dynamic planning

The first step is to find the intermediate state: where the intermediate state st [I] represents the maximum sum of the subarray at the end of the I element.

The second step is to determine the state transition: nums [I] adds a positive sum to make it larger, otherwise starting a new stove is more likely to get a larger sum. So when st [I-1] is positive, st [I] = st [I-1] + nums [I], otherwise st [I] = nums [I].

Class Solution: def maxSubArray (self, nums: List [int])-> int: st = nums [0] for i in range (1jol len (nums)): st.append (max (nums [I], max_ list [I-1] + nums [I]) return max (st)

‍

Two ideas are given in the official video of solving the problem, one is the greedy algorithm: if the sum before the current pointing element is less than 0, the sequence before the current element is lost; the other is dynamic programming: if the previous element is greater than 0, it is added to the current element. Emmm.... I feel that the two ideas are very similar. I don't particularly understand the essential differences. If you are interested, we will discuss them together.

Is it helpful for you to read the above content? If you want to know more about the relevant knowledge or read more related articles, please follow the industry information channel, thank you for your support.

Tags: Element maximum array train of thought state dynamic positive number help planning data clarity starting again two interest content official for this sequence feeling pointing Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno MySQL MariaDB Apple Redmi Shulou Information