Tags: greedy
Iterate from the last element and look at the current element and the one ahead of it, let's call them a and b. If a <= b, keep them the same. Otherwise if a > b, if a is too big to subtract 1 from to make it less than or equal to b, the answer is no. Otherwise, subtract 1 from a and move on to the next element, which is, again, one element down the list because we start from the end.
I didn't try it but I think another solution is to start from the beginning and subtract 1 from an element when the element behind it is less than it, so it minimizes the chance that there is a small element ahead of a big element.
My code: http://stfly.io/ArP9EpG
No comments:
Post a Comment