๐Ÿ’Ž

ARR PS - tree drawing

notion image
ย 
notion image
ย 

ํ’€์–ด๋ณด๊ธฐ

๋งˆ์ง€๋ง‰ ๋ณ„ ๊ฐœ์ˆ˜๋ฅผ ์„ธ๋ณด๋‹ˆ๊นŒ 3์ผ ๋•Œ 5, 5์ผ ๋•Œ 9, 7์ผ ๋•Œ 13์ด๋‹ค.
input * 2 - 1์ด ํ•„์š”ํ•œ ๋‚˜๋ฌด์˜ ๋„ˆ๋น„๋‹ค.
๊ทธ๋ฆฌ๊ณ  ๋งจ์œ„์—์„œ ๋ณ„์ด ํ•œ๊ฐœ, ๊ทธ ๋ฐ‘์œผ๋กœ๋Š” ์–‘์ชฝ์œผ๋กœ 1๊ฐœ์”ฉ๋งŒ ์ถ”๊ฐ€๋œ๋‹ค.
๋ชจ๋“  ์›์†Œ๋ฅผ ๊ณต๋ฐฑ์œผ๋กœ ์ฑ„์šด input * 2 - 1 ๊ธธ์ด์˜ ๋ฐฐ์—ด์„ ๋งŒ๋“ ๋‹ค. >> ๊ฐ€์šด๋ฐ ๋ถ€๋ถ„์€ ๋ณ„๋กœ ๋ฐ”๊พผ๋‹ค.
for๋ฌธ์„ input๋งŒํผ ๋ฐ˜๋ณตํ•˜๋ฉด์„œ ๋‚ด๋ถ€์—์„œ mid ๊ธฐ์ค€ -i, +i ์œ„์น˜์˜ ๊ณต๋ฐฑ๋ฌธ์ž์—ด์„ ๋ณ„๋กœ ๋ฐ”๊ฟ”์ค€๋‹ค.
[ " ", " ", "*", " ", " "]
[ " ", "*", "*", "*", " "]
[ "*", "*", "*", "*", "*"]
ย 
๋ฌธ์ž์—ด์ด ์•„๋‹ˆ๋ผ ์œ„์˜ ๋ฐฐ์—ด๋กœ result ๋ฐฐ์—ด์— ๋ชจ์•„์ฃผ๊ฒŒ ๋˜๋ฉด ์ƒ๊ธฐ๋Š” ๋ฌธ์ œ๊ฐ€ ํ•˜๋‚˜ ์žˆ๋Š”๋ฐ, result ๋ฐฐ์—ด์— arr๋ฅผ for๋ฌธ์˜ ๋ฃจํ”„ ๋งˆ์ง€๋ง‰์— ํ•ญ์ƒ pushํ•ด์ฃผ๋ฉด ๊ฒฐ๊ณผ์ ์œผ๋กœ arr๋ฅผ ์ฐธ์กฐํ˜•์œผ๋กœ ๋„ฃ์–ด์ฃผ๊ธฐ ๋•Œ๋ฌธ์— ๋งˆ์ง€๋ง‰์— result ๋ฐฐ์—ด์„ ํ™•์ธํ•ด๋ณด๋ฉด ๋ชจ๋‘ arr๋ฅผ ๊ฐ€๋ฆฌํ‚ค๋Š” ์›์†Œ์ด๋ฏ€๋กœ ๋ณ„๋กœ ๊ฐ€๋“์ฐฌ ๋ฐฐ์—ด๋งŒ ๋‚˜ํƒ€๋‚œ๋‹ค๋Š” ๊ฒƒ์ด๋‹ค. ๋”ฐ๋ผ์„œ result.push([...arr])์™€ ๊ฐ™์ด ์Šคํ”„๋ ˆ๋“œ์—ฐ์‚ฐ์ž๋กœ ๋‚ด๋ถ€ ๊ฐ’์„ ๊บผ๋‚ด์™€์„œ ๋‹ค์‹œ ๋ฐฐ์—ด๋กœ ๋งŒ๋“ค์–ด ๋„ฃ์„ ์ˆ˜ ์žˆ๋‹ค.
ํ•˜์ง€๋งŒ ์ถœ๋ ฅํ•  ๋•Œ join๋ฉ”์„œ๋“œ๋ฅผ ์‚ฌ์šฉํ•ด ๋ฌธ์ž์—ด๋กœ ๋ฐ”๊พธ์–ด์ค„ ๊ฒƒ์ด๋ฏ€๋กœ ์œ„์™€ ๊ฐ™์ด arr๋ฅผ ๊ณ„์† ๊ฐ€๋ฆฌํ‚ค๋Š” ๋ฌธ์ œ๋Š” ์ƒ๊ธฐ์ง€ ์•Š๋Š”๋‹ค.
notion image
ย 
function answer(height) {
    let result = [];
    let arr = new Array(height * 2 - 1).fill(" ");
    let mid = Math.floor(arr.length / 2);
    arr[Math.floor(arr.length / 2)] = "*";

    for (let i = 0; i < height; i++) {
        arr[mid - i] = "*";
        arr[mid + i] = "*";
        result.push(arr.join(""));
    }

    return result.join("\n");
}

let input = [3, 5, 7];

for (let i = 0; i < input.length; i++) {
    // process.stdout.write(`#${i + 1} `);
    console.log(answer(input[i]));
}