Created
June 14, 2020 01:43
-
-
Save coderinblack08/09a4859db4f7fb1acf16750b6ed4c572 to your computer and use it in GitHub Desktop.
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
// Problem Statement: 1D Peak Finder Algorithm | |
// Key concept: Divide and Conquer Strategy | |
// MIT Link: https://courses.csail.mit.edu/6.006/spring11/lectures/lec02.pdf | |
const list = [1, 2, 6, 5, 3, 7, 4]; | |
const findPeak = (list, h) => { | |
if (list[h - 1] <= list[h] && list[h] >= list[h + 1]) | |
return list[h]; | |
else if (list[h - 1] > list[h]) | |
return findPeak(list, h - 1); | |
else if (list[h + 1] > list[h]) | |
return findPeak(list, h + 1); | |
} | |
console.log(findPeak(list, Math.floor(list.length / 2))); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment