Generating an array of maximum window values

Source: Internet
Author: User

1. Generating a maximum array of window valuesThere is an integer array of arr and a window of size w that slides from the leftmost edge of the array to the far right, and the window slides one position at a time to the right. For example, the array is [4,3,5,4,3,3,6,7] and the window size is 3 o'clock: [4 3 5] 4 3 3 6 7 The maximum value in the window is 54 [3 5 4] 3 3 6 7 The maximum value in the window is 54 3 [5 4 3] 3 6 7 The maximum value in the window is 54 3 5 [4 3 3] 6 7 window maximum value is 44 3 5 4 [3 3 6] 7 window maximum value is 64 3 5 4 3 [3 6 7] The maximum value in the window is 7 if the array length is n and the window size is W, the maximum value of the N-w+1 window is generated altogether. Please implement a function given an array of arr, window size W. Returns an array of length n-w+1 res,res[i] represents the maximum value for each window state. Taking the case as an example, the result should return [5,5,5,4,6,7]. 2. The maximum value minus the minimum number of sub-arrays that are less than or equal to NumGiven the array arr and integer num, returns how many sub-arrays meet the following conditions: Max (arr[i. J])-min (arr[i). J]) <= Nummax (arr[i). J]) represents a subarray of arr[i. J] The maximum value, min (arr[i). J]) represents a subarray of arr[i. The minimum value in J]. If the array length is n, implement a solution with a time complexity of O (n). The reference code is as follows: C + + Code # # requires C + + 11 Support # #
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21st
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
#include<iostream>
#include<cmath>
#include<string.h>
#include<vector>
#include<deque>

using namespacestd;

#defineLENGTH (ARR) (sizeof(ARR)/sizeof(arr[0]))

/** @brief
* Generate a maximum array of window values
* Implement a function, given an array of arr, window size W. Returns an array of length n-w+1 res
* Res[i] Indicates the maximum value for each window state
*
* @param arr[] int
* @param length INT-array Lengths
* @param winlen INT-Window size
* @return vector<int>-array of maximum values
*
*/
vector<int> Slidingwindowmaxarray (intarr[],intlength,intWinlen)
{
deque<int> Maxqueue;//Double-ended queue, store subscript, corresponding elements descending order
vector<int> ret;//Results

for(inti =0; i < length; ++i)
{
/**< Queue Tail-out queue if current element >= Queue trailing element * /
while(!maxqueue.empty () && arr[maxqueue.back ()] <= arr[i])
{
Maxqueue.pop_back ();
}
Maxqueue.push_back (i); //Current element subscript enters queue tail

if(maxqueue.front () = = I-winlen)///Current window range >w, the queue head out of queue
{
Maxqueue.pop_front ();
}

if(i >= Winlen-1) //initial possible window range <w
        {
Ret.push_back (Arr[maxqueue.front ());
}
}

returnret;
}

/** @brief
* Maximum number of sub-arrays minus the minimum value less than or equal to num
* Given an array of arr and integer num, returns how many sub-arrays meet the following conditions:
* MAX (arr[i. J])-min (arr[i). J]) <= num
*
* @param arr[] int
* @param length int
* @param num INT
* @return int
*
*/
intAlllessnumsubarray (intarr[],intlength,intnum)
{
deque<int> Mindeque;
deque<int> Maxdeque;
intret =0;
intLow =0;
intHigh =0;
while(Low < length)
{
while(High < length)
{
while(!mindeque.empty () && arr[mindeque.back ()] >= Arr[high])
{
Mindeque.pop_back ();
}
Mindeque.push_back (high);

while(!maxdeque.empty () && arr[maxdeque.back ()] <= Arr[high])
{
Maxdeque.pop_back ();
}
Maxdeque.push_back (high);

if(Arr[maxdeque.front ()]-Arr[mindeque.front ()] > num) Break;

++high;
}

if(mindeque.front () = = Low) Mindeque.pop_front ();
if(maxdeque.front () = = Low) Maxdeque.pop_front ();

RET + = high-low;
++low;
}

returnret;
}

intMain ()
{
intarr[] = {4, 3, 5, 4, 3, 3, 6, 7};
for(Autoval:slidingwindowmaxarray arr., LENGTH (arr),3))
{
cout << Val <<" ";
}
cout << Endl; Output Result "5 5 5 4 6 7"

intarr1[] = {7, 9, 6, 1, 0,
7, 5, 4, 4, 4,
2, 0, 7, 1, 7,
2, 5, 3, 1, 9,
0, 8, 8, 9, 4,
2, 3, 6, 9, 8
};
cout << Alllessnumsubarray (arr1, LENGTH (arr1),5) << Endl; //79

return 0;
}

Generating an array of maximum window values

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.