Editorial for LCC '24 Contest 1 J4 - Stealing Candy
Submitting an official solution before solving the problem yourself is a bannable offence.
Author:
Notice that checking each possible substring will most definitely TLE. This question requires knowledge of the "Sliding Window" technique.
Begin with a "window" of length at the beginning of the string. Count the amount of Sadness Ranchers in the window.
As you slide the "window" across the length of the string, keep track of the candies that are now in the window, and which ones leave the range of the window. This can be done by checking the indices of the left and right sides of the window and evaluating if the character at that index of the string is a Sadness Rancher.
Finally, store the maximum amount of candies in the "window" at any given moment, and as you slide the "window", update that variable to store the maximum candies in the "window" up until that index.
Time Complexity:
Space Complexity:
Comments