American Regiment 2016 online programming problem maximum difference (greedy)

Source: Internet
Author: User

Title Description

There is an n-long array A, which satisfies the maximum value of 0≤a≤b<n's a[b]-a[a].

Given the array A and its size n, return the maximum difference.

Test examples:
[10,5],2
Returns: 0

Idea: The maximum interpolation value dis is initialized to 0, with a pointer traversing from the beginning, the pointer is found before the minimum value of the element pointed to is the min1, if the pointer points to the element A[i]-min1 > Dis, the dis is updated to a[i]-min1.
1 classLongestdistance {2  Public:3     intGetdis (vector<int> A,intN) {4         //Write code here5         intdis =0;//The maximum difference is initialized to 06         intMin1 = a[0];7          for(inti =1; I < n; i++){8Min1 =std::min (Min1, a[i]);9dis = Std::max (dis, a[i]-min1);Ten         } One         returndis; A     } -};

American Regiment 2016 online programming problem maximum difference (greedy)

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.