본문 바로가기

IT/코딩테스트

[Leetcode] 1299. Replace Elements with Greatest Element on Right Side

728x90

https://leetcode.com/problems/replace-elements-with-greatest-element-on-right-side/

class Solution:
    def replaceElements(self, arr: List[int]) -> List[int]:

        result = []
        before = max(arr)

        for i in range(len(arr) - 1):
            if arr[i] == before:
                before = max(arr[i+1:])
            result.append(before)
        result.append(-1)

        return result
728x90