[JS] 2562번 - 최댓값

박세현·2021년 5월 21일
0

알고리즘

목록 보기
3/19

최댓값

문제 출처

https://www.acmicpc.net/problem/2562


key point


// map의 첫번째 인자는 요소
// 두번째 인자는 배열의 index

// numArr = [1, 2, 3, 4, 5]
numArr.map((num, idx) => {
  console.log(`${num} ${idx}`);
}
/*
    결과값
    1, 0
    2, 1
    3, 2
    4, 3
    5, 4
*/

풀이

입력값을 배열로 저장한 후 map을 이용해서 최대값을 구했다.
최대값이 몇 번째 수인지 구하는 것은 배열의 index에 1을 더하는 것으로 (배열의 index는 0부터 시작하므로) 구할 수 있다.

let input = require('fs').readFileSync('/dev/stdin').toString().split('\n');

const numArr = input.map((x) => Number(x));

let max = numArr[0];
let index = 1;

numArr.map((num, idx) => {
  if (num > max) {
    max = num;
    index = idx + 1;
  }
});

console.log(`${max}\n${index}`);

profile
Front End 공부노트

0개의 댓글

관련 채용 정보