Skip to content

Instantly share code, notes, and snippets.

@restart916
Created June 19, 2019 22:09
Show Gist options
  • Save restart916/e742f4f94a60c6dacb41ace5c6b607bc to your computer and use it in GitHub Desktop.
Save restart916/e742f4f94a60c6dacb41ace5c6b607bc to your computer and use it in GitHub Desktop.
20190619_leetcode_852_PeakIndexinaMountainArray
/**
* @param {number[]} A
* @return {number}
*/
var peakIndexInMountainArray = function(A) {
let maxIndex = 0;
let index = 0;
A.reduce((a,c) => {
index++;
if (a < c) { maxIndex = index; }
return Math.max(a,c)
});
return maxIndex;
};
@restart916
Copy link
Author

그 저도 reduce 란걸 써보고 싶어서 사용했는데 이게 max 값이 아니라 인덱스를 반환해야 되는 문제라서 결국 뭔가 애매한 코드가 나왔네요.

@graynun
Copy link

graynun commented Jun 20, 2019

reduce 나 map 모두 사실 인자가 뒤에 더 옵니다(보통 index 와 array 순으로 옵니다) 이것때문에 가끔 엉뚱한 연산결과를 내서 고통받는 경우도 있으니 주의하셔용,,,, (first class citizen + parameter에 기본값 undefined 가 꽂혀서 일어나는 JS만의 대환장쑈입니다...)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment