Re: [閒聊] 每日LeetCode

作者: Rushia (みけねこ的鼻屎)   2023-04-05 20:30:00
2439. Minimize Maximum of Array
給你一個陣列 nums ,你可以做無限次數的下列一串操作(必須同時做):
1. i 必須滿足 1 <= i < n 且 nums[i] > 0
2.把 nums[i - 1] 遞增
3.把 nums[i] 遞減
試求經過多次操作後,陣列裡的最大值是多少? 這個最大值要盡可能小。
Example:
Input: nums = [3,7,1,6]
Output: 5
Explanation:
One set of optimal operations is as follows:
1. Choose i = 1, and nums becomes [4,6,1,6].
2. Choose i = 3, and nums becomes [4,6,2,5].
3. Choose i = 1, and nums becomes [5,5,2,5].
The maximum integer of nums is 5. It can be shown that the maximum number
cannot be less than 5.
Therefore, we return 5.
Input: nums = [10,1]
Output: 10
Explanation:
It is optimal to leave nums as is, and since 10 is the maximum value, we
return 10.
思路:
1.題目給的操作告訴我們可以把右邊數字的數量往左移,我們必須找出一個數字可以
滿足所有的數字透過多次左移後都小於等於他,要取下界那就是二分搜索了。
2.搜索範圍會是(0, MAX(nums)),攥寫一個函數檢查當前的數字是不是合法,用
have紀錄左邊還可以被右邊填充多少數量,如果檢查結果不合法就往右半邊繼續搜索
,否則把右邊界固定為當前邊界。
Java Code:
作者: h0103661 (路人喵)   2023-04-05 20:37:00
這題設計還蠻有趣的,實際上是在求最大平均

Links booklink

Contact Us: admin [ a t ] ucptt.com