Skip to content

[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

Merged
merged 5 commits into from
Oct 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions clone-graph/TonyKim9401.java
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
Comment on lines +1 to +4
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

n은 노드의 수를 뜻하는 걸까요? n에 대한 추가 설명이 있으면 좋을 듯 싶어요!

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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

저는 노드에 연결된 간선의 수만큼 순회를 하게 될 듯 싶어서 TC: O(총 간선수)라고 생각했습니다. 혹시 어떠한 방식으로 시간복잡도를 구하셨는지 추가 설명 가능하실까요??

return clone;
}
}
23 changes: 23 additions & 0 deletions longest-common-subsequence/TonyKim9401.java
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
Copy link
Contributor

Choose a reason for hiding this comment

The 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;
}
}
27 changes: 27 additions & 0 deletions longest-repeating-character-replacement/TonyKim9401.java
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++) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

오, 각 알파벳의 the longest substring를 구하는 방법도 있군요!
주어진 문자열에서만 해결하려던 제 고정관념을 깨는 느낌이 들었어요😮
좋은 아이디어 감사합니다!!

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;
}
}
26 changes: 26 additions & 0 deletions merge-two-sorted-lists/TonyKim9401.java
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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

혹시 공간복잡도 구하신 부분 설명 부탁드려도 될까요?

저는 매개변수로 주어진 데이터를 제외하고 추가로 사용된 데이터공간에 대해서 복잡도를 구해서 O(1)로 계산했습니다. @TonyKim9401 님께서도 동일한 로직이라 동일한 공간복잡도가 나올줄 알았으나 O(n)으로 계산하셔서 궁금했습니다!

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
Copy link
Contributor

@wogha95 wogha95 Oct 5, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

아, 마지막 남은 linked list 연결할때 저는 휴리스틱하게 while문으로 작성했습니다.. 그런데 if문으로 한번만 연결하면 되었군요...

다시 제 코드를 돌아보게 되었습니다 😂
좋은 코드 감사합니다!!


return output.next;
}
}
12 changes: 12 additions & 0 deletions sum-of-two-integers/TonyKim9401.java
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;
}
}