-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
2af87ce
commit 77ad7f7
Showing
9 changed files
with
255 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
const input1 = [1, -5, 2, 4, 3]; | ||
const input2 = [2, 1, 1, 3, 2, 5, 4]; | ||
const input3 = [6, 1, 7]; | ||
|
||
const solution = arr => { | ||
return arr.sort((a, b) => a - b); | ||
}; | ||
|
||
console.log(solution(input1)); | ||
console.log(solution(input2)); | ||
console.log(solution(input3)); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
const input1 = [4, 2, 2, 1, 3, 4]; | ||
|
||
const input2 = [2, 1, 1, 3, 2, 5, 4]; | ||
|
||
const soulution = arr => { | ||
return [...new Set(arr)].sort((a, b) => b - a); | ||
}; | ||
|
||
console.log(soulution(input1)); | ||
console.log(soulution(input2)); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
input1 = [2, 1, 3, 4, 1]; | ||
input2 = [5, 0, 2, 7]; | ||
|
||
const solution = arr => { | ||
const result = []; | ||
for (let i = 0; i < arr.length; i++) { | ||
for (let j = 0; j < i; j++) { | ||
result.push(arr[i] + arr[j]); | ||
} | ||
} | ||
return [...new Set(result)].sort((a, b) => a - b); | ||
}; | ||
|
||
console.log(solution(input1)); | ||
console.log(solution(input2)); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
const patterns = [ | ||
[1, 2, 3, 4, 5], | ||
[2, 1, 2, 3, 2, 4, 2, 5], | ||
[3, 3, 1, 1, 2, 2, 4, 4, 5, 5], | ||
]; | ||
|
||
input1 = [1, 2, 3, 4, 5]; // 1 | ||
input2 = [1, 3, 2, 4, 2]; // 1,2,3 | ||
|
||
const solution = answer => { | ||
const [p1, p2, p3] = patterns; | ||
const result = []; | ||
result.push(p1.filter((_, i) => answer[i] === p1[i]).length || 0); | ||
result.push(p2.filter((_, i) => answer[i] === p2[i]).length || 0); | ||
result.push(p3.filter((_, i) => answer[i] === p3[i]).length || 0); | ||
|
||
return [1, 2, 3].filter((_, i) => result[i] === Math.max(...result)); | ||
}; | ||
|
||
console.log(solution(input1)); | ||
console.log(solution(input2)); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
input1 = { | ||
arr1: [ | ||
[1, 4], | ||
[3, 2], | ||
[4, 1], | ||
], | ||
arr2: [ | ||
[3, 3], | ||
[3, 3], | ||
], | ||
}; // [[15,15],[15,15],[15,15]] | ||
|
||
input2 = { | ||
arr1: [ | ||
[2, 3, 2], | ||
[4, 2, 4], | ||
[3, 1, 4], | ||
], | ||
arr2: [ | ||
[5, 4, 3], | ||
[2, 4, 1], | ||
[3, 1, 1], | ||
], | ||
}; // [[22,22,11],[36,28,18],[29,20,14]] | ||
|
||
const solution = input => { | ||
const { arr1, arr2 } = input; | ||
const result = []; | ||
|
||
// arr1의 각 행에 대해 순회 | ||
for (let i = 0; i < arr1.length; i++) { | ||
const row = []; | ||
// arr2의 각 열에 대해 순회 | ||
for (let j = 0; j < arr2[0].length; j++) { | ||
let sum = 0; | ||
// 현재 행과 열의 각 요소를 곱하고 더함 | ||
for (let k = 0; k < arr2.length; k++) { | ||
sum += arr1[i][k] * arr2[k][j]; | ||
} | ||
row.push(sum); | ||
} | ||
result.push(row); | ||
} | ||
|
||
return result; | ||
}; | ||
|
||
console.log(solution(input1)); | ||
console.log(solution(input2)); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,127 @@ | ||
// 다익스트라알고리즘 | ||
// O((N+E)logN) | ||
// N: 노드의 개수 | ||
// E: 간선의 개수 | ||
|
||
// # 다익스트라 알고리즘 | ||
|
||
// 최단 경로를 구하는 알고리즘 | ||
|
||
// 가중치가 있는 그래프의 최단 경로를 구하는 문제는 대부분 다익스트라 알고리즘 | ||
|
||
// 1. 시작노드와 다음노드의 최소비용을 저장할 공간과 직전 노드를 저장할 공간 마련 | ||
// 2. 다음 노드들의 개수 N으로 이중 가중치가 적은 노드로 방문한다. | ||
// 3. 직전 노드를 저장하고 최소비용을 저장한다. | ||
// 4. 그 다음 방문하지 않는 노드들 중에서 가중치가 적은 노드를 찾는다. | ||
// 5. 3번을 다시 활용한다. | ||
// 6. 방향이 다르다면 이때 최소비용을 저장하지 않는다. | ||
// 7. 모든 노드들을 방문 하였다면 다시 거슬러 올라가서 최소비용의 노드를 계산한다. | ||
|
||
const input1 = { | ||
graph: { | ||
A: { B: 9, C: 3 }, | ||
B: { A: 5 }, | ||
C: { B: 1 }, | ||
}, | ||
start: 'A', | ||
}; | ||
// return | ||
// [ | ||
// {'A':0,'B':4,'C':3}, | ||
// { | ||
// 'A':['A'], | ||
// 'B':['A','C','B'], | ||
// 'C':['A','C'] | ||
// } | ||
// ] | ||
|
||
const input2 = { | ||
graph: { | ||
A: { B: 1 }, | ||
B: { C: 5 }, | ||
C: { D: 1 }, | ||
D: {}, | ||
}, | ||
start: 'A', | ||
}; | ||
|
||
// return | ||
// [ | ||
// {'A':0,'B':1,'C':6,'D':7}, | ||
// { | ||
// 'A':['A'], | ||
// 'B':['A','B'], | ||
// 'C':['A','B','C'], | ||
// 'D':['A','B','C','D'] | ||
// } | ||
// ] | ||
|
||
class PriorityQueue { | ||
constructor() { | ||
this.values = []; | ||
} | ||
|
||
enqueue(node, priority) { | ||
this.values.push({ node, priority }); | ||
this.sort(); | ||
} | ||
|
||
dequeue() { | ||
return this.values.shift(); | ||
} | ||
|
||
sort() { | ||
this.values.sort((a, b) => a.priority - b.priority); | ||
} | ||
} | ||
|
||
const solution = (graph, start) => { | ||
// 다익스트라는 직전 노드를 저장하는 배열이 필요하다. | ||
// 거리 저장 객체 초기화 | ||
const distances = {}; | ||
// 직전 노드를 저장하는 배열 초기화 | ||
const paths = {}; | ||
const pq = new PriorityQueue(); | ||
|
||
const visited = new Set(); | ||
|
||
// 모든 노드의 거리를 무한대로 초기화 | ||
// 왜? 거리를 무한대로 초기화하는 이유는 거리를 무한대로 초기화하면 최단 거리를 찾을 수 있기 때문이다. | ||
for (let node in graph) { | ||
distances[node] = Infinity; | ||
paths[node] = []; | ||
} | ||
// 시작 노드의 거리를 0으로 초기화 | ||
distances[start] = 0; | ||
// 시작 노드의 직전 노드를 시작 노드로 초기화 | ||
paths[start] = [start]; | ||
|
||
// 시작 노드를 우선순위 큐에 추가 | ||
pq.enqueue(start, 0); | ||
|
||
while (pq.values.length > 0) { | ||
const { node: currentNode } = pq.dequeue(); | ||
|
||
// 이미 처리한 노드는 건너뛰기 | ||
if (visited.has(currentNode)) continue; | ||
visited.add(currentNode); | ||
|
||
// 현재 노드의 이웃들만 확인 | ||
const neighbors = graph[currentNode]; | ||
for (let neighbor in neighbors) { | ||
const newDistance = distances[currentNode] + neighbors[neighbor]; | ||
|
||
if (newDistance < distances[neighbor]) { | ||
distances[neighbor] = newDistance; | ||
// 직접 연결된 경로만 사용 | ||
paths[neighbor] = [...paths[currentNode], neighbor]; | ||
pq.enqueue(neighbor, newDistance); | ||
} | ||
} | ||
} | ||
|
||
return [distances, paths]; | ||
}; | ||
|
||
console.log(solution(input1.graph, input1.start)); | ||
console.log(solution(input2.graph, input2.start)); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -23,7 +23,7 @@ | |
resolved "https://registry.npmjs.org/@babel/compat-data/-/compat-data-7.23.5.tgz" | ||
integrity sha512-uU27kfDRlhfKl+w1U6vp16IuvSLtjAxdArVXPa9BvLkrr7CYIsxH5adpHObeAGY/41+syctUWOZ140a2Rvkgjw== | ||
|
||
"@babel/core@^7.11.6", "@babel/core@^7.12.3": | ||
"@babel/core@^7.0.0", "@babel/core@^7.0.0-0", "@babel/core@^7.11.6", "@babel/core@^7.12.3", "@babel/core@^7.8.0": | ||
version "7.23.5" | ||
resolved "https://registry.npmjs.org/@babel/core/-/core-7.23.5.tgz" | ||
integrity sha512-Cwc2XjUrG4ilcfOw4wBAK+enbdgwAcAJCfGUItPBKR7Mjw4aEfAFYrLxeRp4jWgtNIKn3n2AlBOfwwafl+42/g== | ||
|
@@ -765,7 +765,7 @@ braces@^3.0.2: | |
dependencies: | ||
fill-range "^7.0.1" | ||
|
||
browserslist@^4.21.9: | ||
browserslist@^4.21.9, "browserslist@>= 4.21.0": | ||
version "4.22.1" | ||
resolved "https://registry.npmjs.org/browserslist/-/browserslist-4.22.1.tgz" | ||
integrity sha512-FEVc202+2iuClEhZhrWy6ZiAcRLvNMyYcxZ8raemul1DYVOVdFsbqckWLdsixQZCpJlwe77Z3UTalE7jsjnKfQ== | ||
|
@@ -872,16 +872,16 @@ color-convert@^2.0.1: | |
dependencies: | ||
color-name "~1.1.4" | ||
|
||
[email protected]: | ||
version "1.1.3" | ||
resolved "https://registry.npmjs.org/color-name/-/color-name-1.1.3.tgz" | ||
integrity sha512-72fSenhMw2HZMTVHeCA9KCmpEIbzWiQsjN+BHcBbS9vr1mtt+vJjPdksIBNUmKAW8TFUDPJK5SUU3QhE9NEXDw== | ||
|
||
color-name@~1.1.4: | ||
version "1.1.4" | ||
resolved "https://registry.npmjs.org/color-name/-/color-name-1.1.4.tgz" | ||
integrity sha512-dOy+3AuW3a2wNbZHIuMZpTcgjGuLU/uBL/ubcZF9OXbDo8ff4O8yVp5Bf0efS8uEoYo5q4Fx7dY9OgQGXgAsQA== | ||
|
||
[email protected]: | ||
version "1.1.3" | ||
resolved "https://registry.npmjs.org/color-name/-/color-name-1.1.3.tgz" | ||
integrity sha512-72fSenhMw2HZMTVHeCA9KCmpEIbzWiQsjN+BHcBbS9vr1mtt+vJjPdksIBNUmKAW8TFUDPJK5SUU3QhE9NEXDw== | ||
|
||
[email protected]: | ||
version "0.0.1" | ||
resolved "https://registry.npmjs.org/concat-map/-/concat-map-0.0.1.tgz" | ||
|
@@ -1445,7 +1445,7 @@ jest-resolve-dependencies@^29.7.0: | |
jest-regex-util "^29.6.3" | ||
jest-snapshot "^29.7.0" | ||
|
||
jest-resolve@^29.7.0: | ||
jest-resolve@*, jest-resolve@^29.7.0: | ||
version "29.7.0" | ||
resolved "https://registry.npmjs.org/jest-resolve/-/jest-resolve-29.7.0.tgz" | ||
integrity sha512-IOVhZSrg+UvVAshDSDtHyFCCBUl/Q3AAJv8iZ6ZjnZ74xzvwuzLXid9IIIPgTnY62SJjfuupMKZsZQRsCvxEgA== | ||
|
@@ -1894,7 +1894,14 @@ semver@^6.3.0, semver@^6.3.1: | |
resolved "https://registry.npmjs.org/semver/-/semver-6.3.1.tgz" | ||
integrity sha512-BR7VvDCVHO+q2xBEWskxS6DJE1qRnb7DxzUrogb71CWoSficBxYsiAGd+Kl0mmq/MprG9yArRkyrQxTO6XjMzA== | ||
|
||
semver@^7.5.3, semver@^7.5.4: | ||
semver@^7.5.3: | ||
version "7.5.4" | ||
resolved "https://registry.npmjs.org/semver/-/semver-7.5.4.tgz" | ||
integrity sha512-1bCSESV6Pv+i21Hvpxp3Dx+pSD8lIPt8uVjRrxAUt/nbswYc+tK6Y2btiULjd4+fnq15PX+nqQDC7Oft7WkwcA== | ||
dependencies: | ||
lru-cache "^6.0.0" | ||
|
||
semver@^7.5.4: | ||
version "7.5.4" | ||
resolved "https://registry.npmjs.org/semver/-/semver-7.5.4.tgz" | ||
integrity sha512-1bCSESV6Pv+i21Hvpxp3Dx+pSD8lIPt8uVjRrxAUt/nbswYc+tK6Y2btiULjd4+fnq15PX+nqQDC7Oft7WkwcA== | ||
|