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

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

[์•Œ๊ณ ๋ฆฌ์ฆ˜ || ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค] ์ˆซ์ž ์ง๊ฟ

728x90

 

 

๋Œ€๋žต์ ์ธ ํ’€์ด๋Š” 30๋ถ„ ์ •๋„ ๋งŒ์— ํ–ˆ๋Š”๋ฐ, ๊ณ„์† ์‹œ๊ฐ„ ์ดˆ๊ณผ๊ฐ€ ๋–ด๋‹ค.

11-15๋Š” BigInt ๊ฐ€ ๋ถ„๋ช…ํ•ด!

 

<ํ†ต๊ณผ ๋ชปํ•œ ์ฝ”๋“œ 1>

function solution(X, Y) {
    const Xcount = new Array(10).fill(0);
    const Ycount = new Array(10).fill(0);

    X.split("").map(v => Xcount[+v]++);
    Y.split("").map(v => Ycount[+v]++);

    const resultCount = [];
    for (let i = 0; i < 10; i++) {
        resultCount[i] = Xcount[i] < Ycount[i] ? Array.from({ length: Xcount[i] }, () => i) : Array.from({ length: Ycount[i] }, () => i);
    }

    const result = resultCount.flat()
    if (!result.length) {
        return "-1"
    } else {
        return String(BigInt(result.sort((a, b) => b - a).join("")));
    }
}

Array.from ์„ ์ด์šฉํ•ด ์ž๋ฆฌ์ˆ˜๊ฐ€ ๊ณตํ†ต๋œ ๊ฐœ์ˆ˜๋งŒํผ ๋“ค์–ด์žˆ๋Š” array ๋ฅผ ํ•ด๋‹น index ์— ๋„ฃ์–ด์ฃผ๋Š” ๋ฐฉ์‹์œผ๋กœ ์š”๋ฆฌ์กฐ๋ฆฌ ํ•ด๋ดค๋Š”๋ฐ,

์‹œ๊ฐ„ ์˜ค๋ฅ˜๊ฐ€ ๋‚˜๋Š” ๊ฒƒ์€ ๋™์ผํ–ˆ๋‹ค.

[[0], [1, 1], [0]......[0]] <- ์ด๋Ÿฐ ๋ฐฉ์‹์œผ๋กœ ๋งŒ๋“  ํ›„, 

result ๋กœ 1์ฐจ์› ๋ฐฐ์—ด๋กœ ๋งŒ๋“ค์–ด์ค€ ๋’ค [0, 1, 1, 0, 0, ...0]

์ˆ˜๋“ค์„ ์˜ค๋ฆ„์ฐจ์ˆœ์œผ๋กœ ์ •๋ ฌ, Join ์œผ๋กœ ํ•ฉ์นœ ํ›„ Bigint ๋กœ ๋งŒ๋“ค๊ณ  (์œ—์ž๋ฆฌ์˜ 0์„ ์ œ๊ฑฐํ•˜๊ธฐ ์œ„ํ•จ & ๋ฒ”์œ„๊ฐ€ bigint ํ•ด๋‹น)

๊ทธ ๋‹ค์Œ์— ๋‹ค์‹œ String ์œผ๋กœ ๋งŒ๋“ค์–ด์„œ return ํ•˜๋Š” ๋ฐฉ์‹์ด๋‹ค.

 

๋‹ค๋ฅธ๊ฒƒ๋ณด๋‹ค ์‹œ๊ฐ„ ๋ณต์žก๋„๋ฅผ ๋งŽ์ด ์žก์•„๋จน๋Š” ๊ณณ์„ ์ฐพ์•„์•ผํ–ˆ๋‹ค.

 

 

<ํ†ต๊ณผ ๋ชปํ•œ ์ฝ”๋“œ 2>

function solution(X, Y) {
    const Xcount = new Array(10).fill(0);
    const Ycount = new Array(10).fill(0);

    X.split("").map(v => Xcount[+v]++);
    Y.split("").map(v => Ycount[+v]++);

    const resultCount = [];
    for (let i = 0; i < 10; i++) {
        if (!Xcount[i] == 0 && !Ycount[i] == 0) {
            const minCount = Math.min(Xcount[i], Ycount[i]);
            let string = '';
            for (let j = 0; j < minCount; j++) {
                string += `${i}`
            }
            resultCount[i] = string;
        }
    }
    const result = resultCount.reverse().map(BigInt).join("");
    return result.length > 0 ? result : "-1";
}

๋ฐฐ์—ด๋กœ ํ•˜๋ฉด ์–ด์จŒ๋“  ์‹œ๊ฐ„ ๋ณต์žก๋„๊ฐ€ ํฌ๊ฒŒ ๋‚˜์˜ฌ๊ฑฐ๋ž€ ์ƒ๊ฐ์ด ๋“ค์—ˆ๊ณ , 

์ด ๋ถ€๋ถ„์„ ๋ณด๊ณ  ํžŒํŠธ๋ฅผ ์–ป์—ˆ๋‹ค.

๋ถ„๋ช… string ์œผ๋กœ ์ถœ๋ ฅํ•˜๋Š”๊ฒŒ ๋” ๊ฐ€๋ณ๊ฒ ์ง€!

์ด์ „ ์ฝ”๋“œ์—์„œ [5, 5] ๊ฐ™์€ ๋ฐฐ์—ด์„ ๋„ฃ์–ด์ฃผ๋Š” ๋Œ€์‹ , ๋ฌธ์ž์—ด๋กœ ๋งŒ๋“ค์–ด์ฃผ๊ธฐ๋กœ ํ–ˆ๋‹ค.

for ๋ฌธ์„ ์ด์šฉํ•ด string์— '5', '5' ๋ฅผ ๋ถ™์—ฌ์„œ ๊ทธ ๊ฐ’์„ ๋„ฃ์–ด์คฌ๋Š”๋ฐ, ์ด๊ฒƒ๋„ ์‹œ๊ฐ„์ดˆ๊ณผ๊ฐ€ ๋–ด๋‹ค.

๋‹น์—ฐํ•˜๋‹ค. ์‹œ๊ฐ„์„ ์ค„์—ฌ์•ผ ํ•˜๋Š”๋ฐ for ๋ฌธ์„ ์ค‘์ฒฉ์‹œ์ผฐ์œผ๋‹ˆ O(n²) ๊ฐ€ ๋‚˜์˜ฌ๊ฒŒ ๋ถ„๋ช…ํ•จ!!

๊ทธ๋ ‡๋‹ค๋ฉด ์–ด๋–ป๊ฒŒ ํ•ฉ์ณ์•ผ ๋ฐ˜๋ณต๋ฌธ์„ ๋‘๋ฒˆ ์“ฐ์ง€ ์•Š์„๊นŒ? 

 

 

<ํ†ต๊ณผํ•œ ์ฝ”๋“œ>

function solution(X, Y) {
    const Xcount = new Array(10).fill(0);
    const Ycount = new Array(10).fill(0);

    X.split("").map(v => Xcount[+v]++);
    Y.split("").map(v => Ycount[+v]++);

    let str = '';
    for (let i = 9; i >= 0; i--) {
        const minCount = Math.min(Xcount[i], Ycount[i]);
        str += Array.from({ length: minCount }, () => i).join("");
    }

    if (!str.length) {
        return "-1"
    } else if (Number(str) == 0) {
        return '0';
    } else {
        return str
    }
}

array๋กœ ๋งŒ๋“ค์ง€ ๋ง๊ณ  ๋ฐ”๋กœ string ์œผ๋กœ ๋งŒ๋“ค์ž!

๊ทธ๋ฆฌ๊ณ  for ๋ฌธ์˜ ์ˆœ์„œ๋ฅผ ๋ฐ”๊ฟจ๋‹ค.

๋ฐฐ์—ด์€ sort ๋ฅผ ํ•  ์ˆ˜ ์žˆ์ง€๋งŒ, string์€ ๊ทธ๋Ÿฐ ๊ณ ์ฐจํ•จ์ˆ˜๊ฐ€ ์—†๊ธฐ ๋•Œ๋ฌธ์— (๊ทธ๋ฆฌ๊ณ  ํ•œ ๋ฉ์–ด๋ฆฌ๋ผ ์•ˆ๋จ)

ํฐ ์ˆ˜ ๋จผ์ € string ์— ๋ถ™์—ฌ์คฌ๋‹ค.

์ด์ „์—๋Š” 0, 1, 1, 4, 4 ์ˆœ์„œ๋กœ ๋ฐฐ์—ด์— ๋“ค์–ด๊ฐ”๋‹ค๋ฉด, 

์ง€๊ธˆ์€ 4, 4, 1, 1, 0, 0 ์ˆœ์œผ๋กœ ๋ถ™๋Š”๋‹ค. 

 

์™„์„ฑ๋œ str์˜ length ๊ฐ€ 0 ์ด๋ผ๋ฉด => ์ผ์น˜ํ•˜๋Š” ์ง๊ถ ์ˆซ์ž๊ฐ€ ํ•˜๋‚˜๋„ ์—†๋Š” ๊ฒƒ. "-1"์„ ๋ฆฌํ„ด

str์„ ์ˆซ์ž๋กœ ๋ฐ”๊ฟจ์„ ๋•Œ 0์ด ๋‚˜์˜จ๋‹ค๋ฉด? => "000000" "00" ์ด๋Ÿฐ ํ˜•ํƒœ์ด๊ธฐ ๋•Œ๋ฌธ์— 0์ด๋‹ค. "0" ์„ ๋ฆฌํ„ด

๋‘๊ฐ€์ง€ ๊ฒฝ์šฐ๊ฐ€ ์•„๋‹ˆ๋ผ๋ฉด "124" "2450" ๋“ฑ ์ฒ˜๋Ÿผ ์œ ํšจํ•œ ์ž๋ฆฌ๊ฐ’์„ ๊ฐ€์ง€๋Š” ์ˆซ์ž์ž„. str ์„ ๋ฐ”๋กœ return ํ•ด์ค€๋‹ค.

 

์‹œ๊ฐ„์€ ์ข€ ๊ฑธ๋ ธ์ง€๋งŒ ์Šค์Šค๋กœ ํ’€์–ด๋‚ด์„œ ๋‚˜ ์ž์‹  ์นญ์ฐฌํ•ด!

 

 

 

<๋‹ค๋ฅธ ์‚ฌ๋žŒ์˜ ์ฝ”๋“œ> char ๋ฅผ ์ด์šฉ

function solution(X, Y) {
    let result = '';
    const numObj = {};

    for (const char of X) {
        numObj[char] = (numObj[char] || 0) + 1;
    }

    for (const char of Y) {
        if (!numObj[char]) continue;
        result += char;
        numObj[char]--;
    }

    if (result === '') return '-1';
    if (+result === 0) return '0';
    return [...result]
        .map((num) => +num)
        .sort((a, b) => b - a)
        .join('');    
}
728x90
๋ฐ˜์‘ํ˜•