-
-
Notifications
You must be signed in to change notification settings - Fork 195
[jdy8739] week 8 #955
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
[jdy8739] week 8 #955
Changes from all commits
a46a3b1
7f18137
031f8ca
d2f5691
8043d8e
4177b2f
3eadf05
6e3faf4
550375d
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,82 @@ | ||
/** | ||
* // Definition for a _Node. | ||
* function _Node(val, neighbors) { | ||
* this.val = val === undefined ? 0 : val; | ||
* this.neighbors = neighbors === undefined ? [] : neighbors; | ||
* }; | ||
*/ | ||
|
||
/** | ||
* @param {_Node} node | ||
* @return {_Node} | ||
*/ | ||
var cloneGraph = function (node) { | ||
const nodeMap = new Map(); | ||
|
||
const dfs = (nodeParam) => { | ||
if (!nodeParam) { | ||
return null; | ||
} | ||
|
||
if (nodeMap.has(nodeParam.val)) { | ||
return nodeMap.get(nodeParam.val); | ||
} | ||
|
||
const clone = new Node(nodeParam.val); | ||
|
||
nodeMap.set(clone.val, clone); | ||
|
||
for (const nei of nodeParam.neighbors) { | ||
clone.neighbors = [...clone.neighbors, dfs(nei)]; | ||
} | ||
|
||
return clone; | ||
} | ||
|
||
return dfs(node); | ||
}; | ||
|
||
// 재귀를 활용한 dfs를 사용한 풀이 | ||
// 시간복잡도 O(n + e) -> n(노드의 수) + e(간선의 수) 만큼 탐색을 수행 | ||
// 공간복잡도 O(n) -> map이 총 노드의 수 만큼 크기를 가짐 | ||
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.
|
||
|
||
// ------------------------------------ | ||
|
||
/** | ||
* @param {_Node} node | ||
* @return {_Node} | ||
*/ | ||
var cloneGraph = function(node) { | ||
if (!node) { | ||
return null; | ||
} | ||
|
||
const clone = new Node(node.val); | ||
|
||
const clonedNodeMap = new Map().set(clone.val, clone); | ||
|
||
// 이 queue에는 클론이 아니라 원본 노드가 들어가야함(neighbors에 대한 참조 때문) | ||
const queue = [node]; | ||
Comment on lines
+58
to
+59
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. 알고리즘은 정상적으로 동작하지만 해당 Array는 queue가 아니네요 (stack처럼 동작해요!) |
||
|
||
while (queue.length > 0) { | ||
const currentNode = queue.pop(); | ||
|
||
const currentNodeClone = clonedNodeMap.get(currentNode.val); | ||
|
||
for (const nei of currentNode.neighbors) { | ||
if (!clonedNodeMap.has(nei.val)) { | ||
clonedNodeMap.set(nei.val, new Node(nei.val)); | ||
queue.push(nei); | ||
} | ||
|
||
currentNodeClone.neighbors = [...currentNodeClone.neighbors, clonedNodeMap.get(nei.val)]; | ||
} | ||
} | ||
|
||
return clone; | ||
}; | ||
|
||
// 큐를 활용한 bfs를 사용한 풀이 | ||
// 시간복잡도 O(n + e) -> n(노드의 수) + e(간선의 수) 만큼 탐색을 수행 | ||
// 공간복잡도 O(n) -> map이 총 노드의 수 만큼 크기를 가짐 | ||
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. 시간복잡도에서 노드의 수를 v라고 표현하셨으니까 공간복잡도도 O(v)라고 써주면 더 나을 것 같아요 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. 그러네요 수정해서 커밋하겠습니다. 감사합니다! :) |
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
/** | ||
* @param {string} text1 | ||
* @param {string} text2 | ||
* @return {number} | ||
*/ | ||
var longestCommonSubsequence = function(text1, text2) { | ||
const resultMap = new Map(); | ||
|
||
const dfs = (i, j) => { | ||
const mapKey = `${i}-${j}`; | ||
|
||
if (resultMap.has(mapKey)) { | ||
return resultMap.get(mapKey); | ||
} | ||
|
||
if (i === text1.length || j === text2.length) { | ||
return 0; | ||
} | ||
|
||
if (text1[i] === text2[j]) { | ||
const resultHit = 1 + dfs(i + 1, j + 1); | ||
resultMap.set(mapKey, resultHit); | ||
return resultHit; | ||
} | ||
|
||
const resultMiss = Math.max(dfs(i + 1, j), dfs(i, j + 1)); | ||
resultMap.set(mapKey, resultMiss); | ||
return resultMiss; | ||
} | ||
|
||
return dfs(0, 0); | ||
}; | ||
|
||
// 시간복잡도 O(m * n) -> 재귀를 통해 m과 n이 1씩 증가하면서 상대편의 m or n 번 째 인덱스 문자열을 비교하므로??? (잘 모르겠습니다. 리뷰어분이 여유되시면 설명부탁드립니다 ㅜㅜ) | ||
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. 어디까지 이해되셨고 어디부터 이해가 잘 안되시는지 Discussion에 올려주세요~! |
||
// 공간복잡도 O(m * n) -> 맵에 키가 m * n개로 최대로 요소가 들어옴 |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
/** | ||
* @param {string} s | ||
* @param {number} k | ||
* @return {number} | ||
*/ | ||
var characterReplacement = function(s, k) { | ||
let start = 0; | ||
let end = 0; | ||
let maxLength = 0; | ||
let maxCountOfChar = 0; | ||
|
||
const charMap = new Map(); | ||
charMap.set(s[end], 1); | ||
|
||
while (end < s.length) { | ||
maxCountOfChar = Math.max(maxCountOfChar, charMap.get(s[end])); | ||
|
||
const currLength = end - start + 1; | ||
|
||
const countOfOthers = currLength - maxCountOfChar; | ||
|
||
// 현재 문자열의 길이에서 가장 많이 나오는 문자열의 수를 뺸 값인 countOfOthers가 | ||
// k보다 작으면 현재 문자열에서 start번 째 문자의 수를 map에서 감소시키고 star의 값을 증가시킨다. | ||
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. typo) k보다 크면 |
||
if (countOfOthers > k) { | ||
const startCharCount = charMap.get(s[start]); | ||
charMap.set(s[start], startCharCount - 1); | ||
|
||
start++; | ||
} else { | ||
// countOfOthers가 k보다 같거나 작을 경우 k번 문자를 바꾸는 것으로 현재 문자열을 모두 하나의 문자로 통일시킬 수 있다는 것으로 | ||
// 현재 문자열에서 end번 째 문자의 수를 map에서 증가시킨다. | ||
// 이후 end의 값을 증가시킨다. | ||
end++; | ||
|
||
const endCharCount = charMap.get(s[end]); | ||
|
||
if (endCharCount) { | ||
charMap.set(s[end], endCharCount + 1); | ||
} else { | ||
charMap.set(s[end], 1); | ||
} | ||
} | ||
|
||
maxLength = Math.max(maxLength, Math.min(maxCountOfChar + k, currLength)); | ||
} | ||
|
||
return maxLength; | ||
}; | ||
|
||
// 시간복잡도 O(n) -> 슬라이딩 윈도우기법으로 최대 1번 순회한다. | ||
// 공간복잡도 O(1) -> map의 크기는 알파벳의 갯수인 최대 26개이다. |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
/** | ||
* @param {number} n | ||
* @return {number} | ||
*/ | ||
var hammingWeight = function (n) { | ||
const binary = [1]; | ||
|
||
while (binary[0] < n) { | ||
const latest = binary[0] * 2; | ||
|
||
binary.unshift(latest); | ||
} | ||
|
||
let count = 0; | ||
|
||
for (let i = 0; i < binary.length; i++) { | ||
if (binary[i] <= n) { | ||
count++; | ||
n = n - binary[i]; | ||
} | ||
|
||
if (n === 0) { | ||
break; | ||
} | ||
} | ||
|
||
return count; | ||
}; | ||
|
||
// 시간복잡도 O(logn) -> 이진탐색처럼 2씩 곱해가며 n을 넘어가는 가장 큰 수를 찾으므로 | ||
Comment on lines
+1
to
+30
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.
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. js의 배열이 원소들이 연달아 주소값을 가지는 형태가 아니다보니 O(1)라고 생각했는데요, 한 번 알아보고 주석을 다시 달아 커밋하겠습니다. 코멘트 감사합니다! 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. js의 unshift는 배열의 요소가 맨 앞에 추가될 때마다 뒤 모든 요소들의 index가 1 씩 추가되므로 O(n)의 복잡도를 갖는다는 것을 알게되었습니다!!
✅ (1) Packed Elements (연속된 메모리, 빠름) let arr = [1, 2, 3, 4]; // Packed Elements 사용 let arr = []; |
||
|
||
// 수정된 시간복잡도 -> 위의 while 문에서 O(logn)의 시간복잡도를 수행하면서 O(n)의 시간복잡도를 갖는 unshift 메소드를 사용하며 | ||
// for문에서 다시 O(logn)의 시간복잡도의 루프를 돌기 때문에 | ||
// 총 복잡도는 O(logn) * binary 배열의 길이 + O(logn) |
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.
Array를 매번 새로 만들어주는 대신에 새 원소를 push해주는 방식은 어떤가요?
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.
넵 풀이에 그렇게 push로 원소를 넣어 주던데 그 방식이 좀 더 간결하고 성능도 나은 것 같습니다 :)