Created
May 22, 2019 03:00
-
-
Save kmdarshan/8a0ba0deef7663848caca97ae0710c98 to your computer and use it in GitHub Desktop.
Find Minimum in Rotated Sorted Array II
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
func findMin(_ nums: [Int]) -> Int { | |
let firstElement = nums[0] | |
let lastElement = nums[nums.count-1] | |
if(firstElement < lastElement) | |
{ | |
// go from the first element | |
var minElement = firstElement | |
for item in nums | |
{ | |
if !(item > firstElement) && (item < lastElement) | |
{ | |
minElement = item | |
} | |
} | |
print("first ",minElement) | |
return minElement | |
} | |
else | |
{ | |
// go from the last element | |
var minElement = lastElement | |
for item in nums.reversed() | |
{ | |
if (item < minElement) && (item < firstElement) | |
{ | |
minElement = item | |
} | |
} | |
print("last ",minElement) | |
return minElement | |
} | |
return 1 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment