Maximum value minus the minimum number of sub-arrays that are less than or equal to num

Source: Internet
Author: User

"description":

This article is a C + + recurrence of the topic " maximum value minus minimum value less than or equal to num number of sub-arrays " in the first chapter of the Programmer's Interview code guide by Zhoecheng.

This article only contains the problem description, the implementation of C + + code and simple ideas, not including the explanation, the specific problem analysis please refer to the original book.

Thank Zhoecheng teacher for his support.

"title":

Given the array arr and integer num, the total number of word groups returned satisfies the following conditions:

Max (ARR[I...J])-min (ARR[I...J]) <= num

Max (ARR[I...J]) represents the maximum value in the word group ARR[I...J], and Min (ARR[I...J]) represents the minimum value in the Subarray ARR[I...J].

"ideas":

1, if ARR[I...J] satisfies the condition, then its sub-array will also satisfy the condition;

2, if ARR[I...J] dissatisfaction group conditions, then contains ARR[I...J] array are not satisfied with the conditions;

"Compile environment":

CentOS6.7 (x86_64)

GCC 4.4.7

"Implementation":

Implementing and testing the code:

1 /*2 * File name: GetSatisfiedSubArrNum.cpp3  *4 * Summary: Find the number of sub-arrays that meet the criteria5  */6 7#include <iostream>8#include <deque>9 Ten using namespacestd; One  A intGetnum (intArr[],intLenintnum) - { -     if(NULL = = Arr | |0>=len) the         return 0; -deque<int> qmin;//the first data of qmin is always the location of the smallest data in the current array -deque<int> Qmax;//the first data of Qmax is always the position of the largest data in the current array -      +     intI0), J (0), Res (0); -      while(I <len) +     { A          while(I <len) at         { -              while(!qmin.empty () && Arr[qmin.front ()] >=Arr[j]) - Qmin.pop_back (); - Qmin.push_back (j); -  -              while(!qmax.empty () && Arr[qmax.front () <=Arr[j]]) in Qmax.pop_back (); - Qmax.push_back (j); to  +             if(Arr[qmax.front ()]-Arr[qmin.front ()] >num) -                  Break;// theJ + +; *         } $Res + = J-i;//record the number of word groups that meet the requirements in the current sequencePanax Notoginseng          -         if(Qmin.front () = =i) the Qmin.pop_front (); +         if(Qmax.front () = =i) A Qmax.pop_front (); thei++;//Move Down +     } -     returnRes; $ } $  - intMain () - { the     intA[] = {3,2,5,1,4,7,8,6}; -cout <<"The number of sub arrays to meet the requirements is:"<< Getnum (A,8,4) <<Endl;Wuyi  the     return 0; -}
View Code

Note:

Reprint please indicate the source;

Reprint please indicate source idea from Zhoecheng Teacher's "Programmer Code Interview Guide"

Maximum value minus the minimum number of sub-arrays that are less than or equal to num

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.