Skip to content

Commit

Permalink
update post
Browse files Browse the repository at this point in the history
  • Loading branch information
dev-jonghoonpark committed Jun 29, 2024
1 parent 9eee1b9 commit 12f3007
Showing 1 changed file with 46 additions and 2 deletions.
48 changes: 46 additions & 2 deletions _posts/2024-02-13-leetcode-133.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ tags:
dfs,
id,
object reference,
Java,
자바,
vertex,
edge,
]
date: 2024-02-13 22:00:00 +0900
---
Expand Down Expand Up @@ -125,7 +129,7 @@ True

![graph](/assets/images/2024-02-13-leetcode-133/graph.png)

해당 에러로 검색해보면 맞는것 같은데 왜 안되냐는 질문들이 꽤 있다. 문제 제출자가 원하는 방식으로 풀지 않아서 그런가보다 생각중이다.
해당 에러로 검색해보면 맞는것 같은데 왜 안되냐는 질문들이 꽤 있다. 문제 제출자가 원하는 방식으로 풀지 않아서 그런가보다 생각중이다.
(아니면 내가 아직 파이썬에 대한 이해가 부족해서 그런걸수도 있을 것 같다.)

이후의 다른 케이스들에 대해서도 문제가 있을 수는 있겠지만, 적어도 위에 적은 케이스에 대해서 왜 통과를 못하는지는 확실히 모르겠다.
Expand Down Expand Up @@ -197,5 +201,45 @@ def cloneGraph(self, node: Optional['Node']) -> Optional['Node']:
그리고 찾아보면서 class variable 과 object variable에 대해서 알게 되었다.
class variable는 java로 치면 static 이라고 이해하면 될 것 같다.

이전에는 몰라서 썼던거인지라 이 이후로는 클래스 변수를 쓰는건 자제하고 있다.
이전에는 몰라서 썼던거인지라 이 이후로는 클래스 변수를 쓰는건 자제하고 있다.
파이썬에 조금씩 익숙해져 가는 것 같다.

## Java 로 다시 풀기

```java
class Solution {
public Node cloneGraph(Node node) {
return cloneGraph(new HashMap<>(), node);
}

private Node cloneGraph(Map<Integer, Node> map, Node node) {
if(node == null) {
return null;
}

if (map.containsKey(node.val)) {
return map.get(node.val);
}

Node copy = new Node(node.val);
map.put(node.val, copy);

for (int i = 0; i < node.neighbors.size(); i++) {
Node neighborNode = node.neighbors.get(i);
copy.neighbors.add(map.getOrDefault(neighborNode.val, cloneGraph(map, node.neighbors.get(i))));
}

return copy;
}
}
```

### 그래프 용어 정리

- vertex : 정점 (= node)
- edge : 간선

### TC, SC

node(vertex)의 수를 `V`, edge의 수를 `E` 라고 하였을 때 각 노드 마다 edge의 수만큼 반복을 해야한다.
시간 복잡도는 `O(V + E)` 이다. 공간 복잡도는 `O(V)`이다.

0 comments on commit 12f3007

Please sign in to comment.