-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathminimun_replace_sort_array.py
62 lines (36 loc) · 1.51 KB
/
minimun_replace_sort_array.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# https://leetcode.com/problems/minimum-replacements-to-sort-the-array/
"""
-> Time Limit pero correcto
def minimumReplacement(nums: list) -> int:
replacements = 0
for i in range(len(nums)-2,-1,-1):
if nums[i]>nums[i+1]:
new_left_number = nums[i]
while True:
if new_left_number // nums[i+1] >= 2:
new_left_number= new_left_number // nums[i+1]
replacements+=1
elif new_left_number==nums[i+1]:
nums[i]=new_left_number
break
else:
#poir
if new_left_number % 2 ==0:
new_left_number = new_left_number / 2
else:
new_left_number = new_left_number // 2
replacements+=1
nums[i]=new_left_number
return replacements """
def minimumReplacement(nums: list) -> int:
replacements = 0
for i in range(len(nums)-2, -1, -1):
if nums[i] > nums[i+1]:
nums_elements = (nums[i] + nums[i + 1] - 1) // nums[i + 1]
replacements += (nums_elements-1)
nums[i] = nums[i] // nums_elements
print(nums[i])
return int(replacements)
""" print(minimumReplacement([3,9,3]))
print(minimumReplacement([1,2,3,4,5])) """
print(minimumReplacement([2, 10, 20, 19, 1]))