* [프로그래머스] 삼각 달팽이 - JavaScript

이은빈 EUNBIN·2021년 4월 15일
0
post-thumbnail

📌 문제

https://programmers.co.kr/learn/courses/30/lessons/68645



📌 풀이

function solution(n) {
    const answer = new Array(n).fill().map((_, i) => new Array(i + 1)); //,,,,,,,,,
    
    let count = 0;
    let x = -1;
    let y = 0;
    while (n > 0) {
        for (let i = 0; i < n; i++) answer[++x][y] = ++count; //first
        for (let i = 0; i < n - 1; i++) answer[x][++y] = ++count; //second
        for (let i = 0; i < n - 2; i++) answer[--x][--y] = ++count; //third
        n -= 3;
    }
    
    return answer.flatMap(e => e);
}

// first: 1,2,,3,,,4,,,
// second: 1,2,,3,,,4,5,6,7
// third: 1,2,9,3,,8,4,5,6,7
// first: 1,2,9,3,10,8,4,5,6,7
// second: 1,2,9,3,10,8,4,5,6,7
// third: 1,2,9,3,10,8,4,5,6,7

JavaScript flatMap


어렵다 😵

profile
Frontend Engineer & Value Creator

0개의 댓글