Maximum Element After Decreasing and Rearranging LeetCode solution in Python

1
class Solution:
2
def maximumElementAfterDecrementingAndRearranging(self, arr: List[int]) -> int:
3
counter = collections.Counter(arr)
4
available = sum(n > len(arr) for n in arr)
5
i = ans = len(arr)
6
while i > 0:
7
# This number is not in arr
8
if not counter[i]:
9
# Use another number to fill in its place. If we cannot, we have to decrease our max
10
if available:
11
available -= 1
12
else:
13
ans -= 1
14
# Other occurences can be used for future.
15
else:
16
available += counter[i] - 1
17
i -= 1
18
return ans

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0