관리 메뉴

솜씨좋은장씨

[leetCode] 1299. Replace Elements with Greatest Element on Right Side (Python) 본문

Programming/코딩 1일 1문제

[leetCode] 1299. Replace Elements with Greatest Element on Right Side (Python)

솜씨좋은장씨 2020. 11. 7. 00:19
728x90
반응형

Given an array arr, replace every element in that array with the greatest element among the elements to its right, and replace the last element with -1.

After doing so, return the array.

 

Example 1:

Input: arr = [17,18,5,4,6,1]
Output: [18,6,6,6,1,-1]

 

Constraints:

  • 1 <= arr.length <= 10^4
  • 1 <= arr[i] <= 10^5

Solution

class Solution:
    def replaceElements(self, arr: List[int]) -> List[int]:
        answer = [0] * len(arr)
        answer[-1] = -1
        
        for i in range(len(arr)-2, -1, -1):
            answer[i] = max([answer[i+1], arr[i+1]])
            
        return answer

 

SOMJANG/CODINGTEST_PRACTICE

1일 1문제 since 2020.02.07. Contribute to SOMJANG/CODINGTEST_PRACTICE development by creating an account on GitHub.

github.com

Comments