Thief of Wealth

www.acmicpc.net/problem/10818

 

10818번: 최소, 최대

첫째 줄에 정수의 개수 N (1 ≤ N ≤ 1,000,000)이 주어진다. 둘째 줄에는 N개의 정수를 공백으로 구분해서 주어진다. 모든 정수는 -1,000,000보다 크거나 같고, 1,000,000보다 작거나 같은 정수이다.

www.acmicpc.net

 

핵심아이디어

머리식힐겸 풀어볼만한 아주 간단한 문제이다.

하지만 Javascript(nodejs)로 풀때 Math.max(...array) 를 사용한 후에 제출하면 분명 런타임에러가 뜰것이다..

이 문제의 함정은 N이 1,000,000까지 있으며, Math.max를 사용하면 오버플로우가 난다는 것이다.

결국엔 배열에서 max,min 을 리턴하는 함수를 따로 만들면 쉽게 해결 할 수 있다.

 

'use strict';

const readline = require('readline');
const rl = readline.createInterface({
	input: process.stdin,
	output: process.stdout,
});

const findMax = (array) => array.reduce((a, b) => Math.max(a, b));
const findMin = (array) => array.reduce((a, b) => Math.min(a, b));

const solution = function (input) {
	const n = parseInt(input.shift());
	const nums = input
		.shift()
		.split(' ')
		.map((e) => parseInt(e));
	console.log(findMin(nums), findMax(nums));
};

const input = [];
rl.on('line', function (line) {
	input.push(line);
}).on('close', function () {
	solution(input);
	process.exit();
});
profile on loading

Loading...