LeetCode Entry
1846. Maximum Element After Decreasing and Rearranging
Max possible grow of +0 or +1
1846. Maximum Element After Decreasing and Rearranging medium substack youtube
https://dmitrysamoylenko.com/leetcode/

Join me on Telegram
https://t.me/leetcode_daily_unstoppable/1404
Problem TLDR
Max possible grow of +0 or +1
Intuition
Sort. Iterate. Take at most prev + 1.
Approach
- can it be solved in O(n)? yes, count sort, the max value is the arr.size
Complexity
-
Time complexity: \(O(nlogn)\)
-
Space complexity: \(O(1)\)
Code
fun maximumElementAfterDecrementingAndRearranging(a: IntArray) =
a.sorted().fold(0) { r, t -> min(r+1,t) }
pub fn maximum_element_after_decrementing_and_rearranging(a: Vec<i32>) -> i32 {
a.into_iter().sorted().fold(0,|r,t|t.min(r+1))
}
Comments