diff options
Diffstat (limited to 'max_element_decr_rearr.py')
-rw-r--r-- | max_element_decr_rearr.py | 35 |
1 files changed, 35 insertions, 0 deletions
diff --git a/max_element_decr_rearr.py b/max_element_decr_rearr.py new file mode 100644 index 0000000..3f1267d --- /dev/null +++ b/max_element_decr_rearr.py @@ -0,0 +1,35 @@ +# 1846. Maximum Element After Decreasing and Rearranging + +""" +given an array of positive integers 'arr', perform some operations on 'arr' +so that it satisfies these conditions: +- the value of the first element in 'arr' must be 1 +- the absolute difference between any two adjacent elements must be less than +or equal to 1. in other words 'abs(arr[i] - arr[i - 1]) <= 1' for each i +where 1 <= i < arr.length (0-indexed). 'abs(x)' is the absolute value of 'x' +there are two types operations that you can perform any number of times: +decrease the value of any element of 'arr' to a smaller positive integer +rearrange the elements of 'arr' to be in any order +return the maximum possible value of an element in 'arr' after performing the +operations to satisfy the conditions. +""" + + +class Solution(object): + def maximumElementAfterDecrementingAndRearranging(self, arr): + """ + :type arr: List[int] + :rtype: int + """ + arr.sort() + ans = 0 + for i in arr: + ans = min(ans + 1, i) + return ans + + +if __name__ == "__main__": + obj = Solution() + print(obj.maximumElementAfterDecrementingAndRearranging(arr=[2, 2, 1, 2, 1])) + print(obj.maximumElementAfterDecrementingAndRearranging(arr=[100, 1, 1000])) + print(obj.maximumElementAfterDecrementingAndRearranging(arr=[1, 2, 3, 4, 5])) |