-
Notifications
You must be signed in to change notification settings - Fork 122
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
[강희찬] WEEK 8 Solution #502
Changes from all commits
5c0a937
e765811
3de0ee0
42b6fba
7561b4e
d5373fd
f690485
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,63 @@ | ||
/** | ||
* https://leetcode.com/problems/clone-graph | ||
* T.C. O(N) | ||
* S.C. O(N) | ||
*/ | ||
function cloneGraph(node: _Node | null): _Node | null { | ||
if (!node) return null; | ||
|
||
const map = new Map<number, _Node>(); | ||
return dfs(node); | ||
|
||
function dfs(node: _Node): _Node { | ||
if (map.has(node.val)) return map.get(node.val)!; | ||
|
||
const newNode = new _Node(node.val); | ||
map.set(node.val, newNode); | ||
|
||
for (let neighbor of node.neighbors) { | ||
newNode.neighbors.push(dfs(neighbor)); | ||
} | ||
|
||
return newNode; | ||
} | ||
} | ||
|
||
/** | ||
* T.C. O(N) | ||
* S.C. O(N) | ||
*/ | ||
function cloneGraph(node: _Node | null): _Node | null { | ||
if (!node) return null; | ||
|
||
const map = new Map<number, _Node>(); | ||
const stack = [node]; | ||
const newNode = new _Node(node.val); | ||
map.set(node.val, newNode); | ||
|
||
while (stack.length) { | ||
const node = stack.pop()!; | ||
const newNode = map.get(node.val)!; | ||
|
||
for (let neighbor of node.neighbors) { | ||
if (!map.has(neighbor.val)) { | ||
stack.push(neighbor); | ||
const newNeighbor = new _Node(neighbor.val); | ||
map.set(neighbor.val, newNeighbor); | ||
} | ||
newNode.neighbors.push(map.get(neighbor.val)!); | ||
} | ||
} | ||
|
||
return newNode; | ||
} | ||
|
||
class _Node { | ||
val: number; | ||
neighbors: _Node[]; | ||
|
||
constructor(val?: number, neighbors?: _Node[]) { | ||
this.val = val === undefined ? 0 : val; | ||
this.neighbors = neighbors === undefined ? [] : neighbors; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
/** | ||
* https://leetcode.com/problems/longest-common-subsequence | ||
* T.C. O(m * n) | ||
* S.C. O(n) | ||
*/ | ||
function longestCommonSubsequence(text1: string, text2: string): number { | ||
const dp = Array.from({ length: text2.length + 1 }, () => 0); | ||
|
||
for (let i = 1; i <= text1.length; i++) { | ||
let prev = 0; | ||
for (let j = 1; j <= text2.length; j++) { | ||
const temp = dp[j]; | ||
if (text1[i - 1] === text2[j - 1]) { | ||
dp[j] = prev + 1; | ||
} else { | ||
dp[j] = Math.max(dp[j], dp[j - 1]); | ||
} | ||
prev = temp; | ||
} | ||
} | ||
|
||
return dp[text2.length]; | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
/** | ||
* https://leetcode.com/problems/longest-repeating-character-replacement | ||
* T.C. O(n) | ||
* S.C. O(1) | ||
*/ | ||
function characterReplacement(s: string, k: number): number { | ||
const charCount = new Array(26).fill(0); | ||
let maxCount = 0; | ||
let start = 0; | ||
let maxLen = 0; | ||
|
||
const A = 'A'.charCodeAt(0); | ||
for (let end = 0; end < s.length; end++) { | ||
const endCharIdx = s.charCodeAt(end) - A; | ||
maxCount = Math.max(maxCount, ++charCount[endCharIdx]); | ||
|
||
if (end - start + 1 - maxCount > k) { | ||
charCount[s.charCodeAt(start) - A]--; | ||
start++; | ||
Comment on lines
+15
to
+19
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. 위 코드에서는 저는 일반적으로 연산 순서에 영향받지 않는다면 후위 연산을 선호하는데요, 별다른 이유는 없고 익숙해서 그렇습니다 ㅎㅎ |
||
} | ||
|
||
maxLen = Math.max(maxLen, end - start + 1); | ||
} | ||
return maxLen; | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
class ListNode { | ||
val: number; | ||
next: ListNode | null; | ||
constructor(val?: number, next?: ListNode | null) { | ||
this.val = val === undefined ? 0 : val; | ||
this.next = next === undefined ? null : next; | ||
} | ||
} | ||
|
||
/** | ||
* https://leetcode.com/problems/merge-two-sorted-lists | ||
* T.C. O(m + n) | ||
* S.C. O(1) | ||
*/ | ||
function mergeTwoLists( | ||
list1: ListNode | null, | ||
list2: ListNode | null | ||
): ListNode | null { | ||
let head = new ListNode(); | ||
let current = head; | ||
|
||
while (list1 && list2) { | ||
if (list1.val < list2.val) { | ||
current.next = list1; | ||
list1 = list1.next; | ||
} else { | ||
current.next = list2; | ||
list2 = list2.next; | ||
} | ||
current = current.next; | ||
} | ||
|
||
current.next = list1 || list2; | ||
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. 자스는 null이면 false를 반환하니 이게 가능하군요 :) 👍 |
||
|
||
return head.next; | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
/** | ||
* https://leetcode.com/problems/sum-of-two-integers | ||
* T.C. O(log a) | ||
* S.C. O(1) | ||
*/ | ||
function getSum(a: number, b: number): number { | ||
while (b != 0) { | ||
let carry = a & b; | ||
a = a ^ b; | ||
b = carry << 1; | ||
} | ||
return a; | ||
} |
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.
결국 문제를 풀기 위해서는 재귀적으로 풀어야 하는데, 주어진
cloneGraph
자체를 재귀 함수로 사용하는 방법도 괜찮은것 같습니다!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.
아래와 같은 형태로 해당 함수의
params
를 변경한다면cloneGraph
를 직접 재귀함수로 사용해도 최적의 풀이가 가능할 것 같네요