The maximum difference between the two elements in the array (the following elements minus the preceding elements) O (n) time complexity O (1) space complexity

Source: Internet
Author: User
  1. Since the summer interview was despised, I often think about this problem when I come back. It should be almost two months now. In this afternoon, I took out the draft and finally found my idea and completed it. Just like a wish, it's over.
  2. The problem is as follows: the original question is not described in detail. The problem after simplification is to find two elements in the array, and calculate the difference between the elements following the calculation and the elements above. Find the maximum value of all the differences. (You can think that you are making a profit by buying and selling stocks)
  3. At that time, the thought method was to find the elements of Max and min if it happened that Max was behind min, the key is what to do if this problem does not occur. I have provided a recursive method. When I come back, I think it will not work. recursion is unreliable and it is difficult for some test cases to have high efficiency.
  4. The subconscious tells me that finding the maximum sum of continuous elements in the array has inspired me a lot. This problem has been solved in this blog, and I did it twice. That's great. The solution to that problem is to consider the problem of all elements as a problem starting with an array of two elements. When the problem is solved, what is the impact of adding a new element? What values do you want to change? Each time an element is added, the possible changes are taken into account every time, so that the problem can be solved when the entire array can be traversed. Achieve the fastest speed O (N)
  5. With the above ideas, this problem is simpler. Its implementation is as follows:
Int max_difference (const vector <int> & ARR) {If (ARR. size ()> = 2) {int min = min (ARR [0], arr [1]); int max_difference = arr [1]-Arr [0]; // The first obtained difference, which is used as the maximum difference for (vector <int>: size_type I = 2; I <arr. size (); I ++) {If (ARR [I]-min> max_difference) {max_difference = arr [I]-min;} If (ARR [I] <min) {min = arr [I]; // The Key to influencing the new difference lies in who has found the current minimum element, max_difference} return max_difference;} else {cout <"size of ARR is too small"; return 0 ;}} void give_result (int A [], int N) {vector <int> Arr (A, A + n); print (ARR. begin (), arr. end (); print (max_difference (ARR);} int main (void) {int A0 [] = {7, 9, 10}; give_result (A0, 3 ); int A1 [] = {, 7}; give_result (A1, 3); int A [] = {,}; give_result (A, 4 ); int A2 [] = {,}; give_result (A2, 6); Return 0 ;}

The output result is a group of two rows. The first action is to consider all elements of the array, and the second action is the result of max_difference.

I have made a lot of pen tests, and I feel that I have gained a lot. But after all, I have not done enough. This is the first solution to solve it with the skills I have mastered. The algorithm is powerful !!

Careful readers will also find that the code in this article uses the reverse iterator of vector. Previously, traversal similar to algorithm requirements always felt awkward when using the forward iterator, And suddenly remembered the reverse iterator. The discovery is really suitable for this situation.

 

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.