-
[Programmers] JavaScript 알고리즘 | Lv.0 정사각형으로 만들기► JS Algorithm/Programmers 2024. 8. 11. 23:47반응형
🔒 문제 설명
이차원 정수 배열 arr이 매개변수로 주어집니다. arr의 행의 수가 더 많다면 열의 수가 행의 수와 같아지도록 각 행의 끝에 0을 추가하고, 열의 수가 더 많다면 행의 수가 열의 수와 같아지도록 각 열의 끝에 0을 추가한 이차원 배열을 return 하는 solution 함수를 작성해 주세요.
🔒 제한사항
- 1 ≤ arr의 길이 ≤ 100
- 1 ≤ arr의 원소의 길이 ≤ 100
- arr의 모든 원소의 길이는 같습니다.
- 1 ≤ arr의 원소의 원소 ≤ 1,000
🔒 입출력 예
arr result [[572, 22, 37], [287, 726, 384], [85, 137, 292], [487, 13, 876]] [[572, 22, 37, 0], [287, 726, 384, 0], [85, 137, 292, 0], [487, 13, 876, 0]] [[57, 192, 534, 2], [9, 345, 192, 999]] [[57, 192, 534, 2], [9, 345, 192, 999], [0, 0, 0, 0], [0, 0, 0, 0]] [[1, 2], [3, 4]] [[1, 2], [3, 4]]
🔒 입출력 예 설명
입출력 예 #1
- 예제 1번의 arr은 행의 수가 4, 열의 수가 3입니다. 행의 수가 더 많으므로 열의 수를 4로 만들기 위해 arr의 각 행의 끝에 0을 추가한 이차원 배열 [[572, 22, 37, 0], [287, 726, 384, 0], [85, 137, 292, 0], [487, 13, 876, 0]]를 return 합니다.
입출력 예 #2
- 예제 2번의 arr은 행의 수가 2, 열의 수가 4입니다. 열의 수가 더 많으므로 행의 수를 4로 만들기 위해 arr의 각 열의 끝에 0을 추가한 이차원 배열 [[57, 192, 534, 2], [9, 345, 192, 999], [0, 0, 0, 0], [0, 0, 0, 0]]을 return 합니다.
입출력 예 #3
- 예제 3번의 arr은 행의 수와 열의 수가 2로 같습니다. 따라서 0을 추가하지 않고 [[1, 2], [3, 4]]을 return 합니다.
🔐 solution of mine
Math.max()
Array() 생성자
Array.from()
Array.prototype.fill()
Array.prototype.map()
function solution(arr) { const neededLength = Math.max(arr.length, arr[0].length); //container 배열 길이 구하기 const containerLength = new Array(neededLength); // 필요한 container 배열만큼 빈배열 만들기 const containerArr = Array.from( containerLength, (v, i) => arr[i] || new Array(neededLength).fill(0) ); // 배열이 길어지는 경우, 정답 도출하기 || 배열이 안길어지는 경우, 내부배열 길게 만들기 // 내부배열 길어진경우, 채우기 if (containerArr.length > containerArr[0].length) { const arr = containerArr.map((v) => new Array(containerArr.length).fill(0) ); return console.log( arr.map((v, i) => v.map((z, j) => (containerArr[i][j] ? containerArr[i][j] : 0)) ) ); } else { return console.log(containerArr); } } solution([ [572, 22, 37], [287, 726, 384], [85, 137, 292], [487, 13, 876], ]); // output: [[572, 22, 37, 0], [287, 726, 384, 0], [85, 137, 292, 0], [487, 13, 876, 0]] solution([ [57, 192, 534, 2], [9, 345, 192, 999], ]); // output: [[57, 192, 534, 2], [9, 345, 192, 999], [0, 0, 0, 0], [0, 0, 0, 0]] solution([ [1, 2], [3, 4], ]); // output: [[1, 2], [3, 4]]
🔐 GPT's refactoringMath.max()
Array.prototype.map()
Array.prototype.fill()
Array.prototype.push()
Array() 생성자
function solution(arr) { // 최종 배열의 길이는 주어진 배열의 행과 열 중 더 긴 길이를 선택 const neededLength = Math.max(arr.length, arr[0].length); // 각 행의 길이를 neededLength로 맞추고, 짧으면 0으로 채움 const containerArr = arr.map((row) => row.length < neededLength ? [...row, ...new Array(neededLength - row.length).fill(0)] : row ); // 행의 수가 부족하면 빈 배열을 0으로 채운 배열을 추가 while (containerArr.length < neededLength) { containerArr.push(new Array(neededLength).fill(0)); } // 결과 배열을 출력 return console.log(containerArr); } solution([ [572, 22, 37], [287, 726, 384], [85, 137, 292], [487, 13, 876], ]); // output: [[572, 22, 37, 0], [287, 726, 384, 0], [85, 137, 292, 0], [487, 13, 876, 0]] solution([ [57, 192, 534, 2], [9, 345, 192, 999], ]); // output: [[57, 192, 534, 2], [9, 345, 192, 999], [0, 0, 0, 0], [0, 0, 0, 0]] solution([ [1, 2], [3, 4], ]); // output: [[1, 2], [3, 4]]
🔐 GPT's solution
Array.prototype.map()
Array() 생성자
Array.prototype.fill()
Array.prototype.push()
function solution(arr) { const rowCount = arr.length; // 행의 수 const colCount = arr[0].length; // 열의 수 // 행의 수가 더 많은 경우: 각 행의 끝에 0을 추가 if (rowCount > colCount) { return console.log( arr.map((row) => [...row, ...new Array(rowCount - colCount).fill(0)]) ); } // 열의 수가 더 많은 경우: 행의 끝에 새로운 0으로 채워진 배열 추가 else if (rowCount < colCount) { const newArr = [...arr]; // 원본 배열을 변경하지 않도록 복사본 생성 while (newArr.length < colCount) { newArr.push(new Array(colCount).fill(0)); } return console.log(newArr); } // 행과 열의 수가 같을 경우: 그대로 반환 else { return console.log(arr); } } solution([ [572, 22, 37], [287, 726, 384], [85, 137, 292], [487, 13, 876], ]); // output: [[572, 22, 37, 0], [287, 726, 384, 0], [85, 137, 292, 0], [487, 13, 876, 0]] solution([ [57, 192, 534, 2], [9, 345, 192, 999], ]); // output: [[57, 192, 534, 2], [9, 345, 192, 999], [0, 0, 0, 0], [0, 0, 0, 0]] solution([ [1, 2], [3, 4], ]); // output: [[1, 2], [3, 4]]
🔐 solution of others
Array() 생성자
Math.max()
Array.prototype.fill()
Array.prototype.map()
const solution = (arr) => console.log( new Array(Math.max(arr.length, arr[0].length)) .fill(new Array(Math.max(arr.length, arr[0].length)).fill(0)) .map((v1, i1) => v1.map((v2, i2) => (arr[i1] ? arr[i1][i2] || 0 : 0))) ); solution([ [572, 22, 37], [287, 726, 384], [85, 137, 292], [487, 13, 876], ]); // output: [[572, 22, 37, 0], [287, 726, 384, 0], [85, 137, 292, 0], [487, 13, 876, 0]] solution([ [57, 192, 534, 2], [9, 345, 192, 999], ]); // output: [[57, 192, 534, 2], [9, 345, 192, 999], [0, 0, 0, 0], [0, 0, 0, 0]] solution([ [1, 2], [3, 4], ]); // output: [[1, 2], [3, 4]]
🔐 solution of others
Math.abs()
Array.prototype.push()
Array() 생성자
function solution(arr) { const ROWS = arr.length; const COLS = arr[0].length; const DIFF = Math.abs(ROWS - COLS); if (ROWS > COLS) { for (let i = 0; i < ROWS; i++) { for (let j = 0; j < DIFF; j++) { arr[i].push(0); } } } else if (ROWS < COLS) { for (let i = 0; i < DIFF; i++) { const row = new Array(COLS).fill(0); arr.push(row); } } return console.log(arr); } solution([ [572, 22, 37], [287, 726, 384], [85, 137, 292], [487, 13, 876], ]); // output: [[572, 22, 37, 0], [287, 726, 384, 0], [85, 137, 292, 0], [487, 13, 876, 0]] solution([ [57, 192, 534, 2], [9, 345, 192, 999], ]); // output: [[57, 192, 534, 2], [9, 345, 192, 999], [0, 0, 0, 0], [0, 0, 0, 0]] solution([ [1, 2], [3, 4], ]); // output: [[1, 2], [3, 4]]
반응형'► JS Algorithm > Programmers' 카테고리의 다른 글
[Programmers] JavaScript 알고리즘 | Lv.0 외계어 사전 (0) 2024.08.12 [Programmers] JavaScript 알고리즘 | Lv.0 수열과 구간 쿼리 2 (0) 2024.08.12 [Programmers] JavaScript 알고리즘 | Lv.0 잘라서 배열로 저장하기 (0) 2024.08.07 [Programmers] JavaScript 알고리즘 | Lv.0 수열과 구간 쿼리 3 (0) 2024.08.07 [Programmers] JavaScript 알고리즘 | Lv.0 한 번만 등장한 문자 (0) 2024.08.06 - 1 ≤ arr의 길이 ≤ 100