The largest and most contiguous sub-arrays

Source: Internet
Author: User

Tag: sts Val gets value Max bar for return null

The largest of the public class continuous sub-arrays
{
Time Complexity of O (n)
private static int getsubmaxsum (int[] array)
{
if (array = = NULL | | array.length = = 0)
{
return 0;
}
int sum = 0;
int maxsum = Integer.min_value;
for (int i = 0; i < array.length; i++)
{

When sum<=0, reset sum=0
if (sum <= 0)
{
sum = Array[i];
}
Else
{
Sum + = Array[i];
}
if (Sum > Maxsum)
{
Maxsum = sum;
}
}
return maxsum;
}
Using dynamic planning to implement
public int FindGreatestSumOfSubArray2 (int[] arr,int N) {
int sum = arr[0];
int max = arr[0];
for (int i = 1; i < n; i++) {
sum = Getmax (Sum+arr[i],arr[i]);
if (sum >= max)
max = sum;
}

return Max;
}

public int Getmax (int a,int b) {
Return a > B? A:B;
}
}

The largest and most contiguous sub-arrays

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.