2202. Maximize the Topmost Element After K Moves Medium
1/**
2 * [2202] Maximize the Topmost Element After K Moves
3 *
4 * 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.
5 * In one move, you can perform either of the following:
6 *
7 * If the pile is not empty, remove the topmost element of the pile.
8 * If there are one or more removed elements, add any one of them back onto the pile. This element becomes the new topmost element.
9 *
10 * You are also given an integer k, which denotes the total number of moves to be made.
11 * 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.
12 *
13 * Example 1:
14 *
15 * Input: nums = [5,2,2,4,0,6], k = 4
16 * Output: 5
17 * Explanation:
18 * One of the ways we can end with 5 at the top of the pile after 4 moves is as follows:
19 * - Step 1: Remove the topmost element = 5. The pile becomes [2,2,4,0,6].
20 * - Step 2: Remove the topmost element = 2. The pile becomes [2,4,0,6].
21 * - Step 3: Remove the topmost element = 2. The pile becomes [4,0,6].
22 * - Step 4: Add 5 back onto the pile. The pile becomes [5,4,0,6].
23 * 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.
24 *
25 * Example 2:
26 *
27 * Input: nums = [2], k = 1
28 * Output: -1
29 * Explanation:
30 * In the first move, our only option is to pop the topmost element of the pile.
31 * Since it is not possible to obtain a non-empty pile after one move, we return -1.
32 *
33 *
34 * Constraints:
35 *
36 * 1 <= nums.length <= 10^5
37 * 0 <= nums[i], k <= 10^9
38 *
39 */
40pub struct Solution {}
41
42// problem: https://leetcode.com/problems/maximize-the-topmost-element-after-k-moves/
43// discuss: https://leetcode.com/problems/maximize-the-topmost-element-after-k-moves/discuss/?currentPage=1&orderBy=most_votes&query=
44
45// submission codes start here
46
47impl Solution {
48 pub fn maximum_top(nums: Vec<i32>, k: i32) -> i32 {
49 0
50 }
51}
52
53// submission codes end
54
55#[cfg(test)]
56mod tests {
57 use super::*;
58
59 #[test]
60 fn test_2202() {
61 }
62}
63
Back
© 2025 bowen.ge All Rights Reserved.