forked from tangweikun/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
0553d6a
commit c1f9f46
Showing
5 changed files
with
60 additions
and
0 deletions.
There are no files selected for viewing
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
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
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) |
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
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 | ||
} |
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
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) | ||
}) |
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