Maximum Element After Decreasing and Rearranging LeetCode solution in Python
1class Solution:2def maximumElementAfterDecrementingAndRearranging(self, arr: List[int]) -> int:3counter = collections.Counter(arr)4available = sum(n > len(arr) for n in arr)5i = ans = len(arr)6while i > 0:7# This number is not in arr8if not counter[i]:9# Use another number to fill in its place. If we cannot, we have to decrease our max10if available:11available -= 112else:13ans -= 114# Other occurences can be used for future.15else:16available += counter[i] - 117i -= 118return ans