Maximum sub-array by Haskell

Source: Internet
Author: User
Find-maximum-subarray

Introduction to Algorithm Chinese third edition P42 page exercise 4.1-5
Maximal sub-array algorithm for non-recursive linear time
。。。 But Haskell didn't loop. , my writing is also described in the book ....

However, this problem has a very good function of the advantages of writing this problem is too cool, elegant! completely into the pit

The most gas is the problem on the Codewars, test all over but because a tab does not allow the submit. Rarely do a 5kyu problem, next time in advance to see issue

Algorithm description

The algorithm uses the following idea to design a non-recursive, linear time complexity algorithm for maximal subarray problems. Starting from the left edge of the array, processing from left to right, records the maximum number of sub-arrays that have been processed so far. If the largest subarray of A[1...J] is known, the largest sub-array of the solution is expanded to a[1...j+1] based on the following properties: A[1...j+1] The largest sub-array of either A[1...J], or a sub-array ai...j+1. In the case of the largest subarray of known A[1...J], the shape such as a[i can be found in linear time. J+1] Maximum sub-array

Look at the code.

Maxsequence:: [int], int
maxsequence [] = 0
maxsequence all@ (x:xs) = max (maxsequence xs)  (maximum $ SCA NL (+) 0 All)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.