Skip to content

Commit

Permalink
[Search] Add a solution to Peak Index in a Mountain Array, update the…
Browse files Browse the repository at this point in the history
… solution to Find Peak Element
  • Loading branch information
soapyigu committed Jul 5, 2018
1 parent ed5f57c commit 3bb8109
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 20 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
* [Microsoft](#microsoft)

## Progress
[Problem Status](#problem-status) shows the latest progress to all 800+ questions. Currently we have 264 completed solutions. Note: questions with ♥ mark means that you have to **Subscript to premium membership** of LeetCode to unlock them. Thank you for great contributions from [CharleneJiang](https://github.com/CharleneJiang), [ReadmeCritic](https://github.com/ReadmeCritic), [demonkoo](https://github.com/demonkoo), [DaiYue](https://github.com/DaiYue), [Quaggie](https://github.com/Quaggie) and [jindulys](https://github.com/jindulys).
[Problem Status](#problem-status) shows the latest progress to all 800+ questions. Currently we have 265 completed solutions. Note: questions with ♥ mark means that you have to **Subscript to premium membership** of LeetCode to unlock them. Thank you for great contributions from [CharleneJiang](https://github.com/CharleneJiang), [ReadmeCritic](https://github.com/ReadmeCritic), [demonkoo](https://github.com/demonkoo), [DaiYue](https://github.com/DaiYue), [Quaggie](https://github.com/Quaggie) and [jindulys](https://github.com/jindulys).


## Array
Expand Down Expand Up @@ -306,6 +306,7 @@
[Search a 2D Matrix II](https://leetcode.com/problems/search-a-2d-matrix-ii/)| [Swift](./Search/Search2DMatrixII.swift)| Medium| O(m + n)| O(1)|
[Search for a Range](https://leetcode.com/problems/search-for-a-range/)| [Swift](./Search/SearchForARange.swift)| Medium| O(logn)| O(1)|
[Search Insert Position](https://leetcode.com/problems/search-insert-position/)| [Swift](./Search/SearchForARange.swift)| Medium| O(logn)| O(1)|
[Peak Index in a Mountain Array](https://leetcode.com/problems/peak-index-in-a-mountain-array/)| [Swift](./Search/PeakIndexMountainArray.swift)| Easy| O(logn)| O(1)|
[Find Peak Element](https://leetcode.com/problems/find-peak-element/)| [Swift](./Search/FindPeakElement.swift)| Medium| O(logn)| O(1)|
[Sqrt(x)](https://leetcode.com/problems/sqrtx/)| [Swift](./Search/Sqrtx.swift)| Medium| O(logn)| O(1)|
[Median of Two Sorted Arrays](https://leetcode.com/problems/median-of-two-sorted-arrays/)| [Swift](./Search/MedianTwoSortedArrays.swift)| Hard| O(log(m + n))| O(1)|
Expand Down
27 changes: 8 additions & 19 deletions Search/FindPeakElement.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,30 +6,19 @@
*/

class FindPeakElement {
func findPeakElement(nums: [Int]) -> Int {
guard nums.count > 1 else {
return 0
}

var left = 1
var right = nums.count - 2
var mid = 0
func findPeakElement(_ nums: [Int]) -> Int {
var left = 0, right = nums.count - 1, mid = 0

while left <= right {
while left < right {
mid = (right - left) / 2 + left
if nums[mid] > nums[mid - 1] && nums[mid] > nums[mid + 1] {
return mid
} else if nums[mid] < nums[mid + 1] {
left = mid + 1

if nums[mid] > nums[mid + 1] {
right = mid
} else {
right = mid - 1
left = mid + 1
}
}

if nums[left] >= nums[right] {
return left
} else {
return right
}
return left
}
}
25 changes: 25 additions & 0 deletions Search/PeakIndexMountainArray.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/**
* Question Link: https://leetcode.com/problems/peak-index-in-a-mountain-array/
* Primary idea: Classic Binary Search
*
* Time Complexity: O(logn), Space Complexity: O(1)
*/


class PeakIndexMountainArray {
func peakIndexInMountainArray(_ A: [Int]) -> Int {
var left = 0, right = A.count - 1, mid = 0

while left < right {
mid = (right - left) / 2 + left

if A[mid] > A[mid + 1] {
right = mid
} else {
left = mid + 1
}
}

return left
}
}

0 comments on commit 3bb8109

Please sign in to comment.