Skip to content

Commit 6db9bab

Browse files
authored
Merge pull request #1106 from forest000014/main
[forest000014] Week 15
2 parents e858b39 + 3f0a1a5 commit 6db9bab

File tree

11 files changed

+541
-0
lines changed

11 files changed

+541
-0
lines changed
+111
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
/*
2+
# Time Complexity: O(wl)
3+
- w์€ words์˜ ๊ธธ์ด, l์€ words[i]์˜ ๊ธธ์ด
4+
- word ํ•˜๋‚˜๋ฅผ trie์— ๋“ฑ๋กํ•˜๋Š” ๊ณผ์ • O(l)
5+
- ๋ชจ๋“  word๋ฅผ trie์— ๋“ฑ๋กํ•˜๋Š” ๊ณผ์ • O(wl)
6+
- graph๋ฅผ ์ˆœํšŒํ•˜๋ฉด์„œ ์œ„์ƒ์ •๋ ฌํ•˜๋Š” ๊ณผ์ • O(26^2) = O(1)
7+
# Space Complexity: O(wl)
8+
- trie์˜ ๊ณต๊ฐ„๋ณต์žก๋„ O(wl)
9+
*/
10+
11+
class Solution {
12+
13+
private class TrieNode {
14+
char ch;
15+
boolean ends;
16+
List<TrieNode> children;
17+
18+
TrieNode(char ch) {
19+
this.ch = ch;
20+
children = new ArrayList<>();
21+
}
22+
}
23+
24+
public String alienOrder(String[] words) {
25+
List<List<Integer>> graph = new ArrayList<>();
26+
for (int i = 0; i < 26; i++) {
27+
graph.add(new ArrayList<>());
28+
}
29+
boolean[] visited = new boolean[26];
30+
int[] inDegree = new int[26];
31+
int[] outDegree = new int[26];
32+
Queue<Character> queue = new LinkedList<>();
33+
34+
TrieNode root = new TrieNode('.');
35+
36+
for (int i = 0; i < words.length; i++) {
37+
TrieNode curr = root;
38+
39+
for (int j = 0; j < words[i].length(); j++) {
40+
// ์œ ํšจํ•œ ์ˆœ์„œ๊ฐ€ ์•„๋‹˜์ด ํ™•์‹คํ•˜๋ฉด, ๊ณง๋ฐ”๋กœ false๋ฅผ ๋ฆฌํ„ดํ•œ๋‹ค.
41+
// ์œ ํšจํ•œ ์ˆœ์„œ๊ฐ€ ์•„๋‹˜์ด ํ™•์‹คํ•˜์ง€ ์•Š์œผ๋ฉด, trie์— ์ถ”๊ฐ€ํ•˜๊ณ , relation์„ ์ถ”๊ฐ€ํ•œ๋‹ค.
42+
// ๋‹จ, words[i]์˜ ๋งˆ์ง€๋ง‰ ๊ธ€์ž๋ผ๋ฉด, trie์˜ ๋งˆ์ง€๋ง‰์— ends = true๋ฅผ ์„ธํŒ…ํ•œ๋‹ค.
43+
44+
char ch = words[i].charAt(j);
45+
visited[ch - 'a'] = true;
46+
47+
if (curr.children.size() == 0) {
48+
curr.children.add(new TrieNode(ch));
49+
curr = curr.children.get(curr.children.size() - 1);
50+
if (j == words[i].length()) curr.ends = true;
51+
continue;
52+
}
53+
54+
char lastCh = curr.children.get(curr.children.size() - 1).ch;
55+
if (lastCh == ch) {
56+
curr = curr.children.get(curr.children.size() - 1);
57+
if (j == words[i].length() - 1) {
58+
if (!curr.children.isEmpty()) return "";
59+
else curr.ends = true;
60+
}
61+
continue;
62+
}
63+
64+
for (int p = 0; p < curr.children.size() - 1; p++) {
65+
if (curr.children.get(p).ch == ch) return "";
66+
}
67+
68+
addEdge(graph, inDegree, outDegree, lastCh, ch);
69+
curr.children.add(new TrieNode(ch));
70+
curr = curr.children.get(curr.children.size() - 1);
71+
}
72+
}
73+
74+
for (int i = 0; i < 26; i++) {
75+
if (inDegree[i] == 0 && outDegree[i] != 0) queue.offer((char)('a' + i));
76+
}
77+
78+
StringBuilder sb = new StringBuilder();
79+
80+
for (int i = 0; i < 26; i++) {
81+
if (visited[i] && inDegree[i] == 0 && outDegree[i] == 0) sb.append((char)('a' + i));
82+
}
83+
84+
while (!queue.isEmpty()) {
85+
char ch = queue.poll();
86+
sb.append(ch);
87+
88+
for (int next : graph.get(ch - 'a')) {
89+
if (--inDegree[next] == 0) queue.offer((char)('a' + next));
90+
}
91+
}
92+
93+
for (int i = 0; i < 26; i++) {
94+
if (inDegree[i] > 0) return "";
95+
}
96+
97+
return sb.toString();
98+
}
99+
100+
private boolean addEdge(List<List<Integer>> graph, int[] inDegree, int[] outDegree, char from, char to) {
101+
int sz = graph.get(from - 'a').size();
102+
for (int i = 0; i < sz; i++) {
103+
if (graph.get(from - 'a').get(i) == to - 'a') return false;
104+
}
105+
106+
graph.get(from - 'a').add(to - 'a');
107+
outDegree[from - 'a']++;
108+
inDegree[to - 'a']++;
109+
return true;
110+
}
111+
}

โ€Žcourse-schedule/forest000014.java

+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/*
2+
# Time Complexity: O(n)
3+
# Space Complexity: O(n)
4+
์œ„์ƒ ์ •๋ ฌ์„ ์‚ฌ์šฉํ•ด์„œ ํ’€์—ˆ์Šต๋‹ˆ๋‹ค.
5+
*/
6+
7+
class Solution {
8+
public boolean canFinish(int numCourses, int[][] prerequisites) {
9+
int[] inDegree = new int[numCourses];
10+
Queue<Integer> queue = new LinkedList<>();
11+
List<List<Integer>> graph = new ArrayList<>();
12+
for (int i = 0; i < numCourses; i++) {
13+
graph.add(new ArrayList<>());
14+
}
15+
16+
for (int i = 0; i < prerequisites.length; i++) {
17+
graph.get(prerequisites[i][1]).add(prerequisites[i][0]);
18+
inDegree[prerequisites[i][0]]++;
19+
}
20+
21+
for (int i = 0; i < numCourses; i++) {
22+
if (inDegree[i] == 0) queue.offer(i);
23+
}
24+
25+
while (!queue.isEmpty()) {
26+
int curr = queue.poll();
27+
28+
for (int next : graph.get(curr)) {
29+
inDegree[next]--;
30+
if (inDegree[next] == 0) queue.offer(next);
31+
}
32+
}
33+
34+
for (int i = 0; i < numCourses; i++) {
35+
if (inDegree[i] > 0) return false;
36+
}
37+
return true;
38+
}
39+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/*
2+
# Time Complexity: O(n^2)
3+
# Spcae Complexity: O(1)
4+
*/
5+
class Solution {
6+
public String longestPalindrome(String s) {
7+
String ans = "";
8+
for (int i = 0; i < s.length(); i++) {
9+
int l = i;
10+
int r = i;
11+
12+
while (l >= 0 && r < s.length() && s.charAt(l) == s.charAt(r)) {
13+
if (r - l + 1 > ans.length()) ans = s.substring(l, r + 1);
14+
l--;
15+
r++;
16+
}
17+
18+
l = i;
19+
r = i + 1;
20+
while (l >= 0 && r < s.length() && s.charAt(l) == s.charAt(r)) {
21+
if (r - l + 1 > ans.length()) ans = s.substring(l, r + 1);
22+
l--;
23+
r++;
24+
}
25+
}
26+
27+
return ans;
28+
}
29+
}
+50
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
/*
2+
# Time Complexity: O(nlogk)
3+
- n์€ lists[i].length์˜ ํ•ฉ
4+
# Space Complexity: O(k)
5+
- pq์—๋Š” ์ตœ๋Œ€ k๊ฐœ์˜ ์›์†Œ๊ฐ€ ์ €์žฅ๋จ
6+
*/
7+
/**
8+
* Definition for singly-linked list.
9+
* public class ListNode {
10+
* int val;
11+
* ListNode next;
12+
* ListNode() {}
13+
* ListNode(int val) { this.val = val; }
14+
* ListNode(int val, ListNode next) { this.val = val; this.next = next; }
15+
* }
16+
*/
17+
class Solution {
18+
19+
public ListNode mergeKLists(ListNode[] lists) {
20+
if (lists.length == 0) return null;
21+
22+
PriorityQueue<ListNode> pq = new PriorityQueue<>(new Comparator<ListNode>() {
23+
@Override
24+
public int compare(ListNode n1, ListNode n2) {
25+
return n1.val - n2.val;
26+
}
27+
});
28+
for (int i = 0; i < lists.length; i++) {
29+
if (lists[i] == null) continue;
30+
pq.offer(lists[i]);
31+
}
32+
33+
ListNode head = next(pq);
34+
ListNode curr = head;
35+
36+
while (!pq.isEmpty()) {
37+
curr.next = next(pq);
38+
curr = curr.next;
39+
}
40+
41+
return head;
42+
}
43+
44+
private ListNode next(PriorityQueue<ListNode> pq) {
45+
ListNode top = pq.poll();
46+
if (top == null) return null;
47+
if (top.next != null) pq.offer(top.next);
48+
return top;
49+
}
50+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
/*
2+
# Time Complexity: O(m * c)
3+
- c๋Š” ๋ฌธ์ž์—ด์— ์‚ฌ์šฉ๋œ ๋ฌธ์ž์˜ ๊ฐ€์ง“์ˆ˜(= ์†Œ๋ฌธ์ž 26 + ๋Œ€๋ฌธ์ž 26)
4+
- ์Šฌ๋ผ์ด๋”ฉ ์œˆ๋„์šฐ๋กœ s ์ „์ฒด๋ฅผ ํ›‘๋Š”๋ฐ O(m)์ด ํ•„์š”ํ•˜๊ณ , ๊ฐ ์œˆ๋„์šฐ๋งˆ๋‹ค t์˜ ์ „์ฒด ๋ฌธ์ž๊ฐ€ ํฌํ•จ๋˜์–ด ์žˆ๋Š”์ง€ ํŒ๋‹จํ•˜๋Š”๋ฐ O(c)๊ฐ€ ํ•„์š”ํ•จ
5+
# Space Complexity: O(c)
6+
- sMap(์Šฌ๋ผ์ด๋”ฉ ์œˆ๋„์šฐ์— ํฌํ•จ๋œ ๋ฌธ์ž ์นด์šดํŠธ), tMap(๋ฌธ์ž์—ด t์— ํฌํ•จ๋œ ๋ฌธ์ž ์นด์šดํŠธ) ๊ฐ๊ฐ O(c)
7+
*/
8+
class Solution {
9+
public String minWindow(String s, String t) {
10+
Map<Character, Integer> sMap = new HashMap<>();
11+
Map<Character, Integer> tMap = new HashMap<>();
12+
for (int i = 0; i < t.length(); i++) {
13+
tMap.merge(t.charAt(i), 1, Integer::sum);
14+
}
15+
16+
String ans = "";
17+
int ansLen = Integer.MAX_VALUE;
18+
int l = 0;
19+
int r = 0;
20+
sMap.merge(s.charAt(0), 1, Integer::sum);
21+
while (l <= r) {
22+
if (included(sMap, tMap)) {
23+
if (r - l + 1 < ansLen) {
24+
ansLen = r - l + 1;
25+
ans = s.substring(l, r + 1);
26+
}
27+
sMap.merge(s.charAt(l), -1, Integer::sum);
28+
l++;
29+
} else {
30+
r++;
31+
if (r >= s.length()) break;
32+
sMap.merge(s.charAt(r), 1, Integer::sum);
33+
}
34+
}
35+
36+
while (l <= r && included(sMap, tMap)) {
37+
if (r - l + 1 > ansLen) {
38+
ansLen = r - l + 1;
39+
ans = s.substring(l, r + 1);
40+
}
41+
sMap.merge(s.charAt(l), -1, Integer::sum);
42+
l++;
43+
}
44+
45+
return ans;
46+
}
47+
48+
private boolean included(Map<Character, Integer> sMap, Map<Character, Integer> tMap) {
49+
for (Character key : tMap.keySet()) {
50+
if (!sMap.containsKey(key) || sMap.get(key) < tMap.get(key)) return false;
51+
}
52+
return true;
53+
}
54+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
/*
2+
# Time Complexity: O(m * n * log(m * n))
3+
# Space Complexity: O(m * n)
4+
- visited, pq
5+
DFS์™€ PQ๋ฅผ ์กฐํ•ฉํ•˜์—ฌ ํ’€์—ˆ์Šต๋‹ˆ๋‹ค.
6+
*/
7+
class Solution {
8+
private class Cell {
9+
int r;
10+
int c;
11+
int h;
12+
13+
Cell(int r, int c, int h) {
14+
this.r = r;
15+
this.c = c;
16+
this.h = h;
17+
}
18+
}
19+
20+
public List<List<Integer>> pacificAtlantic(int[][] heights) {
21+
int m = heights.length;
22+
int n = heights[0].length;
23+
PriorityQueue<Cell> pq1 = new PriorityQueue<>(new Comparator<Cell>() {
24+
@Override
25+
public int compare(Cell c1, Cell c2) {
26+
return c1.h - c2.h;
27+
}
28+
});
29+
PriorityQueue<Cell> pq2 = new PriorityQueue<>(new Comparator<Cell>() {
30+
@Override
31+
public int compare(Cell c1, Cell c2) {
32+
return c1.h - c2.h;
33+
}
34+
});
35+
int[][] visited = new int[m][n];
36+
37+
for (int i = 0; i < m; i++) {
38+
pq1.offer(new Cell(i, 0, heights[i][0]));
39+
pq2.offer(new Cell(i, n - 1, heights[i][n - 1]));
40+
visited[i][0] |= 1;
41+
visited[i][n - 1] |= 2;
42+
}
43+
for (int i = 1; i < n; i++) {
44+
pq1.offer(new Cell(0, i, heights[0][i]));
45+
pq2.offer(new Cell(m - 1, i - 1, heights[m - 1][i - 1]));
46+
visited[0][i] |= 1;
47+
visited[m - 1][i - 1] |= 2;
48+
}
49+
50+
int[] dr = {-1, 0, 1, 0};
51+
int[] dc = {0, 1, 0, -1};
52+
while (!pq1.isEmpty()) {
53+
Cell curr = pq1.poll();
54+
for (int i = 0; i < 4; i++) {
55+
int nr = curr.r + dr[i];
56+
int nc = curr.c + dc[i];
57+
if (nr < 0 || nr >= m || nc < 0 || nc >= n || heights[nr][nc] < heights[curr.r][curr.c] || (visited[nr][nc] & 1) == 1) continue;
58+
pq1.offer(new Cell(nr, nc, heights[nr][nc]));
59+
visited[nr][nc] |= 1;
60+
}
61+
}
62+
while (!pq2.isEmpty()) {
63+
Cell curr = pq2.poll();
64+
for (int i = 0; i < 4; i++) {
65+
int nr = curr.r + dr[i];
66+
int nc = curr.c + dc[i];
67+
if (nr < 0 || nr >= m || nc < 0 || nc >= n || heights[nr][nc] < heights[curr.r][curr.c] || (visited[nr][nc] & 2) == 2) continue;
68+
pq2.offer(new Cell(nr, nc, heights[nr][nc]));
69+
visited[nr][nc] |= 2;
70+
}
71+
}
72+
73+
List<List<Integer>> ans = new ArrayList<>();
74+
for (int i = 0; i < m; i++) {
75+
for (int j = 0; j < n; j++) {
76+
if (visited[i][j] == 3) {
77+
ans.add(new ArrayList<>(List.of(i, j)));
78+
}
79+
}
80+
}
81+
82+
return ans;
83+
}
84+
}

โ€Žrotate-image/forest000014.java

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/*
2+
# Time Complexity: O(n^2)
3+
# Space Complexity: O(1)
4+
*/
5+
class Solution {
6+
public void rotate(int[][] matrix) {
7+
int n = matrix.length;
8+
9+
for (int i = 0; i < n / 2; i++) {
10+
for (int j = i; j < n - i - 1; j++) {
11+
// [i][j] -> [j][n - i - 1]
12+
// [j][n - i - 1] -> [n - i - 1][n - j - 1]
13+
// [n - i - 1][n - j - 1] -> [n - j - 1][i]
14+
// [n - j - 1][i] -> [i][j]
15+
// (๊ฐ ์ธ๋ฑ์Šค์˜ ๋“ฑ์žฅ ํšŸ์ˆ˜๋ฅผ ์ฒดํฌํ•ด์„œ, ๊ฐ๊ฐ์˜ ๋“ฑ์žฅํšŸ์ˆ˜๊ฐ€ 4๋ฒˆ์ž„์„ ํ™•์ธํ•˜๋ฉด, ํ‹€๋ฆฐ ์ธ๋ฑ์Šค๊ฐ€ ์•„๋‹˜์„ quickํ•˜๊ฒŒ ์ฒดํฌํ•ด๋ณผ ์ˆ˜๋Š” ์žˆ์Œ. ์ด ๋ฐฉ๋ฒ•์œผ๋กœ ๋งž๋‹ค๋Š” ๋ณด์žฅ์€ ์•ˆ ๋จ.)
16+
17+
int tmp = matrix[i][j];
18+
matrix[i][j] = matrix[n - j - 1][i];
19+
matrix[n - j - 1][i] = matrix[n - i - 1][n - j - 1];
20+
matrix[n - i - 1][n - j - 1] = matrix[j][n - i - 1];
21+
matrix[j][n - i - 1] = tmp;
22+
}
23+
}
24+
}
25+
}

0 commit comments

Comments
ย (0)