diff --git a/bona1122/Array/Cutting_n^2.js b/bona1122/Array/Cutting_n^2.js new file mode 100644 index 0000000..37d43fe --- /dev/null +++ b/bona1122/Array/Cutting_n^2.js @@ -0,0 +1,11 @@ +// 핵심 +// 1. 1차원 배열을 인덱스를 이용해서 (n,n) 행렬로 변환하는 관점 +// 2. i = 1,2, ..., n 에 대해 1행1열부터 i행i열까지의 영역 내의 빈 칸을 숫자 i로 채우는 것이 Math.max(row, col)인 규칙을 갖는다는 것 +function solution(n, left, right) { + return Array.from({ length: right - left + 1 }, (_, idx) => { + const i = left + idx; + const row = Math.floor(i / n); + const col = i % n; + return Math.max(row, col) + 1; + }); +} diff --git a/bona1122/Array/Making_stars_at_intersection_points.js b/bona1122/Array/Making_stars_at_intersection_points.js new file mode 100644 index 0000000..82de4b8 --- /dev/null +++ b/bona1122/Array/Making_stars_at_intersection_points.js @@ -0,0 +1,42 @@ +function solution(line) { + const points = new Set(); + + // 교점 찾기(크래머 공식) + for (let i = 0; i < line.length - 1; i++) { + for (let j = i + 1; j < line.length; j++) { + const [a, b, e] = line[i]; + const [c, d, f] = line[j]; + + const denominator = a * d - b * c; + if (denominator === 0) continue; + + const x = (b * f - e * d) / denominator; + const y = (e * c - a * f) / denominator; + + if (x === Math.floor(x) && y === Math.floor(y)) { + points.add(`${x},${y}`); // set에서 중복없이 하려고 배열 대신 문자열 이용 -> 나중에 다시 변환 필요 + } + } + } + + // 좌표 배열로 변환, 최대/최소 구하기 + const coords = Array.from(points).map((p) => p.split(",").map(Number)); + const [minX, maxX] = [ + Math.min(...coords.map((p) => p[0])), + Math.max(...coords.map((p) => p[0])), + ]; + const [minY, maxY] = [ + Math.min(...coords.map((p) => p[1])), + Math.max(...coords.map((p) => p[1])), + ]; + + // 격자 생성, 별 찍기 + const grid = Array(maxY - minY + 1) + .fill() + .map(() => Array(maxX - minX + 1).fill(".")); + coords.forEach(([x, y]) => { + grid[maxY - y][x - minX] = "*"; + }); + + return grid.map((row) => row.join("")); +} diff --git a/bona1122/Array/Matrix_addition.js b/bona1122/Array/Matrix_addition.js new file mode 100644 index 0000000..d7088e7 --- /dev/null +++ b/bona1122/Array/Matrix_addition.js @@ -0,0 +1,6 @@ +const solution = (arr1, arr2) => { + const add = (a, b) => a + b; + const addArrays = (a, b) => a.map((item, idx) => add(item, b[idx])); + + return arr1.map((row, i) => addArrays(row, arr2[i])); +}; \ No newline at end of file diff --git a/bona1122/Array/Matrix_multiplication.js b/bona1122/Array/Matrix_multiplication.js new file mode 100644 index 0000000..04b95af --- /dev/null +++ b/bona1122/Array/Matrix_multiplication.js @@ -0,0 +1,13 @@ +const solution = (arr1, arr2) => { + // 결과 행렬 크기 생성 + const result = Array.from({ length: arr1.length }, () => + Array(arr2[0].length).fill(0) + ); + + // 행렬 요소 하나씩 값 만들기 + return result.map((row, i) => + r.map((item, j) => { + return arr1[i].reduce((acc, cur, k) => acc + cur * arr2[k][j], 0); + }) + ); +}; \ No newline at end of file diff --git a/bona1122/Array/Remove_smallest_number.js b/bona1122/Array/Remove_smallest_number.js new file mode 100644 index 0000000..c7f2673 --- /dev/null +++ b/bona1122/Array/Remove_smallest_number.js @@ -0,0 +1,7 @@ +const solution = (arr) => { + if (arr.length <= 1) return [-1]; + const min = Math.min(...arr); + const minIdx = arr.indexOf(min); + arr.splice(minIdx, 1); // splice는 제거된 요소들의 배열을 반환하므로 return arr.splice(minIdx, 1);는 틀리게된다. + return arr; +};