Oh right this thing

This commit is contained in:
Dan Buch 2024-03-01 19:48:32 -05:00
parent ad65630b3f
commit a498ff949c
Signed by: meatballhat
GPG Key ID: A12F782281063434
2 changed files with 25 additions and 0 deletions

View File

@ -625,3 +625,16 @@ def traverse_neighborly_node(
continue
yield from traverse_neighborly_node(neighbor, memo)
def find_min_in_rotated_array(nums: list[int]) -> int:
if nums[0] <= nums[-1]:
return nums[0]
if len(nums) <= 3:
return min(nums)
if nums[len(nums) // 2] > nums[-1]:
return find_min_in_rotated_array(nums[len(nums) // 2 :])
return find_min_in_rotated_array(nums[: (len(nums) // 2) + 1])

View File

@ -491,3 +491,15 @@ def test_copy_neighborly_node(inlist):
assert id(orig) != id(copied)
assert orig == copied
assert stuff.neighborly_node_to_list(orig) == stuff.neighborly_node_to_list(copied)
@pytest.mark.parametrize(
("nums", "expected"),
[
([3, 4, 5, 1, 2], 1),
([4, 5, 6, 7, 0, 1, 2], 0),
([11, 13, 15, 17], 11),
],
)
def test_find_min_in_rotated_array(nums: list[int], expected: int):
assert stuff.find_min_in_rotated_array(nums) == expected