Skip to content

Commit

Permalink
✨ Find Peak Element
Browse files Browse the repository at this point in the history
  • Loading branch information
tangweikun committed Feb 8, 2018
1 parent 0553d6a commit c1f9f46
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -139,3 +139,4 @@ npm run test
| 115 | [FindAllDuplicatesInAnArray](src/findAllDuplicatesInAnArray/index.ts) | [:green_book:](src/findAllDuplicatesInAnArray/README.md) | Easy | [LeetCode](https://leetcode.com/problems/find-all-duplicates-in-an-array) | Array |
| 116 | [FindMinimumInRotatedSortedArray](src/findMinimumInRotatedSortedArray/index.ts) | [:green_book:](src/findMinimumInRotatedSortedArray/README.md) | Easy | [LeetCode](https://leetcode.com/problems/find-minimum-in-rotated-sorted-array) | Array |
| 117 | [FindMinimumInRotatedSortedArray2](src/findMinimumInRotatedSortedArray2/index.ts) | [:green_book:](src/findMinimumInRotatedSortedArray2/README.md) | Easy | [LeetCode](https://leetcode.com/problems/find-minimum-in-rotated-sorted-array-ii) | Array |
| 118 | [FindPeakElement](src/findPeakElement/index.ts) | [:green_book:](src/findPeakElement/README.md) | Medium | [LeetCode](https://leetcode.com/problems/find-peak-element) | Array |
20 changes: 20 additions & 0 deletions src/findPeakElement/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# 118.Find Peak Element

## Description

A peak element is an element that is greater than its neighbors.
Given an input array where num[i] ≠ num[i+1], find a peak element and return its index.
The array may contain multiple peaks, in that case return the index to any one of the peaks is fine.
You may imagine that num[-1] = num[n] = -∞.
For example, in array [1, 2, 3, 1], 3 is a peak element and your function should return the index number 2.

## Example

```javascript
Input: [1, 2, 3, 1]
Output: 2
```

## From

[LeetCode](https://leetcode.com/problems/find-peak-element)
13 changes: 13 additions & 0 deletions src/findPeakElement/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
export function findPeakElement(nums: number[]) {
let max = -Infinity
let position = 0

for (let i = 0; i < nums.length; i++) {
if (nums[i] > max) {
position = i
max = nums[i]
}
}

return position
}
25 changes: 25 additions & 0 deletions src/findPeakElement/test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { findPeakElement } from '.'

test('FindPeakElement-1', () => {
expect(findPeakElement([0])).toBe(0)
})

test('FindPeakElement-2', () => {
expect(findPeakElement([0, 1, 2])).toBe(2)
})

test('FindPeakElement-3', () => {
expect(findPeakElement([1, 0])).toBe(0)
})

test('FindPeakElement-4', () => {
expect(findPeakElement([2, 3, 4, 2, 4, 3])).toBe(2)
})

test('FindPeakElement-5', () => {
expect(findPeakElement([5, 0, 5])).toBe(0)
})

test('FindPeakElement-6', () => {
expect(findPeakElement([6, 3, 4, 6, 7, 6])).toBe(4)
})
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -114,3 +114,4 @@ export * from './degreeOfAnArray'
export * from './findAllDuplicatesInAnArray'
export * from './findMinimumInRotatedSortedArray'
export * from './findMinimumInRotatedSortedArray2'
export * from './findPeakElement'

0 comments on commit c1f9f46

Please sign in to comment.