Editorial for GFSSOC '15 Fall J5 - Nightmare-a-thon
Submitting an official solution before solving the problem yourself is a bannable offence.
This problem was quite challenging for the junior level (thank Timothy Li), but the solution is super cool! Because of the high bounds on
Episodes: 1 3 2 4 3 5 2
Maxleft: 1 3 3 4 4 5 5
Maxright: 5 5 5 5 5 5 2
Now, to find the highest rating not within episodes maxleft[a-1]
and maxright[b+1]
.
The second part of the query can be solved in conjunction with the first. Create 2 more occurrence/frequency arrays. Whenever you update your maximum array to a higher rating, the occurrences of high should be set to 1 (since this new high is the only one you've found!). Whenever you find an episode that has equal rating to your previous high, the occurrence at that index should equal the occurrence at the previous index + 1. Otherwise, the occurrence at the current index should equal the occurrence at the previous index. An example iterating from the left side:
Episodes: 1 3 2 4 4 5 2
Maxleft: 1 3 3 4 4 5 5
Freqleft: 1 1 1 1 2 1 1
Now, to answer the second part of the query, we need to consider a few cases. If the left side max is greater than the right side max, we need to take the occurrences of the left side max (aka Freqleft[a-1]
). Similarly if right side max > left side max, we take Freqright[b+1]
. However, if left side max == right side max, we need to find the combined occurrences of this number in both the left side and right side, thus we take Freqleft[a-1] + Freqright[b+1]
.
Comments