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

ํ”„์—” ๊ณต๋ถ€/๐Ÿซง ์•Œ๊ณ ๋ฆฌ์ฆ˜ ๊ณต๋ถ€

[์•Œ๊ณ ๋ฆฌ์ฆ˜ || ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค] ๋Œ€์ถฉ ๋งŒ๋“  ์žํŒ

728x90

์Šคํ„ฐ๋””๊ฐ€ 8์‹œ๋ผ ์ผ์–ด๋‚˜์ž๋งˆ์ž ์•Œ๊ณ ๋ฆฌ์ฆ˜ ๋ฌธ์ œ ํ‘ธ๋‹ˆ๊นŒ ์Šฌ์Šฌ ๋จธ๋ฆฌ๊ฐ€ ์•ˆ๋Œ์•„๊ฐ„๋‹ค.

 

<for๋ฌธ ์‚ฌ์šฉ>

function solution(keymap, targets) {
    const count = {};
    for (const key of keymap) {
        [...key].forEach((char, i) => {
            if (!count[char]) {
                count[char] = i + 1;
            } else {
                if (count[char] > i) {
                    count[char] = i + 1;
                }
            }
        })
    }
    const result = targets.map(target => {
        let sum = 0;
        for (let i = 0; i < target.length; i++) {
            if (!count[target[i]]) {
                sum = -1;
                break;
            } else {
                sum += count[target[i]];
            }
        }
        return sum;
    })
    return result;
}

 

<reduce ์‚ฌ์šฉ>

function solution(keymap, targets) {
    const count = {};
    for (const key of keymap) {
        [...key].forEach((char, i) => {
            if (!count[char]) {
                count[char] = i + 1;
            } else {
                if (count[char] > i) {
                    count[char] = i + 1;
                }
            }
        })
    }
    const result = [];
    targets.forEach(target => {
        const arr = [...target];
        result.push(arr.reduce((acc, cur) => {
            if (count[cur]) {
                return acc + count[cur];
            } else {
                result.push(-1);
                arr.splice(1);
            }
        }, 0))
    })
    return result.filter(v => v !== undefined)
}

 

๋จผ์ € obj ๋กœ ๊ฐ ๋ฐฐ์—ด์—์„œ ์ž‘์€ ์ˆ˜๋ฅผ ๋„ฃ์—ˆ๋‹ค.

์—ฌ๊ธฐ์„œ๋„ ๋ญ”๊ฐ€ ๊ผฌ์˜€๋Š”๋ฐ index ๋ฅผ value ๊ฐ’์œผ๋กœ ๋„ฃ์€ ํ›„, 

๋งŒ์•ฝ key ๋กœ ์ฐพ์€ value ๊ฐ’์ด ์—†๋‹ค๋ฉด ์ƒˆ๋กœ ๋„ฃ์–ด์ฃผ๋Š” ๋ฐฉ์‹์œผ๋กœ ์ฝ”๋“œ๋ฅผ ์งฐ๋‹ค.

๊ทผ๋ฐ ์ž๊พธ value ๊ฐ€ ์žˆ๋Š”๋ฐ๋„ value ๊ฐ’์ด ๊ฐฑ์‹ ๋˜๋Š” ๊ฒƒ ์•„๋‹Œ๊ฐ€!

console.log ๋กœ ์ฐ์–ด์„œ ํ•˜๋‚˜ํ•˜๋‚˜ ๋ณ€ํ™”ํ•˜๋Š” ๊ณผ์ •์„ ๋ณด๋‹ˆ, 

value ๊ฐ’์— 0 ์ด ๋“ค์–ด์žˆ๋Š” ๊ฒƒ๋„ !value ๋ผ๊ณ  ์ฒ˜๋ฆฌ๋˜๊ธฐ ๋•Œ๋ฌธ์ด์˜€๋‹ค.

์•„ ์ด์ ์„ ๋†“์น˜๋‹ค๋‹ˆ...!

 

 

 

๊ทธ๋ž˜์„œ i+1 ๊ฐ’์œผ๋กœ ๊ณ ์ณค๋‹ค.

 

๋ฌธ์ œ๋Š” ์•„๋ž˜ ๋กœ์ง ์งœ๋‹ค๊ฐ€ ๋ฉํ•ด์ ธ์„œ ์‹œ๊ฐ„์ด ๋„ˆ๋ฌด ์˜ค๋ž˜๊ฑธ๋ ธ๋‹ค.

๊ณ ์ฐจํ•จ์ˆ˜๋ฅผ ๋‘๋ฒˆ ์จ๋†“๊ณ  return ํ•˜๋ฉด ๋‹น์—ฐํžˆ ์ „์ฒด ๋กœ์ง์—์„œ ๋ชป๋น ์ ธ์˜ค์ง€ ๋ฐ”๋ณด์•ผ......

reduece ์“ฐ๋‹ค๊ฐ€ ์—ฅ์—ฅ?? ์™ธ์•Š๋˜ ------------ ๋‡Œ์ •์ง€ ^_^

 

๋‹คํ–‰ํžˆ for ๋ฌธ์œผ๋กœ ๊ณ ์ณ์„œ break ๋ฅผ ์‚ฌ์šฉํ–ˆ๋‹ค.

reduece๋„ ์ ์šฉ๋˜๋˜ arr ๋ฅผ ๋ณ€ํ˜•์‹œํ‚ค๋ฉด break ๋˜๊ธฐ ๋•Œ๋ฌธ์— splice ๋ฅผ ์‚ฌ์šฉํ•œ ๋ฐฉ์‹์œผ๋กœ ๋น ์ ธ๋‚˜์˜ฌ ์ˆ˜ ์žˆ๋‹ค. 

๋Œ€์‹  undefined ๊ฐ€ ๋ฐฐ์—ด์— ๋“ค์–ด๊ฐ€๊ธฐ ๋•Œ๋ฌธ์—, undefined ๋ฅผ ๊ฑธ๋Ÿฌ์ค€ ๊ฒฐ๊ณผ ๋ฐฐ์—ด์„ return ํ•ด์ค˜์•ผํ•œ๋‹ค.

 

 

 

 

์š”์ฆ˜ ๋‹ค๋ฅธ ์‚ฌ๋žŒ์˜ ํ’€์ด๋ฅผ ์•ˆ๋ดค๋”๋‹ˆ ๋ฐœ์ „์ด ์—†๋Š” ๊ฒƒ ๊ฐ™๋‹ค.

 

function solution(keymap, targets) {
    const answer = [];
    const map = {}
    for (const items of keymap) {
        items.split('').map((item, index) => map[item] = (map[item] < index+1 ? map[item] : index+1))
    }
    for (const items of targets) {
        answer.push(items.split('').reduce((cur, item) => cur += map[item], 0) || -1)
    }
    return answer;
}

....

์ด๋ ‡๊ฒŒ ๊ฐ„๋‹จํžˆ ํ•  ์ˆ˜ ์žˆ๋Š”๋ฐ ์‹œ๋ฌด๋ฃฉํ•ด์ง....

 

๋ชจ๋”ฅ๋‹ค๋ฅผ ๋‹ค์‹œ ๋ณผ ๋•Œ๊ฐ€ ๋œ ๊ฒƒ ๊ฐ™๋‹ค....

728x90
๋ฐ˜์‘ํ˜•