0

I have googled and found a solution for this,

void maxSlidingWindow(int A[], int n, int w, int B[]) {
  deque<int> Q;
  for (int i = 0; i < w; i++) {
    while (!Q.empty() && A[i] >= A[Q.back()])
      Q.pop_back();
    Q.push_back(i);
  }
  for (int i = w; i < n; i++) {
    B[i-w] = A[Q.front()];
    while (!Q.empty() && A[i] >= A[Q.back()])
      Q.pop_back();
    while (!Q.empty() && Q.front() <= i-w)
      Q.pop_front();
    Q.push_back(i);
  }
  B[n-w] = A[Q.front()];
}

But I couldn't get this solution. For example, if you take the example {10,5,3,2} After the first for loop, the Dequeue will be like this, 3(rear)->5->10(head). When this comes to the second for loop 10 is saved in B[0]. , and the list will be like this, 2(rear)-> 3 -> 5 -> 10(head).

But, here '5' should be at front. Could someone please explain this code with an example.

vignesh
  • 123
  • 11
  • I haven't tried to understand the problem with the code, but I can tell you that this problem can be solved easily in linear time *and constant space*: Just add up the first k numbers, and then scan through the remaining array positions A[i] for k+1 <= i <= n, on each loop cycle adding A[i] to and subtracting A[i-k] from the total, and checking whether this is the best sum so far. – j_random_hacker Jun 15 '15 at 15:46
  • The solution is to ind out the maximum of subarrays, not the maximum sum of subarrays. – vignesh Jun 15 '15 at 15:58
  • What is "the maximum of subarrays"? A subarray is a bunch of numbers, so I don't understand how you say that one subarray is larger than another unless you calculate something (like a sum) from each of them. – j_random_hacker Jun 15 '15 at 16:02
  • The given array is {10,5,3,2} and the sub array size is 3 (K=3). Then the sub-arrays would be {10,5,3} and {5,3,2} and if K = 2, then, {10,5}{5,3}{3,2} – vignesh Jun 15 '15 at 16:14
  • Right, but which of those 3 subarrays is "maximum"? (I mean: what is the criterion, if not the sum of the elements?) – j_random_hacker Jun 15 '15 at 16:25
  • The criteron is to find the maximum, the result array for W = 3 is [10,5] and W = 2 result array would be [10,5,3] – vignesh Jun 16 '15 at 16:41
  • It's *still* not clear how to decide which of two arrays is "the maximum". With two individual numbers there's an obvious rule, but with 2 *arrays* of numbers there are many possible rules, so you need to say which, but you haven't yet, and your example doesn't distinguish the obvious possibilities (max, sum, lexicographic order) so let's try other examples. Which of {1, 5} and {5, 1} is the "maximum", or are they both? Which of {30, 20} and {30, 21} is the "maximum", or are they both? Which of {100, 50} and {90, 80} is the "maximum"? Which of {10, 3} and {1, 11} is the "maximum"? – j_random_hacker Jun 16 '15 at 21:17

1 Answers1

0

It seems you missed this condition: Q.front() <= i-w which removes too old front items.

You can see also my comments in this answer

Community
  • 1
  • 1
MBo
  • 77,366
  • 5
  • 53
  • 86
  • while (!Q.empty() && Q.front() <= i-w), is this you are talking about ?? – vignesh Jun 15 '15 at 15:58
  • Yes. This code removes element that leaves the moving window – MBo Jun 15 '15 at 15:59
  • I couldn't understand this, Could you please explain ? – vignesh Jun 15 '15 at 16:11
  • I particularly, din't get this "Q.front() <= i-w" , Q.front() will always be less than i-w. Since i-w will be growing 0,1,2,3. Q.front() will stays at '0'. so the first item will be poped always ??? – vignesh Jun 15 '15 at 16:44
  • i-w is index of the last element still in window (the oldest).Yes, when i-w=1, 0th element will be poped – MBo Jun 15 '15 at 17:19
  • For eg : {12, 1, 78, 90, 57, 89, 56} and w = 3 ; after processing the first w elements, The dequeue would be : 78., and when it enters the second for loop, dequeue would be : 90., and 90 will be poped after it gets printed for the first time, cos of the condition Q.front() <= i-w. Actually 90 has to be printed thrice... – vignesh Jun 15 '15 at 18:48
  • When the second for loop starts: 1. output 78 2. pop 78 due to the first while condition 78<90 3. push 90 – MBo Jun 16 '15 at 07:04
  • In the second iteration of the second for loop, 90 will be printed once and poped out right because of the condition Q.front() <= i-w right ??? – vignesh Jun 16 '15 at 12:34
  • for i=4 i-w=1<4 (index of 90), so 90 will be stored for 3 cycles. I think you could debug the code step by step with computer or with pen and paper. – MBo Jun 16 '15 at 15:17
  • I couldn't get, how Q.front() becomes 4 in the second iteration. – vignesh Jun 16 '15 at 16:34
  • My mistake - index of 90 is 3 – MBo Jun 16 '15 at 16:38
  • deque will be [12,1] after processing 2 elements, when 3rd element comes , dequeue will be emptied [] and [78] is pushed (index of 78 is still '0' here,since it is the only element ). Now the dequeue enters the second for loop, a[Q.front()] printed out and 78 is poped out because of the condition Q.front() <= i-w (i.e. 0<= 0 ) and dequeue is emptied again [], 90 is added at the index 0, [90] but will be poped out in the second iteration of second for loop right ? because Q.front() <= i-w (i.e. 0<= 1) Q.front() is 0 still ? Is my assumption right ? – vignesh Jun 16 '15 at 18:10
  • No, index of 78 is 2 - this is index in array! – MBo Jun 16 '15 at 18:24
  • May I know how ? when 78 is pushing in, 12 and 1 will be poped out of dequeue. So, 78 will be at index 0 (front) right ? – vignesh Jun 16 '15 at 18:41
  • There are NO INDEXES in Deque. They are from array. Probably this is a reason of your misunderstanding – MBo Jun 17 '15 at 02:49
  • Yeah that was my misunderstanding., thanks a alot :) – vignesh Jun 18 '15 at 17:57