Editorial for LCC '24 Contest 1 J4 - Stealing Candy


Remember to use this editorial only when stuck, and not to copy-paste code from it. Please be respectful to the problem author and editorialist.
Submitting an official solution before solving the problem yourself is a bannable offence.

Author: Iaminnocent4298

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 K 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: O(N)

Space Complexity: O(N)


Comments

There are no comments at the moment.