-
-
Save kyle-mccarthy/c745f5dd7921d48917c6 to your computer and use it in GitHub Desktop.
find min pivot value
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
## https://oj.leetcode.com/problems/find-minimum-in-rotated-sorted-array/ | |
class Solution: | |
def findMin(self, nums): | |
mem = list() | |
mem.append(nums[0]) | |
mem = self.findMinRecur(nums, mem) | |
return mem[0] | |
## split the list into a left and right subset until the left and right | |
## side only contain one value each. Then check that value against the | |
## mem subset to see if it is less current least value. | |
def findMinRecur(self, nums, mem): | |
## we isolated a value | |
if len(nums) == 1: | |
## the isolated value is less than the current minimum value | |
if nums[0] < mem[0]: | |
del mem[:] | |
mem.append(nums[0]) | |
## an equal min pivot was found, add it to the memory list | |
elif nums[0] == mem[0]: | |
mem.append(nums[0]) | |
## continue to split the subset | |
elif len(nums) > 1: | |
left, right = self.split(nums) | |
self.findMinRecur(left, mem) | |
self.findMinRecur(right, mem) | |
return mem | |
## split the array into two parts subsets | |
def split(self, sub): | |
if len(sub) == 1: | |
return sub, list() | |
left = sub[0:len(sub)//2] | |
right = sub[len(sub)//2:] | |
return left, right | |
## get the numbers | |
#inp = input("Enter numbers: ") | |
inp = "4 5 6 7 0 1 2" | |
nums = [int(x) for x in inp.split(' ') if x.strip()] | |
sol = Solution() | |
print(sol.findMin(nums)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment