LeetCode-in-Java

3828. Final Element After Subarray Deletions

Medium

You are given an integer array nums.

Two players, Alice and Bob, play a game in turns, with Alice playing first.

Alice aims to maximize the final element, while Bob aims to minimize it. Assuming both play optimally, return the value of the final remaining element.

Example 1:

Input: nums = [1,5,2]

Output: 2

Explanation:

One valid optimal strategy:

Example 2:

Input: nums = [3,7]

Output: 7

Explanation:

Alice removes [3], leaving the array [7]. Since Bob cannot play a turn now, the answer is 7.

Constraints:

Solution

public class Solution {
    public int finalElement(int[] nums) {
        return Math.max(nums[0], nums[nums.length - 1]);
    }
}