0% found this document useful (0 votes)
38 views

Progcomp Training Week 2

This document discusses sliding window problems and provides an example problem. It introduces a brute force solution with quadratic runtime and shows how to optimize it to linear time using a sliding window approach. This involves updating subarray sums by subtracting the outgoing and adding the incoming elements rather than recalculating from scratch. The document recommends two additional sliding window problems and provides a hint for optimizing one with a sliding window instead of a brute force solution.

Uploaded by

Cheng Lu
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
38 views

Progcomp Training Week 2

This document discusses sliding window problems and provides an example problem. It introduces a brute force solution with quadratic runtime and shows how to optimize it to linear time using a sliding window approach. This involves updating subarray sums by subtracting the outgoing and adding the incoming elements rather than recalculating from scratch. The document recommends two additional sliding window problems and provides a hint for optimizing one with a sliding window instead of a brute force solution.

Uploaded by

Cheng Lu
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

Mansions, Aliens and Sliding Window Problems

Viney Kumar
16/08/2019

Mansion-Australian Informatics Competition 2007:


http://orac.amt.edu.au/cgi-bin/train/problem.pl?problemid=338

Concise Problem Summary


Given an array of size N , we must find the maximum sum of all continuous sub-arrays of a fixed length
w. For example, in the sample case, where N = 7, w = 4 and the array elements are [3, 2, 5, 1, 4, 1, 3], the
continuous sub-arrays of length 4 are [3, 2, 5, 1], [2, 5, 1, 4], [5, 1, 4, 1] and [1, 4, 1, 3]. The maximum sum is
given by 2 + 5 + 1 + 4 = 12.

Brute Force Approach


When we first approach a problem that involves choosing the ‘best’ collection from a large number of items
(given some definition of best), it is often helpful to try a brute force approach. This involves listing every
possible collection that is feasible, comparing them all (keeping track of the best collection found so far) and
then selecting the best one. How might we list all possible sub-arrays, so that we can compare them?

Observation 1

There are N − w + 1 possible sub-arrays, and therefore N − w + 1 possible starting locations for the sub-arrays.

Explanation

Let’s find the starting location for the last sub-array. Clearly, this sub-array must consist of the last w
houses in the array of N elements. Therefore, the starting location must be w − 1 houses away from house N
(assuming houses numbered 1 to N ).Therefore, the starting position of the last sub-array (and therefore the
number of possible starting points) is N − w + 1.

Idea (Algorithm)

1. Loop through all N − w + 1 starting points.


2. Create a second nested loop summing all of the w consecutive elements from the starting point in the
sub-array. Keep track of the maximum sum found so far.
3. Is the sum of the most recent sub-array we’ve looped through bigger than the maximum sum we’ve
found so far? If so, then update the maximum sum to be the sum of this particular sub-array.
Note: There are still some small implementation details to sort out, but this is the essence of the program (or
algorithm).

1
How many marks would this solution score?

In order to work this out, we need to be able to do some big-oh analysis of the running time of our program.
Here’s how you might go about it:
1. There are N − w + 1 starting points. For each starting point, we are looping through w elements to
find the sum of the current sub-array.
2. This means that the main computation takes approximately w ∗(N −w +1) basic operations to complete,
and each of these operations (adding, comparisons ect) takes constant (in other words O(1) ) time.
3. Now take a look at the constraints section in the problem statement. The constraints are 1 <= N <=
100, 000 and 1 <= w <= N . Let’s apply the second constraint, which says that 1 <= w <= n. Hence
w could be equal to N/2. In that case, the number of operations (which we previously showed was
approximately w ∗ (N − w + 1) ), is now equal to (N/2) ∗ (N − (N/2) + 1) = (N/2) ∗ (1 + N/2). Hence,
in the worst case, the number of operations is a quadratic function of the input N ! Therefore, our
algorithm runs in O(N 2 ) time.
4. How many points would such an algorithm score? Recall that N can be at most 100,000, and
(105 )2 = 101 0 operations, which is much too big to fit in the time limit of 1 second. Remember, a
computer can only do 50 million= 5 ∗ 107 operations per second. As a result, this algorithm would
time out on the largest testcases. However, there is also some good news. Recall that for 50% of the
available marks, 1 <= N <= 1000. For the case of N = 1000, our algorithm would execute about
10002 = 106 = 1 million operations, which comfortably fits in the allocated time limit of 1 second.
5. Therefore, our approach (if coded with no errors) will score at most 50/100 marks.

How do we score 100% ??


Some more clever thinking is required.

Observation 2

When we sum up the sub-arrays, a lot of the elements are repeated from the previous sum. For example, in
the sample input, when we sum up [3, 2, 5, 1] and [2, 5, 1, 4], we are repeating the computation 2 + 5 + 1 twice!
In fact, if we already have the first sum, we could have simply subtracted 3 and added 4 to get the next sum!

Idea 2

Using this observation we can modify our previous brute force in the following way: We compute the sum of
the first w elements as in the brute force method. However, for the rest of the sub-arrays, we simply subtract
and add the respective elements to get the next sum! This addition and subtraction takes constant time,
which means that we only need one loop! As a result, the running time of a program of this form will be
O(n) or linear time.

Exercise

Implement such an approach on your own and try to score 100% (or close to 100% if you have some minor
bugs) on the test cases.

2
Moral of the Story
The technique where we use clever manipulations to avoid an extra nested loop is known as the “Sliding
Window technique”, and is applicable to many other problems too! Here are some more examples of sliding
window problems for you to practice on:

Homework - More Sliding Window problems to practice on


Warning: Don’t expect to practice exactly the same techniques each time- each problem is very different!
You will need to use your own reasoning combined with this “sliding window” paradigm in order to complete
the problems.
Complete the problems “Alien” and “Wet Chairs” in the practice time and for homework. Next week, we will
be covering the solution to “Wet Chairs” focus on the Set and Hash Table (Dictionary) data structures, their
associated methods, runtime and how they can be used to solve lots of different programming problems.
https://orac.amt.edu.au/cgi-bin/train/problem.pl?problemid=568
https://orac.amt.edu.au/cgi-bin/train/problem.pl?problemid=853

Hint for “Alien”:

A brute force solution would involve going through every possible pair (or slice) from the list, checking
whether the beam could cover the interval, and then checking how many humans were covered in order to
obtain the maximum possible number of humans covered by the beam. This takes quadratic time (i.e O(n2 )
time) and will score 60%.
To score full marks, a sliding window solution is necessary. Is there a way to use the fact that the numbers
are already sorted to our advantage?

You might also like