-
-
Notifications
You must be signed in to change notification settings - Fork 195
[TONY] Week 08 Solutions #501
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,19 @@ | ||
// TC: O(n) | ||
// -> visit all elements once for each to clone them | ||
// SC: O(n) | ||
// -> all elements are stored in HashMap and values are limited maximum 2 | ||
class Solution { | ||
private Map<Integer, Node> map = new HashMap<>(); | ||
public Node cloneGraph(Node node) { | ||
if (node == null) return null; | ||
if (map.containsKey(node.val)) return map.get(node.val); | ||
|
||
Node clone = new Node(node.val); | ||
map.put(clone.val, clone); | ||
|
||
for (Node neighbor : node.neighbors) { | ||
clone.neighbors.add(cloneGraph(neighbor)); | ||
} | ||
Comment on lines
+14
to
+16
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 clone; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
// TC: O(n * m) | ||
// the length of text1 by the length of text2 | ||
// SC: O(n) | ||
// both size of text1 and text2 can be the size of dp | ||
Comment on lines
+3
to
+4
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. 일차원배열의 dp를 가지고도 풀 수 있군요!!👍 코드로 이해를 완벽하게 못해서 모임에서 설명을 들어보고 싶어요! |
||
class Solution { | ||
public int longestCommonSubsequence(String text1, String text2) { | ||
int[] dp = new int[text2.length()]; | ||
int output = 0; | ||
|
||
for (char c : text1.toCharArray()) { | ||
int curLength = 0; | ||
|
||
for (int i = 0; i < dp.length; i++) { | ||
if (curLength < dp[i]) curLength = dp[i]; | ||
else if (c == text2.charAt(i)) { | ||
dp[i] = curLength + 1; | ||
output = Math.max(output, dp[i]); | ||
} | ||
} | ||
} | ||
return output; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
// TC: O(26 * n) => O(n) | ||
// iterates 26 times at the first for-loop, while loop O(n) | ||
// SC: O(1) | ||
class Solution { | ||
public int characterReplacement(String s, int k) { | ||
int ans = 0; | ||
int n = s.length(); | ||
for (char c = 'A'; c <= 'Z'; c++) { | ||
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. 오, 각 알파벳의 |
||
int i = 0, j = 0, replaced = 0; | ||
while (j < n) { | ||
if (s.charAt(j) == c) { | ||
j += 1; | ||
} else if (replaced < k) { | ||
j += 1; | ||
replaced++; | ||
} else if (s.charAt(i) == c) { | ||
i += 1; | ||
} else { | ||
i += 1; | ||
replaced -= 1; | ||
} | ||
ans = Math.max(ans, j - i); | ||
} | ||
} | ||
return ans; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
// TC: O(n) | ||
// n = length sum of list1 and list2 | ||
// SC: O(n) | ||
// n = node 0 ~ length sum of list1 and list2 | ||
Comment on lines
+3
to
+4
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. 혹시 공간복잡도 구하신 부분 설명 부탁드려도 될까요? 저는 매개변수로 주어진 데이터를 제외하고 추가로 사용된 데이터공간에 대해서 복잡도를 구해서 |
||
class Solution { | ||
public ListNode mergeTwoLists(ListNode list1, ListNode list2) { | ||
ListNode node = new ListNode(0); | ||
ListNode output = node; | ||
|
||
while (list1 != null && list2 != null) { | ||
if (list1.val > list2.val) { | ||
node.next = list2; | ||
list2 = list2.next; | ||
} else { | ||
node.next = list1; | ||
list1 = list1.next; | ||
} | ||
node = node.next; | ||
} | ||
|
||
if (list1 == null) node.next = list2; | ||
if (list2 == null) node.next = list1; | ||
Comment on lines
+21
to
+22
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. 아, 마지막 남은 linked list 연결할때 저는 휴리스틱하게 while문으로 작성했습니다.. 그런데 if문으로 한번만 연결하면 되었군요... 다시 제 코드를 돌아보게 되었습니다 😂 |
||
|
||
return output.next; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
// TC: O(1) | ||
// SC: O(1) | ||
class Solution { | ||
public int getSum(int a, int b) { | ||
while (b != 0) { | ||
int temp = (a & b) << 1; | ||
a = a^b; | ||
b = temp; | ||
} | ||
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.
n
은 노드의 수를 뜻하는 걸까요?n
에 대한 추가 설명이 있으면 좋을 듯 싶어요!