diff --git a/README.md b/README.md index fd211d9..2eb46a8 100644 --- a/README.md +++ b/README.md @@ -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 | diff --git a/src/findPeakElement/README.md b/src/findPeakElement/README.md new file mode 100644 index 0000000..6553419 --- /dev/null +++ b/src/findPeakElement/README.md @@ -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) diff --git a/src/findPeakElement/index.ts b/src/findPeakElement/index.ts new file mode 100644 index 0000000..8882d7c --- /dev/null +++ b/src/findPeakElement/index.ts @@ -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 +} diff --git a/src/findPeakElement/test.ts b/src/findPeakElement/test.ts new file mode 100644 index 0000000..bfb6094 --- /dev/null +++ b/src/findPeakElement/test.ts @@ -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) +}) diff --git a/src/index.ts b/src/index.ts index 8db2226..28226ce 100644 --- a/src/index.ts +++ b/src/index.ts @@ -114,3 +114,4 @@ export * from './degreeOfAnArray' export * from './findAllDuplicatesInAnArray' export * from './findMinimumInRotatedSortedArray' export * from './findMinimumInRotatedSortedArray2' +export * from './findPeakElement'