All Posts
Leetcode 2202: Maximize the Topmost Element After K Moves
You are given an integer array nums
representing a pile, where nums[0]
is the topmost element. You can perform the following operations in exactly k
moves:
- Remove the topmost element of the pile (if it’s not empty).
- If any elements have been removed, you can add any one of them back onto the pile as the new topmost element.
Your goal is to determine the maximum value that can be at the top of the pile after exactly k
moves. If it is not possible to have any elements left in the pile after k
moves, return -1
.