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

Create findRelativeMaximumPointCount.js #771

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
raklaptudirm merged 16 commits into TheAlgorithms:master from jhonDoe15:patch-2
Oct 28, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
16 commits
Select commit Hold shift + click to select a range
d70df40
Create find_relative_maximum_point_count.js
jhonDoe15 Oct 10, 2021
1f849a8
rename file to match requested casing
jhonDoe15 Oct 10, 2021
868c80e
add inline comments and greater documentation
jhonDoe15 Oct 20, 2021
0824ec1
fix wrong reference to algorithm explanation
jhonDoe15 Oct 20, 2021
abed03d
remove live code and fix function misnaming
jhonDoe15 Oct 27, 2021
5588de6
add multiple cases tests
jhonDoe15 Oct 27, 2021
7cb522e
add last line as empty line
jhonDoe15 Oct 27, 2021
96d0de0
git pull
jhonDoe15 Oct 27, 2021
cee9038
style changes
jhonDoe15 Oct 27, 2021
9cb75be
move tests to test folder
jhonDoe15 Oct 28, 2021
a5d9830
chore: fix spelling
raklaptudirm Oct 28, 2021
bf9a6f8
fix package-lock
jhonDoe15 Oct 28, 2021
c3051f3
Merge branch 'patch-2' of https://github.com/jhonDoe15/Javascript int...
jhonDoe15 Oct 28, 2021
dba60ca
Merge branch 'master' into patch-2
jhonDoe15 Oct 28, 2021
056e483
revert to old lock file
jhonDoe15 Oct 28, 2021
28d17ac
chore: add line feed
raklaptudirm Oct 28, 2021
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 42 additions & 0 deletions Data-Structures/Array/NumberOfLocalMaximumPoints.js
View file Open in desktop
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/**
* [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
*
* Notes:
* - like the other similar local maxima search function find relative maxima points in array but doesnt stop at one but returns total point count
* - runs on array A of size n and returns the local maxima count using divide and conquer methodology
*
* @complexity: O(n) (on average )
* @complexity: O(n) (worst case)
* @flow
*/

// check if returned index is a local maxima
const IsMaximumPoint = (array, index) => {
// handle array bounds
// array start
if (index === 0) {
return array[index] > array[index + 1]
// array end
} else if (index === array.length - 1) {
return array[index] > array[index - 1]
// handle index inside array bounds
} else {
return array[index] > array[index + 1] && array[index] > array[index - 1]
}
}

const CountLocalMaximumPoints = (array, startIndex, endIndex) => {
// stop check in divide and conquer recursion
if (startIndex === endIndex) {
return IsMaximumPoint(array, startIndex) ? 1 : 0
}

// handle the two halves
const middleIndex = parseInt((startIndex + endIndex) / 2)
return CountLocalMaximumPoints(array, startIndex, middleIndex) +
CountLocalMaximumPoints(array, middleIndex + 1, endIndex)
}

const NumberOfLocalMaximumPoints = (A) => CountLocalMaximumPoints(A, 0, A.length - 1)

export { NumberOfLocalMaximumPoints }
43 changes: 43 additions & 0 deletions Data-Structures/Array/test/NumberOfLocalMaximumPoints.test.js
View file Open in desktop
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import { NumberOfLocalMaximumPoints } from '../NumberOfLocalMaximumPoints'

describe('LocalMaximomPoint tests', () => {
it('test boundry maximom points - last element', () => {
const Array = [1, 2, 3, 4, 5, 6, 12]
expect(NumberOfLocalMaximumPoints(Array)).toEqual(1)
})

it('test boundry maximom points - first element', () => {
const Array = [13, 6, 5, 4, 3, 2, 1]
expect(NumberOfLocalMaximumPoints(Array)).toEqual(1)
})

it('test boundry maximom points - both boundries have maximum points', () => {
// Test a mix of number types (i.e., positive/negative, numbers with decimals, fractions)
const Array = [13, 2, 3, 4, 5, 6, 12]
expect(NumberOfLocalMaximumPoints(Array)).toEqual(2)
})

it('multiple maximom points in the middle', () => {
// Test a mix of number types (i.e., positive/negative, numbers with decimals, fractions)
const Array = [1, 3, 2, 5, 6, 9, 2, 7, 12, 1, 0]
expect(NumberOfLocalMaximumPoints(Array)).toEqual(3)
})

it('multiple maximom points in the middle with one at end', () => {
// Test a mix of number types (i.e., positive/negative, numbers with decimals, fractions)
const Array = [1, 3, 2, 5, 6, 9, 2, 7, 12, 1, 10]
expect(NumberOfLocalMaximumPoints(Array)).toEqual(4)
})

it('multiple maximom points in the middle with one at start', () => {
// Test a mix of number types (i.e., positive/negative, numbers with decimals, fractions)
const Array = [10, 3, 2, 5, 6, 9, 2, 7, 12, 1, 0]
expect(NumberOfLocalMaximumPoints(Array)).toEqual(3)
})

it('multiple maximom points in the middle with two more at both ends', () => {
// Test a mix of number types (i.e., positive/negative, numbers with decimals, fractions)
const Array = [10, 3, 11, 5, 6, 9, 2, 7, 12, 1, 10]
expect(NumberOfLocalMaximumPoints(Array)).toEqual(5)
})
})

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