반응형
문제
https://www.acmicpc.net/problem/2798
풀이
#1
const fs = require("fs");
const filePath = process.platform === "linux" ? "/dev/stdin" : "./input.txt";
let [nums, arr] = fs.readFileSync(filePath).toString().trim().split("\n");
const [N, M] = nums.split(" ").map(Number);
const cards = arr.split(" ").map(Number);
let minVal = Number.MAX_SAFE_INTEGER;
let diff = Number.MAX_SAFE_INTEGER;
let answer = 0;
for (let i = 0; i < N; i++) {
for (let j = 0; j < N; j++) {
if (i === j) continue;
for (let k = 0; k < N; k++) {
if (k === j || k === i) continue;
const sum = cards[i] + cards[j] + cards[k];
diff = M - sum;
if (diff < 0) continue;
if (diff < minVal) {
minVal = diff;
answer = sum;
}
}
}
}
console.log(answer);
#2
const [N, M] = nums.split(" ").map(Number);
const cards = arr.split(" ").map(Number);
let answer = Number.MIN_SAFE_INTEGER;
for (let i = 0; i < N; i++) {
for (let j = i + 1; j < N; j++) {
for (let k = j + 1; k < N; k++) {
const sum = cards[i] + cards[j] + cards[k];
if (sum > M) continue;
answer = Math.max(answer, sum);
}
}
}
console.log(answer);
반응형
'Algorithm > BOJ' 카테고리의 다른 글
[알고리즘/백준/15721] 번데기 (4) | 2023.06.01 |
---|---|
[알고리즘/백준/19532] 수학은 비대면 강의입니다. (0) | 2023.05.31 |
[알고리즘/백준/17298] 오큰수 (0) | 2023.05.26 |
[알고리즘/백준/1158] 요세푸스 문제 (0) | 2023.05.26 |
[알고리즘/백준/18258] 큐 2 (0) | 2023.05.25 |