-
Notifications
You must be signed in to change notification settings - Fork 4
[Bona1122] 25.01.02 #8
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| 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; | ||
| }); | ||
| } |
| 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])), | ||
| ]; | ||
|
|
||
| // 격자 생성, 별 찍기 | ||
| 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("")); | ||
| } | ||
| 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
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. add함수랑 addArrays함수를 만들어서 문제를 푼 부분에서 이런 방식으로 푸니깐 더 코드가 깔끔해보이네요👍👍 배우고 갑니다~ |
||
| }; | ||
| 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) => { | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
| return arr1[i].reduce((acc, cur, k) => acc + cur * arr2[k][j], 0); | ||
| }) | ||
| ); | ||
| }; | ||
| 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);는 틀리게된다. | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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; | ||
| }; | ||
There was a problem hiding this comment.
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메서드 사용하면 한번의 순회로 모든 값을 처리할수 있어서 이런식으로 개선해보는것이 어떨까 생각해봤습니다!