Challenge 6 (Year 2 Sem 1)

Challenge 6 is the sixth challenge in semester 1 of year 2, and the sixth challenge in the total of year 2. It is a challenge for lists, and is considered to be difficult. At the time of writing (June 2021), there are only 15 people who have completed this challenge.

Description
WARNING: The questions in this training may be very hard. Don't go insane completing it.

You may also require out-of-syllabus knowledge or content that is covered in later chapters, but it will be kept to a minimum.

For some of the problems the input size may be very large, and thus it is possible for your code to take a long time to process and produce the result. If you get an error message " ", it is possible that your code is too inefficient to complete larger scenarios within a reasonable amount of time. If that happens, try to rethink your coding strategy to come up with something more efficient.

Good luck!

Unlocked after completion

 * Intern of the Year (Score at least 60% for Challenge 6: Lists)
 * List Master (Score at least 100% for Challenge 6: Lists)

To unlock

 * Complete Challenge 5: String Processing
 * Complete Lab 9.1: Lists
 * Complete Review 9.1: Lists

Question
It has been five years since you founded your highly successful online restaurant Bytes and Nybbles, and you have amassed a small fortune. In need of something to do with all this money, you decide to build an extremely large and luxurious mansion.

Of course you could never live in such a mansion--you would get lost trying to find your way from one end to the other. The only purpose of this mansion would be for people to look at it and admire it. You therefore need to choose a location for your mansion so as many people can see it as possible.

Your mansion will be built on a long road, as illustrated below. One side of the road is filled with houses, each of the same size. You have done your homework, and you have a complete listing of how many people live in each house. Your mansion will cover the width of w houses combined, and will be built on the other side of this road. Your aim is to choose a location so that as many people as possible live opposite your mansion. For instance, consider the road above and suppose that w=4. There are four possible locations for your mansion, each illustrated below. In the first location you would have 3+2+5+1=11 people living opposite. In the second location you would have 2+5+1+4=12 people opposite, and in the third and fourth locations you would have 11 and 9 people respectively. The best you can do is 12 people, and so you build your mansion in the second location (to the envy of your neighbours). Of course this is just an example--in reality the road might be much longer, and your mansion might be much larger. Your task is to write a computer program that can choose the best location for you.

The function find_best_location takes in 2 parameters; the width of your mansion w (1 <= w <= n) and an integer list of houses of size n (1 <= n <= 100000). Each integer within the list represent the number of people living in each house. You are guaranteed that the number of people in each house will be between 1 and 100 inclusive. Your function is to return an integer, giving the largest possible number of people living opposite your mansion.

Solution
The solution is not unlike question 1 of challenge 4. Think about what happens to the sum when you move from one sum to the next: what is added and what is subtracted? What can you use to compare if the next sum is greater than the current sum?

Question
''Note: This is an actual informatics olympiad problem. Good luck!''

A scientist wants to study how the rising sea level changes the landscape, in particular, how it changes the number of islands. He first investigates one-dimensional worlds. A one-dimensional world is represented by a sequence of non-negative integers {h0, h1,. . ., hn−1}, where each integer hi is the altitude at the location i. The following figure depicts an example of such world represented by the sequence {5, 6, 1, 3, 2, 9, 8}. Now, if the sea level is at altitude 2.5, there are 3 islands formed by landmass of the first two columns, the fourth column and the last two columns. Furthermore, if the sea level is at altitude 3.5, there are only 2 islands. When the sea level is at altitude x, landmass with altitude x is considered to be submerged under the sea. Hence, if the sea level is at altitude 3, there are 2 islands. Note that having 3 islands is the maximum among all possible sea levels. Given a one-dimensional world, the scientist wants to find the maximum number of islands among all sea levels.

You are to code the function  which takes in a list of n integers (5 <= n <= 10,000 because Coursemology can't handle extremely large test cases) representing the sequence {h0, h1,. . ., hn−1}. All numbers in the sequence are non-negative and smaller than 230. The function should then return an integer, which is the maximum number of islands. Your code should run within 1.0 seconds. You should not import any packages or modules.

Solution
starting from the highest peak, take a look at the number of peaks and valleys

(peaks are [1, 2, 1], valleys are [2, 1, 2] for example)

if a peak appears +1 to the running total, if a valley appears -1

do not forget to check for corner cases (literally if the numbers are at the corner)

note: your solution must be O(N) not O(N2)

A O(N2) solution is provided below for you to check code against.

def find_max_islands(lst): return max([len(list(filter(lambda a: a != "", "".join([str(x>h) for x in lst]).split("False")))) for h in lst])