Skip to content
LC-2202 Medium LeetCode

2202. Maximize the Topmost Element After K Moves

Read the full problem statement on LeetCode.
Difficulty: medium Acceptance: 23% Topics: Array, Greedy
View full problem on LeetCode
Reference solution (spoiler · python)
# Time:  O(min(n, k))
# Space: O(1)

# constructive algorithms
class Solution(object):
    def maximumTop(self, nums, k):
        """
        :type nums: List[int]
        :type k: int
        :rtype: int
        """
        if len(nums) == 1 == k%2:
            return -1
        if k <= 1:
            return nums[k]
        return max(nums[i] for i in xrange(min(k+1, len(nums))) if i != k-1)

Solution from kamyu104/LeetCode-Solutions · MIT

Similar questions