문제 설명
경기 점수의 배열이 주어지고, 최저점을 갱신한 횟수와 최고점을 갱신한 횟수를 계산하면 되는 문제!
'use strict';
const fs = require('fs');
process.stdin.resume();
process.stdin.setEncoding('utf-8');
let inputString = '';
let currentLine = 0;
process.stdin.on('data', function(inputStdin) {
inputString += inputStdin;
});
process.stdin.on('end', function() {
inputString = inputString.split('\n');
main();
});
function readLine() {
return inputString[currentLine++];
}
/*
* Complete the 'breakingRecords' function below.
*
* The function is expected to return an INTEGER_ARRAY.
* The function accepts INTEGER_ARRAY scores as parameter.
*/
function breakingRecords(scores) {
// Write your code here
let min = [scores[0]]
let min_count = 0
let max = [scores[0]]
let max_count = 0
for(let i=0; i<scores.length; i++) {
if(scores[i] > max[max.length-1]) max.push( scores[i]) && max_count ++
else if(scores[i] < min[min.length-1]) min.push(scores[i]) && min_count ++
}
return([max_count, min_count])
}
function main() {
const ws = fs.createWriteStream(process.env.OUTPUT_PATH);
const n = parseInt(readLine().trim(), 10);
const scores = readLine().replace(/\s+$/g, '').split(' ').map(scoresTemp => parseInt(scoresTemp, 10));
const result = breakingRecords(scores);
ws.write(result.join(' ') + '\n');
ws.end();
}
배열의 가장 첫 값을 min, max값으로 지정해준 뒤,
배열을 돌면서 min, max 값보다 scores의 i 값이 작고, 크면
min, max값을 i 값으로 바꿔주고 min_count, max_count에 하나씩 더해주었다.
해결!