#3828

Final Element After Subarray Deletions

Medium
ArrayMathBrainteaserGame TheoryGame TheoryDynamic Programming
LeetCode ↗

Approaches

Brute ForceOptimal
Complexity Comparison
Brute ForceOptimal Solution
Time
O(n²)
O(1)
Space
O(1)
O(1)
💡

Intuition

Time O(1)Space O(1)

Alice can only protect the endpoints of the array, as Bob can always remove any middle element. Hence, the optimal strategy revolves around preserving the maximum of the endpoints.

⚙️

Algorithm

2 steps
  1. 1Step 1: Identify the maximum of the first and last elements of the array.
  2. 2Step 2: Return the maximum value since Alice will always choose to protect it.
solution.py2 lines
1def finalElement(nums):
2    return max(nums[0], nums[-1])

Complexity note: The time complexity is constant since we only compare two values, and space complexity is also constant as no additional space is used.

  • 1Only endpoints can be protected.
  • 2Bob can always remove middle elements.

Solutions and explanations are original Tejav content. Problem titles © LeetCode — use the LeetCode button above for the full problem statement.