Notice
Recent Posts
Recent Comments
Link
ยซ   2024/12   ยป
์ผ ์›” ํ™” ์ˆ˜ ๋ชฉ ๊ธˆ ํ† 
1 2 3 4 5 6 7
8 9 10 11 12 13 14
15 16 17 18 19 20 21
22 23 24 25 26 27 28
29 30 31
Archives
Today
Total
๊ด€๋ฆฌ ๋ฉ”๋‰ด

Lennon FE

[ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค Lv.2] ์ฃผ์ฐจ ์š”๊ธˆ ๊ณ„์‚ฐ(js) ๋ณธ๋ฌธ

๐Ÿ”ฅ Algorithm/Programmers

[ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค Lv.2] ์ฃผ์ฐจ ์š”๊ธˆ ๊ณ„์‚ฐ(js)

Lennon 2022. 1. 22. 19:52
728x90
๋ฐ˜์‘ํ˜•

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

 

์ฝ”๋”ฉํ…Œ์ŠคํŠธ ์—ฐ์Šต - ์ฃผ์ฐจ ์š”๊ธˆ ๊ณ„์‚ฐ

[180, 5000, 10, 600] ["05:34 5961 IN", "06:00 0000 IN", "06:34 0000 OUT", "07:59 5961 OUT", "07:59 0148 IN", "18:59 0000 IN", "19:09 0148 OUT", "22:59 5961 IN", "23:00 5961 OUT"] [14600, 34400, 5000]

programmers.co.kr

 

const timeToMin = (time) => {
    let words = time.split(':');
    return Number(words[0]) * 60 + Number(words[1]);
}

function solution(fees, records) {
    // ๋ฐฐ์—ด๋กœ ์ˆ˜์ • ๋ฐ ์ฐจ๋Ÿ‰ ๋ฒˆํ˜ธ๋กœ ์ •๋ ฌ
    records = records.map(v => v.split(' ')).sort((a,b) => a[1]-b[1]);
    // ์œ ์ผํ•œ ์ฐจ๋Ÿ‰๋ฒˆํ˜ธ ๋ฐฐ์—ด ์ƒ์„ฑ
    const uniqueCars = [...new Set(records.map(v => v[1]))];
    
    // ์ฐจ๋Ÿ‰๋ฒˆํ˜ธ๋ณ„ ์ž…์ถœ์ฐจ ๋ฐฐ์—ด ์ƒ์„ฑ (์ž…์ถœ์ฐจ ์ˆ˜๊ฐ€ ํ™€์ˆ˜๋ฉด ๋’ค์— 23:59 push)
    const result = uniqueCars.map((v) => {
        const arr = [];
        for(let i = 0; i < records.length; i++){
            if(v === records[i][1]) {
                arr.push(records[i]);
            }
        }
        if(arr.length % 2 === 1) {
            return [...arr, ['23:59', v, 'OUT']]
        } else {
            return arr;
        }
    })
    // [์ด ์‹œ๊ฐ„(minute)] answer๋ฐฐ์—ด ์ดˆ๊ธฐํ™”
    const answer = result.map(v => {
        let minute = 0;
        for(let i = 0; i < v.length; i+=2){
            const time = timeToMin(v[i+1][0])-timeToMin(v[i][0]); 
            minute += time;
        }
        
        return minute;
    })
    // ์ฐจ๋Ÿ‰ ๋ฒˆํ˜ธ๋ณ„ ์ •๋ ฌ ํ›„ ๊ธˆ์•ก ๊ณ„์‚ฐ ํ›„ return
    return answer.map(v => {
        let price = 0;
        if(v > fees[0]) {
            return fees[1] + Math.ceil((v-fees[0]) / fees[2]) * fees[3]
        } else {
            return fees[1];
        }
    })
}

์ •๋ง ๋‹ค์–‘ํ•˜๊ฒŒ ํ‘ธ๋Š” ์‚ฌ๋žŒ๋“ค์ด ๋งŽ์€ ๋ฌธ์ œ์˜€๋‹ค.

์‚ฌ์‹ค ์ ‘๊ทผ๋ฒ• ์ž์ฒด๊ฐ€ ๋‚ด๊ฐ€ ํ‹€๋ฆฐ ๊ฒŒ ๋งž์„ ์ˆ˜๋„ ์žˆ๋‹ค. ์ฃผ์–ด์ง„ ๋ฐฐ์—ด์— in, out ์š”์†Œ๋ฅผ ์‚ฌ์šฉ ์•ˆ ํ•˜๊ณ  ํ’€์—ˆ๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค.

์‹ค์ œ ๊ณต์ฑ„๋•Œ๋„ ํ’€์—ˆ๋˜ ๋ฌธ์ œ์˜€๋Š”๋ฐ ์ง€๊ธˆ์ด๋ž‘ ๋น„์Šทํ•˜๊ฒŒ ํ’€์—ˆ๋˜ ๊ฒƒ ๊ฐ™๋‹ค.

 

๋‚ด ์•Œ๊ณ ๋ฆฌ์ฆ˜

1. ์ฃผ์–ด์ง„ records ๋ฐฐ์—ด์„ ์ฐจ๋Ÿ‰ ๋ฒˆํ˜ธ๊ฐ€ ์ž‘์€ ์ˆœ์„œ๋กœ ์ •๋ ฌํ•œ๋‹ค. (๊ฒฐ๊ณผ ๊ฐ’์ด ์ฐจ๋Ÿ‰๋ฒˆํ˜ธ๋ณ„๋กœ ์ž‘์€ ์ˆœ์„œ๋Œ€๋กœ ๊ธˆ์•ก์ด ๋ฐ˜ํ™˜๋˜์–ด์•ผ ํ•จ)

 

2. ์ฐจ๋Ÿ‰์˜ ์ˆ˜๋ฅผ ์„ผ๋‹ค. => ์ฐจ๋Ÿ‰ ๋ณ„๋กœ ์ž…์ถœ์ฐจ๋ฅผ ๊ธฐ๋กํ•œ๋‹ค. => ๊ฐœ์ˆ˜๊ฐ€ ํ™€์ˆ˜๋ฉด ์•„์ง ์•ˆ ๋‚˜๊ฐ„๊ฑฐ๋‹ˆ(in=>out=>in)

['23:59', '์ฐจ๋Ÿ‰๋ฒˆํ˜ธ', 'out'] ๋ฅผ ๋ฐฐ์—ด์— pushํ•˜๊ณ  returnํ•œ๋‹ค.

 

=> ๊ทธ๋Ÿผ ๋ชจ๋“  ์ฐจ๋Ÿ‰์— ๋Œ€ํ•ด ์ž…์ฐจ, ์ถœ์ฐจ ๊ธฐ๋ก ์ˆœ์„œ๋Œ€๋กœ ์ €์žฅ์ด ๋˜์–ด์žˆ๋Š” ๋ฐฐ์—ด์ด ์ƒ์„ฑ๋œ๋‹ค.

 

3. timeToMin ํ•จ์ˆ˜๋ฅผ ์ด์šฉํ•ด ์ž…,์ถœ์ฐจ๋ฅผ ์‹œ๊ฐ„ ์ฐจ๋ฅผ ๋๊นŒ์ง€ ๋ฐ˜๋ณตํ•ด ๊ตฌํ•ด์ฃผ๊ณ  answer ๋ฐฐ์—ด์— ์ €์žฅํ•ด์ค€๋‹ค.

 

4. answer ๋ฐฐ์—ด์—์„œ ๋ฌธ์ œ์—์„œ ์ฃผ์–ด์ง„๋Œ€๋กœ ๊ณ„์‚ฐํ•˜๊ณ  ๊ฒฐ๊ณผ ๊ธˆ์•ก์„ ๋ฐฐ์—ด ํ˜•์‹์œผ๋กœ returnํ•œ๋‹ค.

 

 

 

 

728x90
๋ฐ˜์‘ํ˜•
Comments