ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • [Programmers] JavaScript 알고리즘 | Lv.0 특별한 이차원 배열 1
    ► JS Algorithm/Programmers 2024. 1. 8. 20:17
    반응형

    🔒 문제 설명

    정수 n이 매개변수로 주어질 때, 다음과 같은 n × n 크기의 이차원 배열 arr를 return 하는 solution 함수를 작성해 주세요.

    • arr[i][j] (0 ≤ i, j < n) 값은 i = j라면 1, 아니라면 0입니다.

    🔒 제한사항  

    • 1 ≤ n ≤ 100

    🔒 입출력 예

    n result
    3 [[1, 0, 0], [0, 1, 0], [0, 0, 1]]
    6 [[1, 0, 0, 0, 0, 0], [0, 1, 0, 0, 0, 0], [0, 0, 1, 0, 0, 0], [0, 0, 0, 1, 0, 0], [0, 0, 0, 0, 1, 0], [0, 0, 0, 0, 0, 1]]
    1 [[1]]
     

    🔒 입출력 예 설명

    입출력 예 #1

    • 예제 1번의 n의 값은 3으로 다음과 같이 2차원 배열을 채울 수 있습니다.
    i \ j 0 1 2
    0 1 0 0
    1 0 1 0
    2 0 0 1

    따라서 [[1, 0, 0], [0, 1, 0], [0, 0, 1]]을 return 합니다.

     

    입출력 예 #2

    • 예제 2번의 n의 값은 6으로 다음과 같이 2차원 배열을 채울 수 있습니다.
    i \ j 0 1 2 3 4 5
    0 1 0 0 0 0 0
    1 0 1 0 0 0 0
    2 0 0 1 0 0 0
    3 0 0 0 1 0 0
    4 0 0 0 0 1 0
    5 0 0 0 0 0 1

    따라서 [[1, 0, 0, 0, 0, 0], [0, 1, 0, 0, 0, 0], [0, 0, 1, 0, 0, 0], [0, 0, 0, 1, 0, 0], [0, 0, 0, 0, 1, 0], [0, 0, 0, 0, 0, 1]]을 return 합니다.

     

    입출력 예 #3

    • 예제 1번의 n의 값은 1이고 다음과 같이 2차원 배열을 채울 수 있습니다.
    i \ j 0
    0 1

    따라서 [[1]] return 합니다.

     


     

    🔐 solution of mine

    for문

    new Array()Array.fill()Array.push()
      const solution = (n, arr = []) => {
        for (let i = 0; i < n; i++) {
          let innerArr = new Array(n).fill(0);
          innerArr[i] = 1;
          arr.push(innerArr);
        }
        return console.log(arr);
      };
    
      solution(3); // expected output: [[1, 0, 0], [0, 1, 0], [0, 0, 1]]
      solution(6); // expected output: [[1, 0, 0, 0, 0, 0], [0, 1, 0, 0, 0, 0], [0, 0, 1, 0, 0, 0], [0, 0, 0, 1, 0, 0], [0, 0, 0, 0, 1, 0], [0, 0, 0, 0, 0, 1]]
      solution(1); // expected output: [[1]]

     


     

    🔐 solution of others 

    Array.from()

    Array.fill()

    new Array()

      const solution = (n) => {
        const answer = Array.from(new Array(n), () => new Array(n).fill(0));
        for (let i = 0; i < n; i++) answer[i][i] = 1;
    
        return console.log(answer);
      };
    
      solution(3); // expected output: [[1, 0, 0], [0, 1, 0], [0, 0, 1]]
      solution(6); // expected output: [[1, 0, 0, 0, 0, 0], [0, 1, 0, 0, 0, 0], [0, 0, 1, 0, 0, 0], [0, 0, 0, 1, 0, 0], [0, 0, 0, 0, 1, 0], [0, 0, 0, 0, 0, 1]]
      solution(1); // expected output: [[1]]

     

    🔐 solution of others 

    new Array()

    Array.fill()

    Array.map()

     

      const solution = (n) =>
        console.log(
          new Array(n)
            .fill()
            .map((_, i) =>
              new Array(n).fill(0).map((_, j) => (i === j ? 1 : 0))
            )
        );
    
      solution(3); // expected output: [[1, 0, 0], [0, 1, 0], [0, 0, 1]]
      solution(6); // expected output: [[1, 0, 0, 0, 0, 0], [0, 1, 0, 0, 0, 0], [0, 0, 1, 0, 0, 0], [0, 0, 0, 1, 0, 0], [0, 0, 0, 0, 1, 0], [0, 0, 0, 0, 0, 1]]
      solution(1); // expected output: [[1]]
     

     

    반응형
Designed by Tistory.