You are given an array of positive integers
arr
. Perform some operations (possibly none) onarr
so that it satisfies these conditions:
- The value of the first element in
arr
must be1
.- The absolute difference between any 2 adjacent elements must be less than or equal to
1
. In other words,abs(arr[i] - arr[i - 1]) <= 1
for eachi
where1 <= i < arr.length
(0-indexed).abs(x)
is the absolute value ofx
.
There are 2 types of 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.
Constraints:
1 <= arr.length <= 105
1 <= arr[i] <= 109
給你一個正整數的陣列
arr
。 對arr
執行幾種操作(也可能不做任何事)使其滿足以下條件:
arr
中的第一個元素必須是1
。- 兩個相鄰元素的絕對值必須小於等於
1
。換句話說,abs(arr[i] - arr[i - 1]) <= 1
對於任何i
為1 <= i < arr.length
(索引值從0
開始)。abs(x)
是x
的絕對值.
有兩種操作你可以執行任意次數:
- 減少
arr
中任意元素的值變為更小的正整數。- 重新排列
arr
的元素變為任意順序。
回傳
arr
經過操作並滿足條件時,陣列中可能的最大值為多少。
限制:
1 <= arr.length <= 105
1 <= arr[i] <= 109
1
2
,將最大值紀錄為 2
(因為絕對值最多只能是 1
)2
,最大值不變LeetCode
C++