Skip to content

Commit a7004da

Browse files
authored
Merge pull request #1660 from Tessa1217/main
[Tessa1217] Week 15 Solutions
2 parents 6e7633d + 718e09c commit a7004da

File tree

5 files changed

+314
-0
lines changed

5 files changed

+314
-0
lines changed

alien-dictionary/Tessa1217.java

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
import java.util.*;
2+
3+
public class Solution {
4+
5+
/**
6+
* @param words: a list of words
7+
* @return: a string which is correct order
8+
*/
9+
// 라틴 알파벳을 쓰는 새로운 외계 문자가 있다. 하지만 문자의 순서는 알지 못한다.
10+
// 새로운 언어의 규칙에 맞는 사전식 순서(알파벳 순서)로 정의된 non-empty(비지 않은 상태인) 단어 목록을 받았다.
11+
// 이 언어의 순서를 찾으시오.
12+
public String alienOrder(String[] words) {
13+
14+
// wrt wrf => wr은 공통 t가 f보다 먼저 나왔으므로 t < f
15+
// wrt er => w보다 e가 뒤에 나왔으므로 w < e
16+
// er ett => r이 t보다 뒤에 나왔으므로 r < t
17+
// ett rftt => r이 e보다 뒤에 나왔으므로 e < r
18+
19+
Map<Character, Set<Character>> graph = new HashMap<>();
20+
Map<Character, Integer> char_order = new HashMap<>();
21+
22+
for (String word : words) {
23+
for (char c : word.toCharArray()) {
24+
graph.putIfAbsent(c, new HashSet<>());
25+
char_order.putIfAbsent(c, 0);
26+
}
27+
}
28+
29+
for (int i = 0; i < words.length - 1; i++) {
30+
String cur_word = words[i];
31+
String next_word = words[i + 1];
32+
33+
if (cur_word.length() > next_word.length() && cur_word.startsWith(next_word)) {
34+
return "";
35+
}
36+
37+
// 비교: (wrt, wrf) => t와 f 비교
38+
// 비교: (wrf, er) => w와 e 비교
39+
for (int j = 0; j < Math.min(cur_word.length(), next_word.length()); j++) {
40+
char c1 = cur_word.charAt(j);
41+
char c2 = next_word.charAt(j);
42+
43+
// from "wrt"and"wrf" ,we can get 't'<'f'
44+
// from "wrt"and"er" ,we can get 'w'<'e'
45+
// from "er"and"ett" ,we can get 'r'<'t'
46+
// from "ett"and"rftt" ,we can get 'e'<'r'
47+
// 순서 저장
48+
if (c1 != c2) {
49+
if (!graph.get(c1).contains(c2)) {
50+
graph.get(c1).add(c2);
51+
char_order.put(c2, char_order.get(c2) + 1);
52+
}
53+
break;
54+
}
55+
}
56+
}
57+
58+
Queue<Character> queue = new LinkedList<>();
59+
for (char c : char_order.keySet()) {
60+
// 최상위 부터 queue에 삽입 => w
61+
if (char_order.get(c) == 0) {
62+
queue.offer(c);
63+
}
64+
}
65+
66+
StringBuilder sb = new StringBuilder();
67+
while (!queue.isEmpty()) {
68+
char cur = queue.poll();
69+
sb.append(cur);
70+
71+
// 연관 글자들의 순서 조정 => w의 연관 글자들 => e, e와 연관 글자들 => t, t와 연관 글자들 => f ...
72+
for (char linked_char : graph.get(cur)) {
73+
char_order.put(linked_char, char_order.get(linked_char) - 1);
74+
if (char_order.get(linked_char) == 0) {
75+
queue.offer(linked_char);
76+
}
77+
}
78+
}
79+
80+
if (sb.length() != char_order.size()) {
81+
return "";
82+
}
83+
84+
return sb.toString();
85+
86+
}
87+
88+
}
89+
90+
91+
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
import java.util.HashMap;
2+
import java.util.Map;
3+
4+
/**
5+
* Definition for a binary tree node.
6+
* public class TreeNode {
7+
* int val;
8+
* TreeNode left;
9+
* TreeNode right;
10+
* TreeNode() {}
11+
* TreeNode(int val) { this.val = val; }
12+
* TreeNode(int val, TreeNode left, TreeNode right) {
13+
* this.val = val;
14+
* this.left = left;
15+
* this.right = right;
16+
* }
17+
* }
18+
*/
19+
class Solution {
20+
21+
private Map<Integer, Integer> inorderIndexMap;
22+
private int preorderIndex = 0;
23+
24+
public TreeNode buildTree(int[] preorder, int[] inorder) {
25+
26+
inorderIndexMap = new HashMap<>();
27+
for (int i = 0; i < inorder.length; i++) {
28+
inorderIndexMap.put(inorder[i], i);
29+
}
30+
31+
return buildTree(preorder, 0, inorder.length - 1);
32+
33+
}
34+
35+
private TreeNode buildTree(int[] preorder, int start, int end) {
36+
if (start > end) {
37+
return null;
38+
}
39+
40+
int rootVal = preorder[preorderIndex++];
41+
// root 생성
42+
TreeNode root = new TreeNode(rootVal);
43+
44+
int inorderIndex = inorderIndexMap.get(rootVal);
45+
46+
// 분할 정복 방식으로 좌측과 우측 트리에 대한 정보 탐색
47+
// preorder의 0번째 = head
48+
// inorder 기준으로 봤을 때
49+
// root의 좌측은 좌측 서브트리, 우측은 우측 서브트리
50+
// 좌우측 서브트리를 각각 분할해서 탐색 진행
51+
// 분할한 preorder의 첫번째 노드 = 해당 트리의 head
52+
// depth 0 => head 3, left side => [9], right side => [15, 20, 7]
53+
// depth 1 =>
54+
// left => head 9 => search node 더 이상 없음
55+
// right => head 20 => left side => [15], right side => [7]
56+
// depth 2 =>
57+
// left => head 15 => 더는 탐색할 노드 없음
58+
// right => head 7 => 더는 탐색할 노드 없음
59+
root.left = buildTree(preorder, start, inorderIndex - 1);
60+
root.right = buildTree(preorder, inorderIndex + 1, end);
61+
62+
return root;
63+
}
64+
}
65+
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
class Solution {
2+
3+
// 시간복잡도: O(N^2)
4+
public String longestPalindrome(String s) {
5+
6+
// 회문 구성 불가 경우
7+
if (s == null || s.length() < 1) {
8+
return "";
9+
}
10+
11+
String result = "";
12+
13+
for (int i = 0; i < s.length(); i++) {
14+
String odd = palindrome(s, i, i);
15+
String even = palindrome(s, i, i + 1);
16+
17+
if (odd.length() > result.length()) {
18+
result = odd;
19+
}
20+
if (even.length() > result.length()) {
21+
result = even;
22+
}
23+
24+
}
25+
26+
return result;
27+
28+
}
29+
30+
private String palindrome(String s, int left, int right) {
31+
32+
// 중앙을 기준으로 좌우 회문 여부 검사
33+
while (left >= 0 && right < s.length() && s.charAt(left) == s.charAt(right)) {
34+
left--;
35+
right++;
36+
}
37+
38+
return s.substring(left + 1, right);
39+
}
40+
41+
}
42+

rotate-image/Tessa1217.java

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
class Solution {
2+
3+
// 공간복잡도 개선 및 별도의 방문 배열 처리 등 하지 않기 위해
4+
// 2nd solution
5+
public void rotate(int[][] matrix) {
6+
7+
int n = matrix.length;
8+
9+
// 행, 열 자리 바꾸기
10+
for (int i = 0; i < n; i++) {
11+
for (int j = i + 1; j < n; j++) {
12+
int temp = matrix[i][j];
13+
matrix[i][j] = matrix[j][i];
14+
matrix[j][i] = temp;
15+
}
16+
}
17+
18+
// 열 자리 swap 하기
19+
// ex) 3 X 3 => 0 <-> 2 열 스왑
20+
// ex) 4 x 4 => 0 <-> 3, 1 <-> 2 스왑
21+
for (int i = 0; i < n; i++) {
22+
for (int j = 0; j < n / 2; j++) {
23+
int temp = matrix[i][j];
24+
matrix[i][j] = matrix[i][n - 1 - j];
25+
matrix[i][n - 1 - j] = temp;
26+
}
27+
}
28+
}
29+
30+
31+
// public void rotate(int[][] matrix) {
32+
33+
// [0, 0], [0, 1], [0, 2] [2, 0], [1, 0], [0, 0]
34+
// [1, 0], [1, 1], [1, 2] [2, 1], [1, 1], [0, 1]
35+
// [2, 0], [2, 1], [2, 2] [2, 2], [2, 1], [0, 2]
36+
37+
38+
// [0, 0], [0, 1], [0, 2], [0, 3] [3, 0], [2, 0], [1, 0], [0, 0]
39+
// [1, 0], [1, 1], [1, 2], [1, 3] [3, 1], [2, 1], [1, 1], [0, 1]
40+
// [2, 0], [2, 1], [2, 2], [2, 3] [3, 2], [2, 2], [1, 2], [0, 2]
41+
// [3, 0], [3, 1], [3, 2], [3, 3] [3, 3], [2, 3], [1, 3], [0, 3]
42+
43+
// 90D =>
44+
// n = matrix.length이고 m = matrix[0].length일 때
45+
// 1 => 3 matrix[0][0] => matrix[0][2]
46+
// 2 => 6 matrix[0][1] => matrix[1][2]
47+
// 3 => 9 matrix[0][2] => matrix[2][2]
48+
// matrix[i][j] => matrix[j][n - 1 - i]
49+
50+
// int n = matrix.length;
51+
// int m = matrix[0].length;
52+
53+
// boolean[][] visited = new boolean[n][m];
54+
55+
// for (int i = 0; i < n; i++) {
56+
// for (int j = 0; j < m; j++) {
57+
// if (visited[i][j]) {
58+
// continue;
59+
// }
60+
// int temp = matrix[i][j];
61+
// matrix[i][j] = matrix[j][n - 1 - i];
62+
// matrix[j][n - 1 - i] = temp;
63+
64+
// visited[i][j] = true;
65+
// visited[j][n - 1 - i] = true;
66+
// }
67+
// }
68+
// }
69+
}
70+
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
/**
2+
* Definition for a binary tree node.
3+
* public class TreeNode {
4+
* int val;
5+
* TreeNode left;
6+
* TreeNode right;
7+
* TreeNode() {}
8+
* TreeNode(int val) { this.val = val; }
9+
* TreeNode(int val, TreeNode left, TreeNode right) {
10+
* this.val = val;
11+
* this.left = left;
12+
* this.right = right;
13+
* }
14+
* }
15+
*/
16+
class Solution {
17+
public boolean isSubtree(TreeNode root, TreeNode subRoot) {
18+
// subtree null일 경우
19+
if (subRoot == null) {
20+
return true;
21+
}
22+
// 이진 트리가 null일 경우
23+
if (root == null) {
24+
return false;
25+
}
26+
// subtree라면
27+
if (isIdentical(root, subRoot)) {
28+
return true;
29+
}
30+
31+
return isSubtree(root.left, subRoot) || isSubtree(root.right, subRoot);
32+
33+
}
34+
35+
// isIdentical(s,t)= s.val==t.val AND isIdentical(s.left,t.left) AND isIdentical(s.right,t.right)
36+
private boolean isIdentical(TreeNode r, TreeNode s) {
37+
if (r == null || s == null) {
38+
return r == null && s == null;
39+
}
40+
if (r.val != s.val) {
41+
return false;
42+
}
43+
return isIdentical(r.left, s.left) && isIdentical(r.right, s.right);
44+
}
45+
}
46+

0 commit comments

Comments
 (0)