Algorithm/basic

[백준/JS/10818] 최소, 최대

서린허 2025. 1. 12. 17:03

문제

N개의 정수가 주어진다. 이때, 최솟값과 최댓값을 구하는 프로그램을 작성하시오.

입력

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

출력

첫째 줄에 주어진 정수 N개의 최솟값과 최댓값을 공백으로 구분해 출력한다.

 


 

Do you mean Switch statements are not suitable logical comparisons?

=> Yes, that's correct.
Switch statements in JavaScript are not suitable for logical comparisons like `<`,`>`, or `<=`.
They are designed to match strict values, not conditions. 

 

이후에는 Math를 사용하여 최소, 최대를 출력하도록 변경했다.

const fs = require("fs");
const path = process.platform === "linux" ? "/dev/stdin" : "./input.txt";

// Read input and parse it
const input = fs.readFileSync(path).toString().trim().split("\n");
const N = Number(input[0]); // Number of elements (not actually needed)
const data = input[1].split(" ").map(Number); // Parse numbers from the second line

// Use Math.min and Math.max for better efficiency
const min = Math.min(...data);
const max = Math.max(...data);

console.log(min, max);