Tap here to switch tabs
Problem
Submissions

Problem: Maximize the Topmost Element After K Moves

med
30 min
Try to solve the Maximize the Topmost Element After K Moves problem.

Statement

You are given an integer array nums indexed starting at 00, which represents a pile of elements where the element at index 00 is at the top of the pile.

In a single move, you can perform either of the following operations:

  • If the pile is not empty, remove the topmost element from the pile.

  • If at least 11 element has been removed, select any previously removed element and place it back onto the top of the pile.

You are also given an integer k, representing the total number of moves you must perform.

Return the maximum possible value of the topmost element of the pile after executing exactly k moves. If it is impossible to have any elements remaining in the pile after k moves, return −1-1.

Constraints:

  • 1≤1 \leq nums.length ≤105\leq 10^5

  • 0≤0 \leq nums[i], k ≤109\leq 10^9

⋮
Tap here to switch tabs
Problem
Submissions

Problem: Maximize the Topmost Element After K Moves

med
30 min
Try to solve the Maximize the Topmost Element After K Moves problem.

Statement

You are given an integer array nums indexed starting at 00, which represents a pile of elements where the element at index 00 is at the top of the pile.

In a single move, you can perform either of the following operations:

  • If the pile is not empty, remove the topmost element from the pile.

  • If at least 11 element has been removed, select any previously removed element and place it back onto the top of the pile.

You are also given an integer k, representing the total number of moves you must perform.

Return the maximum possible value of the topmost element of the pile after executing exactly k moves. If it is impossible to have any elements remaining in the pile after k moves, return −1-1.

Constraints:

  • 1≤1 \leq nums.length ≤105\leq 10^5

  • 0≤0 \leq nums[i], k ≤109\leq 10^9