给出一个数列,每一次操作可以将其中一个数换成两个数,两数之和和原数一样,问最少几次操作可以将数列变成单调不减,贪心思想,最后一个数作为初始值,如果前一个数比他大,尽量分裂成更大更相等的几个数,不断和前一个数比直到数列头
1 #2366
2 #Runtime: 432 ms (Beats 100%) Sorry, there are not enough accepted submissions to show data
3 #Memory: 25.8 MB (Beats 100%) Sorry, there are not enough accepted submissions to show data
4
5 class Solution(object):
6 def minimumReplacement(self, nums):
7 """
8 :type nums: List[int]
9 :rtype: int
10 """
11 pre = nums[-1]
12 ans = 0
13 for i in range(len(nums) - 1, -1, -1):
14 d = nums[i] // pre
15 if nums[i] % pre != 0:
16 d += 1
17 ans += d - 1
18 pre = nums[i] // d
19 return ans