Editorial for DMOPC '18 Contest 2 P4 - Damage Output


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: r3mark

For the first subtask, brute-force all subarrays and their sums.

Time Complexity: \mathcal{O}(N^3)

For the second subtask, check all subarrays and use a prefix sum to calculate their sums.

Time Complexity: \mathcal{O}(N^2)

For the third subtask, we do a two-pointer walk. Keep a left and right pointer, initially both set to the starting index. If the subarray currently described by the two pointers has a sum less than M, move the right pointer. Otherwise, move the left pointer. Since all the elements are non-negative, this will find the smallest subarray with sum at least M.

Time Complexity: \mathcal{O}(N)


Comments

There are no comments at the moment.