Skip to content

Commit 1bc8b59

Browse files
authored
[Bona1122] 25.01.02 (#8)
* Remove smallest number / 중급 * Matrix addition / 중급 * Matrix multiplication / 심화 * Cutting n^2 / 심화 * Making stars at intersection points / 심화
1 parent 4ad8b58 commit 1bc8b59

File tree

5 files changed

+79
-0
lines changed

5 files changed

+79
-0
lines changed

bona1122/Array/Cutting_n^2.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
// 핵심
2+
// 1. 1차원 배열을 인덱스를 이용해서 (n,n) 행렬로 변환하는 관점
3+
// 2. i = 1,2, ..., n 에 대해 1행1열부터 i행i열까지의 영역 내의 빈 칸을 숫자 i로 채우는 것이 Math.max(row, col)인 규칙을 갖는다는 것
4+
function solution(n, left, right) {
5+
return Array.from({ length: right - left + 1 }, (_, idx) => {
6+
const i = left + idx;
7+
const row = Math.floor(i / n);
8+
const col = i % n;
9+
return Math.max(row, col) + 1;
10+
});
11+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
function solution(line) {
2+
const points = new Set();
3+
4+
// 교점 찾기(크래머 공식)
5+
for (let i = 0; i < line.length - 1; i++) {
6+
for (let j = i + 1; j < line.length; j++) {
7+
const [a, b, e] = line[i];
8+
const [c, d, f] = line[j];
9+
10+
const denominator = a * d - b * c;
11+
if (denominator === 0) continue;
12+
13+
const x = (b * f - e * d) / denominator;
14+
const y = (e * c - a * f) / denominator;
15+
16+
if (x === Math.floor(x) && y === Math.floor(y)) {
17+
points.add(`${x},${y}`); // set에서 중복없이 하려고 배열 대신 문자열 이용 -> 나중에 다시 변환 필요
18+
}
19+
}
20+
}
21+
22+
// 좌표 배열로 변환, 최대/최소 구하기
23+
const coords = Array.from(points).map((p) => p.split(",").map(Number));
24+
const [minX, maxX] = [
25+
Math.min(...coords.map((p) => p[0])),
26+
Math.max(...coords.map((p) => p[0])),
27+
];
28+
const [minY, maxY] = [
29+
Math.min(...coords.map((p) => p[1])),
30+
Math.max(...coords.map((p) => p[1])),
31+
];
32+
33+
// 격자 생성, 별 찍기
34+
const grid = Array(maxY - minY + 1)
35+
.fill()
36+
.map(() => Array(maxX - minX + 1).fill("."));
37+
coords.forEach(([x, y]) => {
38+
grid[maxY - y][x - minX] = "*";
39+
});
40+
41+
return grid.map((row) => row.join(""));
42+
}

bona1122/Array/Matrix_addition.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
const solution = (arr1, arr2) => {
2+
const add = (a, b) => a + b;
3+
const addArrays = (a, b) => a.map((item, idx) => add(item, b[idx]));
4+
5+
return arr1.map((row, i) => addArrays(row, arr2[i]));
6+
};
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
const solution = (arr1, arr2) => {
2+
// 결과 행렬 크기 생성
3+
const result = Array.from({ length: arr1.length }, () =>
4+
Array(arr2[0].length).fill(0)
5+
);
6+
7+
// 행렬 요소 하나씩 값 만들기
8+
return result.map((row, i) =>
9+
r.map((item, j) => {
10+
return arr1[i].reduce((acc, cur, k) => acc + cur * arr2[k][j], 0);
11+
})
12+
);
13+
};
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
const solution = (arr) => {
2+
if (arr.length <= 1) return [-1];
3+
const min = Math.min(...arr);
4+
const minIdx = arr.indexOf(min);
5+
arr.splice(minIdx, 1); // splice는 제거된 요소들의 배열을 반환하므로 return arr.splice(minIdx, 1);는 틀리게된다.
6+
return arr;
7+
};

0 commit comments

Comments
 (0)