Advertisement
smj007

Untitled

Mar 9th, 2025
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.66 KB | None | 0 0
  1. class Solution:
  2.     def replaceElements(self, arr: List[int]) -> List[int]:
  3.        
  4.         # max to the right
  5.         max_seen_so_far = -1
  6.  
  7.         for i in range(len(arr)-1, -1, -1):
  8.             # first store thay you will use later before getting overwritten
  9.             temp = arr[i]
  10.  
  11.             # this should be the first step that you should right
  12.             # because of you have already obtained the answer from
  13.             # the previous steps and that is what it is
  14.             arr[i] = max_seen_so_far
  15.  
  16.             # you are literally doing this for the next step
  17.             max_seen_so_far = max(max_seen_so_far, temp)
  18.  
  19.         return arr
  20.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement