-
-
Notifications
You must be signed in to change notification settings - Fork 5.7k
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
Merged
Changes from 6 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 1f849a8
rename file to match requested casing
jhonDoe15 868c80e
add inline comments and greater documentation
jhonDoe15 0824ec1
fix wrong reference to algorithm explanation
jhonDoe15 abed03d
remove live code and fix function misnaming
jhonDoe15 5588de6
add multiple cases tests
jhonDoe15 7cb522e
add last line as empty line
jhonDoe15 96d0de0
git pull
jhonDoe15 cee9038
style changes
jhonDoe15 9cb75be
move tests to test folder
jhonDoe15 a5d9830
chore: fix spelling
raklaptudirm bf9a6f8
fix package-lock
jhonDoe15 c3051f3
Merge branch 'patch-2' of https://github.com/jhonDoe15/Javascript int…
jhonDoe15 dba60ca
Merge branch 'master' into patch-2
jhonDoe15 056e483
revert to old lock file
jhonDoe15 28d17ac
chore: add line feed
raklaptudirm File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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,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 | ||
let 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 } |
This file contains hidden or 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,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 maximom 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) | ||
}) | ||
}) | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.