How can I solve LeetCode problem 1052: Grumpy Bookstore Owner efficiently?