Skip to content

Commit 27df172

Browse files
committed
Merge branch 'week10'
2 parents ce4a26e + 586eedc commit 27df172

4 files changed

+142
-0
lines changed

Diff for: week10/Leetcode_(Path Sum III).java

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
class Solution {
2+
private int count;
3+
private int targetSum;
4+
private long[] sum;
5+
6+
public int pathSum(TreeNode root, int _targetSum) {
7+
targetSum = _targetSum;
8+
sum = new long[1001];
9+
dfs(root, 1);
10+
return count;
11+
}
12+
13+
public void dfs(TreeNode node, int depth) {
14+
if(node == null) {
15+
return;
16+
}
17+
18+
sum[depth] = sum[depth-1] + node.val;
19+
for(int i=depth-1; i>=0; i--) {
20+
if(sum[depth] - sum[i] == targetSum) {
21+
count++;
22+
}
23+
}
24+
25+
dfs(node.left, depth+1);
26+
dfs(node.right, depth+1);
27+
}
28+
}

Diff for: week10/Programmers_12946(하노이의 탑).java

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import java.util.*;
2+
3+
class Solution {
4+
5+
private List<int[]> arr;
6+
7+
public int[][] solution(int n) {
8+
arr = new ArrayList<>();
9+
hanoi(1, 2, 3, n);
10+
int[][] answer = arr.stream().toArray(int[][]::new);
11+
return answer;
12+
}
13+
14+
public void hanoi(int from, int mid, int to, int n) {
15+
if(n == 0) {
16+
return;
17+
}
18+
19+
hanoi(from, to, mid, n-1);
20+
arr.add(new int[]{from, to});
21+
hanoi(mid, from, to, n-1);
22+
}
23+
}

Diff for: week10/Programmers_43163(단어 변환).java

+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
class Solution {
2+
3+
private String[] words;
4+
private boolean[] visited;
5+
private String target;
6+
private int answer;
7+
8+
public int solution(String begin, String _target, String[] _words) {
9+
words = _words;
10+
target = _target;
11+
visited = new boolean[words.length];
12+
answer = Integer.MAX_VALUE;
13+
dfs(begin, 0, 0);
14+
return (answer == Integer.MAX_VALUE) ? 0 : answer;
15+
}
16+
17+
public void dfs(String now, int step, int count) {
18+
if(now.equals(target)) {
19+
answer = Math.min(answer, count);
20+
return;
21+
}
22+
if(step == words.length) {
23+
return;
24+
}
25+
26+
for(int i=0; i<words.length; i++) {
27+
if(!visited[i] && check(now, words[i])) {
28+
visited[i] = true;
29+
dfs(words[i], step+1, count+1);
30+
visited[i] = false;
31+
}
32+
}
33+
}
34+
35+
public boolean check(String s1, String s2) {
36+
int count = 0;
37+
int len = s1.length();
38+
for(int i=0; i<len; i++) {
39+
count += (s1.charAt(i) == s2.charAt(i)) ? 1 : 0;
40+
}
41+
return (count == len-1);
42+
}
43+
}

Diff for: week10/Programmers_43164(여행경로).java

+48
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
import java.util.*;
2+
3+
class Solution {
4+
private String[][] tickets;
5+
private boolean[] visited;
6+
private List<List<String>> pathList;
7+
8+
public String[] solution(String[][] _tickets) {
9+
tickets = _tickets;
10+
visited = new boolean[tickets.length];
11+
pathList = new ArrayList<>();
12+
List<String> path = new ArrayList<>();
13+
path.add("ICN");
14+
path = dfs(path, "ICN");
15+
Collections.sort(pathList, (o1, o2) -> {
16+
for(int i=0; i<o1.size(); i++) {
17+
String s1 = o1.get(i);
18+
String s2 = o2.get(i);
19+
if(!s1.equals(s2)) {
20+
return s1.compareTo(s2);
21+
}
22+
}
23+
return 0;
24+
});
25+
String[] answer = pathList.get(0).stream().toArray(String[]::new);
26+
return answer;
27+
}
28+
29+
public List<String> dfs(List<String> path, String now) {
30+
if(path.size() == tickets.length+1) {
31+
pathList.add(new ArrayList<String>(path));
32+
return path;
33+
}
34+
35+
for(int i=0; i<tickets.length; i++) {
36+
String v1 = tickets[i][0];
37+
String v2 = tickets[i][1];
38+
if(v1.equals(now) && !visited[i]) {
39+
path.add(v2);
40+
visited[i] = true;
41+
dfs(path, v2);
42+
visited[i] = false;
43+
path.remove(path.size()-1);
44+
}
45+
}
46+
return path;
47+
}
48+
}

0 commit comments

Comments
 (0)