๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ

๐Ÿ“ ์•Œ๊ณ ๋ฆฌ์ฆ˜/Programmers

[Javascript/PGS] Lv.2 : ์ฃผ์ฐจ ์š”๊ธˆ ๊ณ„์‚ฐ(2022 KAKAO BLIND RECRUITMENT)

728x90
๋ฐ˜์‘ํ˜•

 

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

 

ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค

SW๊ฐœ๋ฐœ์ž๋ฅผ ์œ„ํ•œ ํ‰๊ฐ€, ๊ต์œก, ์ฑ„์šฉ๊นŒ์ง€ Total Solution์„ ์ œ๊ณตํ•˜๋Š” ๊ฐœ๋ฐœ์ž ์„ฑ์žฅ์„ ์œ„ํ•œ ๋ฒ ์ด์Šค์บ ํ”„

programmers.co.kr

 


ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค ๋ ˆ๋ฒจ2 ๋ฌธ์ œ.

2022 ์นด์นด์˜ค ๊ธฐ์ถœ

 

์†Œ์š”์‹œ๊ฐ„ ์•ฝ 40๋ถ„.. ๋ ˆ๋ฒจ 2๋Š” ์ด์ œ 30-40๋ถ„ ์•ˆ์— ๋๋‚ผ ์ˆ˜ ์žˆ๊ฒŒ ๋นก๊ณตํ•ด์•ผ๊ฒ ๋‹ค

js์—์„œ map ์•ˆ๋˜๋ฉด forEach๋กœ ์‹œ๋„ํ•ด๋ณด๊ธฐ ใ… ใ…กใ… 

ํ•ด์‹œ๋งต์„ ์‚ฌ์šฉํ•ด์„œ ํ’€์ดํ•˜์˜€๋‹ค.

 

ํ’€์ด ์ˆœ์„œ

1. ๋ˆ„์  ์‹œ๊ฐ„ ๊ณ„์‚ฐ

2. ์ถœ์ฐจ ์•ˆํ•˜๋ฉด 23:59 ์ถœ์ฐจ๋กœ ๋ณ€๊ฒฝ

3. ์‹œ๊ฐ„ -> ๊ธฐ๋ณธ์š”๊ธˆ + ์ถ”๊ฐ€์š”๊ธˆ ๊ณ„์‚ฐํ•˜๊ธฐ

 

๋‚˜์˜ ํ’€์ด

const timeToMinutes=(timeString)=>{
    let time = timeString.split(":");
    return Number(time[0])*60 + Number(time[1]);
}

function solution(fees, records) {
    var answer = [];
    let carTime = new Map();
    let carFeeMinutes = new Map();
    let carFee = new Map();
    records.map((r)=>{
        let carNumber = r.substr(6,4);
        if(r.substr(11)=='IN'){ // ๋“ค์–ด์˜ฌ ๋•Œ
            carTime.set(carNumber, r.substr(0,5));
        }
        else{
            let inTime = carTime.get(carNumber);
            let nowFee = carFeeMinutes.get(carNumber);
            let allTime = (timeToMinutes(r.substr(0,5)) - timeToMinutes(inTime));
            carFeeMinutes.set(carNumber, nowFee>0 ? nowFee+allTime : allTime)
            carTime.set(carNumber, 0);
        }
    })
    
    carTime.forEach((value, key)=>{
        if(value !== 0){
            console.log(key);
            let allTime = (timeToMinutes("23:59") - timeToMinutes(value));
            let nowFee = carFeeMinutes.get(key);
            carFeeMinutes.set(key, nowFee>0 ? nowFee+allTime : allTime);
            carTime.set(key, 0);
        }
    })
    
    carFeeMinutes.forEach((value, key)=>{
        // fees[0]: ๊ธฐ๋ณธ์‹œ๊ฐ„, [1]: ๊ธฐ๋ณธ์š”๊ธˆ, [2]: ๋‹จ์œ„์‹œ๊ฐ„, [3]: ๋‹จ์œ„์š”๊ธˆ
        let overtime = value-fees[0];
        if(overtime <= 0){
            carFee.set(key, fees[1]);
        }
        else{
            let addFee = Math.ceil(overtime/fees[2]);
            carFee.set(key, fees[1] + addFee*fees[3]);
        }
    })
    console.log(carFee);
    let sortedCarFee = [...carFee].sort((a, b) => a[0].localeCompare(b[0]));
    sortedCarFee.forEach((fee)=>{
        answer.push(fee[1]);
    })
    return answer;
}

728x90
๋ฐ˜์‘ํ˜•