Files
JavaScript/Data-Structures/Array/NumberOfLocalMaximumPoints.js
Roland Hummel 86d333ee94 feat: Test running overhaul, switch to Prettier & reformat everything (#1407)
* chore: Switch to Node 20 + Vitest

* chore: migrate to vitest mock functions

* chore: code style (switch to prettier)

* test: re-enable long-running test

Seems the switch to Node 20 and Vitest has vastly improved the code's and / or the test's runtime!

see #1193

* chore: code style

* chore: fix failing tests

* Updated Documentation in README.md

* Update contribution guidelines to state usage of Prettier

* fix: set prettier printWidth back to 80

* chore: apply updated code style automatically

* fix: set prettier line endings to lf again

* chore: apply updated code style automatically

---------

Co-authored-by: github-actions <${GITHUB_ACTOR}@users.noreply.github.com>
Co-authored-by: Lars Müller <34514239+appgurueu@users.noreply.github.com>
2023-10-04 02:38:19 +05:30

46 lines
1.5 KiB
JavaScript

/**
* [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 doesn't 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 }