Off-by-one on range boundaries
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Move from brute-force thinking to an efficient approach using array strategy.
You are given a 0-indexed integer array nums representing the contents of a pile, where nums[0] is the topmost element of the pile.
In one move, you can perform either of the following:
You are also given an integer k, which denotes the total number of moves to be made.
Return the maximum value of the topmost element of the pile possible after exactly k moves. In case it is not possible to obtain a non-empty pile after k moves, return -1.
Example 1:
Input: nums = [5,2,2,4,0,6], k = 4 Output: 5 Explanation: One of the ways we can end with 5 at the top of the pile after 4 moves is as follows: - Step 1: Remove the topmost element = 5. The pile becomes [2,2,4,0,6]. - Step 2: Remove the topmost element = 2. The pile becomes [2,4,0,6]. - Step 3: Remove the topmost element = 2. The pile becomes [4,0,6]. - Step 4: Add 5 back onto the pile. The pile becomes [5,4,0,6]. Note that this is not the only way to end with 5 at the top of the pile. It can be shown that 5 is the largest answer possible after 4 moves.
Example 2:
Input: nums = [2], k = 1 Output: -1 Explanation: In the first move, our only option is to pop the topmost element of the pile. Since it is not possible to obtain a non-empty pile after one move, we return -1.
Constraints:
1 <= nums.length <= 1050 <= nums[i], k <= 109Problem summary: You are given a 0-indexed integer array nums representing the contents of a pile, where nums[0] is the topmost element of the pile. In one move, you can perform either of the following: If the pile is not empty, remove the topmost element of the pile. If there are one or more removed elements, add any one of them back onto the pile. This element becomes the new topmost element. You are also given an integer k, which denotes the total number of moves to be made. Return the maximum value of the topmost element of the pile possible after exactly k moves. In case it is not possible to obtain a non-empty pile after k moves, return -1.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Array · Greedy
[5,2,2,4,0,6] 4
[2] 1
gas-station)Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #2202: Maximize the Topmost Element After K Moves
class Solution {
public int maximumTop(int[] nums, int k) {
if (k == 0) {
return nums[0];
}
int n = nums.length;
if (n == 1) {
if (k % 2 == 1) {
return -1;
}
return nums[0];
}
int ans = -1;
for (int i = 0; i < Math.min(k - 1, n); ++i) {
ans = Math.max(ans, nums[i]);
}
if (k < n) {
ans = Math.max(ans, nums[k]);
}
return ans;
}
}
// Accepted solution for LeetCode #2202: Maximize the Topmost Element After K Moves
func maximumTop(nums []int, k int) int {
if k == 0 {
return nums[0]
}
n := len(nums)
if n == 1 {
if k%2 == 1 {
return -1
}
return nums[0]
}
ans := -1
for i := 0; i < min(k-1, n); i++ {
ans = max(ans, nums[i])
}
if k < n {
ans = max(ans, nums[k])
}
return ans
}
# Accepted solution for LeetCode #2202: Maximize the Topmost Element After K Moves
class Solution:
def maximumTop(self, nums: List[int], k: int) -> int:
if k == 0:
return nums[0]
n = len(nums)
if n == 1:
if k % 2:
return -1
return nums[0]
ans = max(nums[: k - 1], default=-1)
if k < n:
ans = max(ans, nums[k])
return ans
// Accepted solution for LeetCode #2202: Maximize the Topmost Element After K Moves
/**
* [2202] Maximize the Topmost Element After K Moves
*
* You are given a 0-indexed integer array nums representing the contents of a pile, where nums[0] is the topmost element of the pile.
* In one move, you can perform either of the following:
*
* If the pile is not empty, remove the topmost element of the pile.
* If there are one or more removed elements, add any one of them back onto the pile. This element becomes the new topmost element.
*
* You are also given an integer k, which denotes the total number of moves to be made.
* Return the maximum value of the topmost element of the pile possible after exactly k moves. In case it is not possible to obtain a non-empty pile after k moves, return -1.
*
* Example 1:
*
* Input: nums = [5,2,2,4,0,6], k = 4
* Output: 5
* Explanation:
* One of the ways we can end with 5 at the top of the pile after 4 moves is as follows:
* - Step 1: Remove the topmost element = 5. The pile becomes [2,2,4,0,6].
* - Step 2: Remove the topmost element = 2. The pile becomes [2,4,0,6].
* - Step 3: Remove the topmost element = 2. The pile becomes [4,0,6].
* - Step 4: Add 5 back onto the pile. The pile becomes [5,4,0,6].
* Note that this is not the only way to end with 5 at the top of the pile. It can be shown that 5 is the largest answer possible after 4 moves.
*
* Example 2:
*
* Input: nums = [2], k = 1
* Output: -1
* Explanation:
* In the first move, our only option is to pop the topmost element of the pile.
* Since it is not possible to obtain a non-empty pile after one move, we return -1.
*
*
* Constraints:
*
* 1 <= nums.length <= 10^5
* 0 <= nums[i], k <= 10^9
*
*/
pub struct Solution {}
// problem: https://leetcode.com/problems/maximize-the-topmost-element-after-k-moves/
// discuss: https://leetcode.com/problems/maximize-the-topmost-element-after-k-moves/discuss/?currentPage=1&orderBy=most_votes&query=
// submission codes start here
impl Solution {
pub fn maximum_top(nums: Vec<i32>, k: i32) -> i32 {
0
}
}
// submission codes end
#[cfg(test)]
mod tests {
use super::*;
#[test]
#[ignore]
fn test_2202_example_1() {
let nums = vec![5, 2, 2, 4, 0, 6];
let k = 4;
let result = 5;
assert_eq!(Solution::maximum_top(nums, k), result);
}
#[test]
#[ignore]
fn test_2202_example_2() {
let nums = vec![2];
let k = 1;
let result = -1;
assert_eq!(Solution::maximum_top(nums, k), result);
}
}
// Accepted solution for LeetCode #2202: Maximize the Topmost Element After K Moves
// Auto-generated TypeScript example from java.
function exampleSolution(): void {
}
// Reference (java):
// // Accepted solution for LeetCode #2202: Maximize the Topmost Element After K Moves
// class Solution {
// public int maximumTop(int[] nums, int k) {
// if (k == 0) {
// return nums[0];
// }
// int n = nums.length;
// if (n == 1) {
// if (k % 2 == 1) {
// return -1;
// }
// return nums[0];
// }
// int ans = -1;
// for (int i = 0; i < Math.min(k - 1, n); ++i) {
// ans = Math.max(ans, nums[i]);
// }
// if (k < n) {
// ans = Math.max(ans, nums[k]);
// }
// return ans;
// }
// }
Use this to step through a reusable interview workflow for this problem.
Try every possible combination of choices. With n items each having two states (include/exclude), the search space is 2ⁿ. Evaluating each combination takes O(n), giving O(n × 2ⁿ). The recursion stack or subset storage uses O(n) space.
Greedy algorithms typically sort the input (O(n log n)) then make a single pass (O(n)). The sort dominates. If the input is already sorted or the greedy choice can be computed without sorting, time drops to O(n). Proving greedy correctness (exchange argument) is harder than the implementation.
Review these before coding to avoid predictable interview regressions.
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Wrong move: Locally optimal choices may fail globally.
Usually fails on: Counterexamples appear on crafted input orderings.
Fix: Verify with exchange argument or monotonic objective before committing.