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

 

1049번: 기타줄

첫째 줄에 N과 M이 주어진다. N은 100보다 작거나 같은 자연수이고, M은 50보다 작거나 같은 자연수이다. 둘째 줄부터 M개의 줄에는 각 브랜드의 패키지 가격과 낱개의 가격이 공백으로 구분하여 주

www.acmicpc.net

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

const [N, M] = input.shift().split(" ").map( i => Number(i));
const packages = [];
const singles = [];
let cost = 0;

input.forEach((i) => {
    const [p, s] = i.split(" ").map( m => Number(m));
    packages.push(p);
    singles.push(s);
});

let packagePay = Math.floor(N/6);
// packages 가격 중 최솟값이 singles 최솟값 * 6 보다 큰 경우 낱개로 사는게 이득
cost += Math.min(...packages) > Math.min(...singles) * 6 ? Math.min(...singles) * packagePay * 6 : Math.min(...packages) * packagePay;

// 낱개 최솟값 * 남은 구매 수 보다 패키지 최솟값으로 구매하는 것이 더 싼 경우 패키지 구매가 이득
cost += Math.min(...singles) * (N - (packagePay) * 6) < Math.min(...packages) ? Math.min(...singles) * (N - (packagePay) * 6) : Math.min(...packages)
console.log(cost);
728x90

https://school.programmers.co.kr/learn/courses/30/lessons/42862?language=javascript 

 

프로그래머스

코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.

programmers.co.kr

function solution(n, lost, reserve) {
    var answer = 0;
    
    lost = lost.map((l) => {
       if(reserve.includes(l)) {
           reserve = new Set(reserve);
           reserve.delete(l);
           reserve = Array.from(reserve);
       } else return l;
    });
    
    let borrow = new Array(n).fill().map(n => 0);
    borrow = borrow.map((n, index) => {
        if(reserve.includes(index+1)) {
            return 1;
        } else return 0;
    });
    
    let student = new Array(n).fill().map(n => true);
    student = student.map((n, index) => {
        if(lost.includes(index+1)) {
            return false;
        } else return true;
    });
    
    student.forEach((s, index) => {
        if(!s) { // 체육복 없는 학생
            let left = index -1 < 0 ? null : index -1;
            let right = index +1 >= student.length ? null : index +1;
            
            if(left !== null && borrow[left] === 1) {
                student[index] = true;
                borrow[left] = 0;
            } else if(borrow[right] !== null && borrow[right] === 1) {
                student[index] = true;
                borrow[right] = 0;            
            }
        }
    })

    return student.filter(s => s === true).length;
}
728x90

+ Recent posts