Skip to content

Commit

Permalink
Merge pull request soapyigu#125 from soapyigu/Search
Browse files Browse the repository at this point in the history
[Search] Optimize Solution to Find Minimum Rotated Sorted Array
  • Loading branch information
soapyigu authored Nov 15, 2016
2 parents 1fe5781 + b0c22c5 commit 0e50903
Showing 1 changed file with 8 additions and 9 deletions.
17 changes: 8 additions & 9 deletions Search/FindMinimumRotatedSortedArray.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,23 +6,22 @@
*/

class FindMinimumRotatedSortedArray {
func findMin(nums: [Int]) -> Int {
func findMin(_ nums: [Int]) -> Int {
var minVal = Int.max
var left = 0
var right = nums.count - 1
var mid = 0
var minVal = Int.max

while left + 1 < right {
mid = (right - left) / 2 + left
if nums[mid] > nums[left] {
minVal = min(nums[left], minVal)
while left <= right {
let mid = (right - left) / 2 + left
if nums[mid] >= nums[left] {
minVal = min(minVal, nums[left])
left = mid + 1
} else {
minVal = min(nums[mid], minVal)
minVal = min(minVal, nums[mid])
right = mid - 1
}
}

return min(minVal, nums[left], nums[right])
return minVal
}
}

0 comments on commit 0e50903

Please sign in to comment.