-
Notifications
You must be signed in to change notification settings - Fork 2
/
33.py
133 lines (116 loc) · 3.44 KB
/
33.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
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
124
125
126
127
128
129
130
131
132
133
# [ LeetCode ] 33. Search in Rotated Sorted Array
def solution(nums: list[int], target: int) -> int:
def find_rotate_index(start: int, end: int) -> int:
if nums[start] < nums[end]:
return 0
else:
while start <= end:
middle: int = start + (end - start) // 2
if nums[middle] > nums[middle + 1]:
return middle + 1
elif nums[middle] < nums[start]:
end = middle - 1
else:
start = middle + 1
def binary_search(start: int, end: int) -> int:
while start <= end:
middle: int = start + (end - start) // 2
if nums[middle] > target:
end = middle - 1
elif nums[middle] < target:
start = middle + 1
else:
return middle
return -1
n: int = len(nums) - 1
if n == 0:
return 0 if nums[0] == target else -1
else:
pivot: int = find_rotate_index(start=0, end=n)
if nums[pivot] == target:
return pivot
elif pivot == 0:
return binary_search(start=0, end=n)
elif nums[0] > target:
return binary_search(start=pivot, end=n)
else:
return binary_search(start=0, end=pivot)
def another_solution(nums: list[int], target: int) -> int:
start, end = 0, len(nums) - 1
while start <= end:
middle: int = start + (end - start) // 2
if nums[middle] == target:
return middle
elif nums[middle] >= nums[start]:
if nums[start] <= target and nums[middle] > target:
end = middle - 1
else:
start = middle + 1
else:
if nums[end] >= target and nums[middle] < target:
start = middle + 1
else:
end = middle - 1
return -1
if __name__ == "__main__":
cases: list[dict[str, dict[list[int | int]] | int]] = [
{
"input": {
"nums": [4, 5, 6, 7, 0, 1, 2],
"target": 0
},
"output": 4
},
{
"input": {
"nums": [4, 5, 6, 7, 0, 1, 2],
"target": 3
},
"output": -1
},
{
"input": {
"nums": [1],
"target": 0
},
"output": -1
},
{
"input": {
"nums": [1, 3],
"target": 3
},
"output": 1
},
{
"input": {
"nums": [1, 3],
"target": 1
},
"output": 0
},
{
"input": {
"nums": [3, 1],
"target": 3
},
"output": 0
},
{
"input": {
"nums": [1, 2, 3, 4, 5, 6],
"target": 4
},
"output": 3
},
{
"input": {
"nums": [4, 5, 6, 7, 8, 1, 2, 3],
"target": 8
},
"output": 4
}
]
for case in cases:
assert case["output"] == solution(**case["input"])
assert case["output"] == another_solution(**case["input"])