-
-
Notifications
You must be signed in to change notification settings - Fork 50.5k
Expand file tree
/
Copy pathbinary_search_on_answers.py
More file actions
123 lines (86 loc) · 2.6 KB
/
binary_search_on_answers.py
File metadata and controls
123 lines (86 loc) · 2.6 KB
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
from collections.abc import Callable
def binary_search_on_answer(
low: int, high: int, condition: Callable[[int], bool]
) -> int:
"""
Generic Binary Search on Answer template.
Finds the minimum value in the range [low, high] that satisfies the condition.
The condition function must be monotonic:
- False False False True True True
:param low: lower bound of search space
:param high: upper bound of search space
:param condition: function that returns True if mid is a valid answer
:return: smallest value that satisfies the condition
Examples:
>>> def condition(x): return x * x >= 16
>>> binary_search_on_answer(0, 10, condition)
4
>>> def condition(x): return x >= 7
>>> binary_search_on_answer(0, 10, condition)
7
"""
answer = high
while low <= high:
mid = low + (high - low) // 2
if condition(mid):
answer = mid
high = mid - 1
else:
low = mid + 1
return answer
# Doctests-1. Doctests for binary_search_on_answer
"""
Generic Binary Search on Answer template.
Finds the minimum value in the range [low, high] that satisfies the condition.
The condition must be monotonic:
False False False True True True
Examples:
>>> def condition(x): return x * x >= 16
>>> binary_search_on_answer(0, 10, condition)
4
>>> def condition(x): return x >= 7
>>> binary_search_on_answer(0, 10, condition)
7
>>> def condition(x): return x >= 0
>>> binary_search_on_answer(-5, 5, condition)
0
>>> def condition(x): return True
>>> binary_search_on_answer(1, 5, condition)
1
"""
# Example1: minimum capacity to ship
def min_capacity_to_ship(weights: list[int], days: int) -> int:
"""
Find minimum capacity to ship packages within given days.
>>> min_capacity_to_ship([1,2,3,4,5,6,7,8,9,10], 5)
15
"""
def can_ship(capacity: int) -> bool:
days_used = 1
current = 0
for weight in weights:
if current + weight > capacity:
days_used += 1
current = 0
current += weight
return days_used <= days
return binary_search_on_answer(max(weights), sum(weights), can_ship)
# Doctest-2. Doctests for min_capacity_to_ship
"""
Find minimum capacity to ship packages within given days.
Examples:
>>> min_capacity_to_ship([1,2,3,4,5,6,7,8,9,10], 5)
15
>>> min_capacity_to_ship([3,2,2,4,1,4], 3)
6
>>> min_capacity_to_ship([1,2,3,1,1], 4)
3
>>> min_capacity_to_ship([10], 1)
10
"""
# Edge case to be handled
"""
>>> def condition(x): return x >= 100
>>> binary_search_on_answer(0, 50, condition)
50
"""