Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 9ad93c7

Browse files
jhonDoe15raklaptudirm
andauthored
merge: Create findRelativeMaximumPointCount.js (#771)
* Create find_relative_maximum_point_count.js print number of relative maximum points in array runs in O(n) * rename file to match requested casing * add inline comments and greater documentation * fix wrong reference to algorithm explanation * remove live code and fix function misnaming * add multiple cases tests * add last line as empty line * git pull * style changes * move tests to test folder * chore: fix spelling * fix package-lock * revert to old lock file * chore: add line feed Co-authored-by: Rak Laptudirm <raklaptudirm@gmail.com>
1 parent 00900f1 commit 9ad93c7

File tree

2 files changed

+85
-0
lines changed

2 files changed

+85
-0
lines changed
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
/**
2+
* [NumberOfLocalMaximumPoints](https://www.geeksforgeeks.org/find-indices-of-all-local-maxima-and-local-minima-in-an-array/) is an algorithm to find relative bigger numbers compared to their neighbors
3+
*
4+
* Notes:
5+
* - like the other similar local maxima search function find relative maxima points in array but doesnt stop at one but returns total point count
6+
* - runs on array A of size n and returns the local maxima count using divide and conquer methodology
7+
*
8+
* @complexity: O(n) (on average )
9+
* @complexity: O(n) (worst case)
10+
* @flow
11+
*/
12+
13+
// check if returned index is a local maxima
14+
const IsMaximumPoint = (array, index) => {
15+
// handle array bounds
16+
// array start
17+
if (index === 0) {
18+
return array[index] > array[index + 1]
19+
// array end
20+
} else if (index === array.length - 1) {
21+
return array[index] > array[index - 1]
22+
// handle index inside array bounds
23+
} else {
24+
return array[index] > array[index + 1] && array[index] > array[index - 1]
25+
}
26+
}
27+
28+
const CountLocalMaximumPoints = (array, startIndex, endIndex) => {
29+
// stop check in divide and conquer recursion
30+
if (startIndex === endIndex) {
31+
return IsMaximumPoint(array, startIndex) ? 1 : 0
32+
}
33+
34+
// handle the two halves
35+
const middleIndex = parseInt((startIndex + endIndex) / 2)
36+
return CountLocalMaximumPoints(array, startIndex, middleIndex) +
37+
CountLocalMaximumPoints(array, middleIndex + 1, endIndex)
38+
}
39+
40+
const NumberOfLocalMaximumPoints = (A) => CountLocalMaximumPoints(A, 0, A.length - 1)
41+
42+
export { NumberOfLocalMaximumPoints }
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
import { NumberOfLocalMaximumPoints } from '../NumberOfLocalMaximumPoints'
2+
3+
describe('LocalMaximomPoint tests', () => {
4+
it('test boundry maximom points - last element', () => {
5+
const Array = [1, 2, 3, 4, 5, 6, 12]
6+
expect(NumberOfLocalMaximumPoints(Array)).toEqual(1)
7+
})
8+
9+
it('test boundry maximom points - first element', () => {
10+
const Array = [13, 6, 5, 4, 3, 2, 1]
11+
expect(NumberOfLocalMaximumPoints(Array)).toEqual(1)
12+
})
13+
14+
it('test boundry maximom points - both boundries have maximum points', () => {
15+
// Test a mix of number types (i.e., positive/negative, numbers with decimals, fractions)
16+
const Array = [13, 2, 3, 4, 5, 6, 12]
17+
expect(NumberOfLocalMaximumPoints(Array)).toEqual(2)
18+
})
19+
20+
it('multiple maximom points in the middle', () => {
21+
// Test a mix of number types (i.e., positive/negative, numbers with decimals, fractions)
22+
const Array = [1, 3, 2, 5, 6, 9, 2, 7, 12, 1, 0]
23+
expect(NumberOfLocalMaximumPoints(Array)).toEqual(3)
24+
})
25+
26+
it('multiple maximom points in the middle with one at end', () => {
27+
// Test a mix of number types (i.e., positive/negative, numbers with decimals, fractions)
28+
const Array = [1, 3, 2, 5, 6, 9, 2, 7, 12, 1, 10]
29+
expect(NumberOfLocalMaximumPoints(Array)).toEqual(4)
30+
})
31+
32+
it('multiple maximom points in the middle with one at start', () => {
33+
// Test a mix of number types (i.e., positive/negative, numbers with decimals, fractions)
34+
const Array = [10, 3, 2, 5, 6, 9, 2, 7, 12, 1, 0]
35+
expect(NumberOfLocalMaximumPoints(Array)).toEqual(3)
36+
})
37+
38+
it('multiple maximom points in the middle with two more at both ends', () => {
39+
// Test a mix of number types (i.e., positive/negative, numbers with decimals, fractions)
40+
const Array = [10, 3, 11, 5, 6, 9, 2, 7, 12, 1, 10]
41+
expect(NumberOfLocalMaximumPoints(Array)).toEqual(5)
42+
})
43+
})

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /