Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions bona1122/Array/Cutting_n^2.js
Original file line number Diff line number Diff line change
@@ -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;
});
}
42 changes: 42 additions & 0 deletions bona1122/Array/Making_stars_at_intersection_points.js
Original file line number Diff line number Diff line change
@@ -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])),
];
Comment on lines +23 to +31
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

그리고 map과 Math.min(), Math.max()가 각각 두 번 호출하기에, 각 좌표 배열을 두 번 순회해야 하는 부분을 reduce메서드 사용하면 한번의 순회로 모든 값을 처리할수 있어서 이런식으로 개선해보는것이 어떨까 생각해봤습니다!

const coords = Array.from(points).map((p) => p.split(",").map(Number));
const [minX, maxX, minY, maxY] = coords.reduce(
  ([minX, maxX, minY, maxY], [x, y]) => [
    Math.min(minX, x),
    Math.max(maxX, x),
    Math.min(minY, y),
    Math.max(maxY, y),
  ],
  [Infinity, -Infinity, Infinity, -Infinity]
);


// 격자 생성, 별 찍기
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(""));
}
6 changes: 6 additions & 0 deletions bona1122/Array/Matrix_addition.js
Original file line number Diff line number Diff line change
@@ -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]));
Comment on lines +2 to +5
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

add함수랑 addArrays함수를 만들어서 문제를 푼 부분에서 이런 방식으로 푸니깐 더 코드가 깔끔해보이네요👍👍 배우고 갑니다~

};
13 changes: 13 additions & 0 deletions bona1122/Array/Matrix_multiplication.js
Original file line number Diff line number Diff line change
@@ -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) => {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

r.map -> row.map 으로 변경해야 할것 같습니다:)

return arr1[i].reduce((acc, cur, k) => acc + cur * arr2[k][j], 0);
})
);
};
7 changes: 7 additions & 0 deletions bona1122/Array/Remove_smallest_number.js
Original file line number Diff line number Diff line change
@@ -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);는 틀리게된다.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

저는 filter를 사용해서 문제를 풀었는데, splice를 이용해서 푸는 방법도 알아갑니다!! arr이 [4, 3, 2, 1]일때, arr.splice(minIdx, 1);는 1일텐데, return arr.splice(minIdx, 1);를 하면 1를 제거한 배열이 아닌 1를 반환해서 틀리는군요....잘 알아둬야겠군요!!

return arr;
};
Loading