Skip to content

Instantly share code, notes, and snippets.

@coderinblack08
Created June 14, 2020 01:43
Show Gist options
  • Save coderinblack08/09a4859db4f7fb1acf16750b6ed4c572 to your computer and use it in GitHub Desktop.
Save coderinblack08/09a4859db4f7fb1acf16750b6ed4c572 to your computer and use it in GitHub Desktop.
// 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