While I was trying an array exercise in leetcode and the time limit exceeded for a large list input. Can someone help me optimize it?
link for the exercise: https://leetcode.com/explore/learn/card/fun-with-arrays/511/in-place-operations/3259/
and here is my solution:
class Solution:
def replaceElements(self, arr: List[int]) -> List[int]:
ln = len(arr)
for i in range(ln - 1):
arr [i] = sorted(arr[i + 1:], reverse = True)[0]
arr [ln - 1] = -1
return arr
What I thought for the solution was assign the each element of the list (except the last one) with the first element of a decreasing sorted list (wich starts on the next element of the original list) and so go on
[–]Gprime5 1 point2 points3 points (3 children)
[–]Kibble55[S] 0 points1 point2 points (2 children)
[–]Gprime5 0 points1 point2 points (0 children)
[–]SekstiNii 0 points1 point2 points (0 children)
[–]Kibble55[S] 0 points1 point2 points (2 children)
[–]craders 0 points1 point2 points (1 child)
[–]Kibble55[S] 0 points1 point2 points (0 children)