Skip to content
LC-1299 Easy LeetCode

1299. Replace Elements with Greatest Element on Right Side

Read the full problem statement on LeetCode.
Difficulty: easy Acceptance: 71% Topics: Array
View full problem on LeetCode

Reading material

Reference solution (spoiler · python)
# Time:  O(n)
# Space: O(1)

class Solution(object):
    def replaceElements(self, arr):
        """
        :type arr: List[int]
        :rtype: List[int]
        """
        curr_max = -1
        for i in reversed(xrange(len(arr))):
            arr[i], curr_max = curr_max, max(curr_max, arr[i])
        return arr

Solution from kamyu104/LeetCode-Solutions · MIT