[프로그래머스 Lv.2] 2022 KAKAO BLIND RECRUITMENT - 주차 요금 계산

김민지·2023년 9월 20일
0

✨ 문제 ✨



✨ 정답 ✨

function solution(fees, records) {
    var answer = [];
    let carArray=[]
    let basic=fees[0];
    let basicFee=fees[1];
    let add=fees[2];
    let addFee=fees[3];
    for (let i=0;i<records.length;i++){
        let cut=records[i].split(' ');
        carArray.push(cut[1]);
    }
    carArray=carArray.filter((a,b)=>carArray.indexOf(a)===b)
    carArray.sort();
    for (let i=0;i<carArray.length;i++){
        let listArray=[];
        for(let j=0;j<records.length;j++){
            if (records[j].split(' ')[1]===carArray[i]){
                console.log(records[j].split(' ')[1], records[j])
                listArray.push(records[j].split(' ')[0])
            }
        }
        let totalFee=0;
        let useTime=0;
        for (let i=0;i<Math.ceil(listArray.length/2);i++){
            let carIn=listArray[2*i]
            let carOut=listArray[2*i+1]
            let carInTime=(+carIn.split(':')[0])*60+(+carIn.split(':')[1]);
            let carOutTime=0;
            if (carOut!==undefined){
                carOutTime=(+carOut.split(':')[0])*60+(+carOut.split(':')[1]);
            }else{
                carOutTime=23*60+59
            }
            useTime+=(carOutTime-carInTime)
        }
        if (useTime<basic){
                totalFee+=basicFee;
            }else{
                totalFee+=basicFee;
                useTime-=basic;
                totalFee+=Math.ceil(useTime/add)*addFee
            }
           answer.push(totalFee)     
    }

    return answer;
}

🧵 참고한 정답지 🧵

💡💡 기억해야 할 점 💡💡

문제를 정확하게 읽자...

profile
이건 대체 어떻게 만든 거지?

0개의 댓글

관련 채용 정보