Skip to content

Commit 0df10ef

Browse files
authored
Merge pull request #2165 from se6816/main
[se6816] WEEK 05 Solutions
2 parents bc39741 + c2c0227 commit 0df10ef

File tree

4 files changed

+283
-0
lines changed

4 files changed

+283
-0
lines changed
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
2+
/**
3+
dp를 이용한 방식?
4+
prices의 길이 -> N
5+
시간복잡도 : O(N^2) -> 시간 초과
6+
공간복잡도 : O(N)
7+
*/
8+
class Solution2 {
9+
public int maxProfit(int[] prices) {
10+
int[] dp =new int[prices.length];
11+
for(int i = 0; i < dp.length; i++) {
12+
for(int j = 0; j < i; j++) {
13+
dp[i] = Math.max(dp[i], prices[i] - prices[j]);
14+
}
15+
}
16+
17+
return Arrays.stream(dp)
18+
.max()
19+
.getAsInt();
20+
}
21+
}
22+
23+
/**
24+
이전 연산 값을 기억할 필요 없이 특정 인덱스 지점까지의 최소 값만 알면 되므로,
25+
26+
prices의 길이 -> N
27+
시간복잡도 : O(N)
28+
공간복잡도 : O(1)
29+
*/
30+
class Solution {
31+
public int maxProfit(int[] prices) {
32+
int min=prices[0];
33+
int profit=0;
34+
for(int i=1; i<prices.length; i++){
35+
if(prices[i] < min){
36+
min=prices[i];
37+
continue;
38+
}
39+
profit=Math.max(profit, prices[i] - min);
40+
}
41+
return profit;
42+
}
43+
}

group-anagrams/se6816.java

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/**
2+
Map을 통해 사전순으로 정렬된 문자열의 집합을 구한 뒤 그 집합들을 리턴하는 방식
3+
strs의 길이 -> N
4+
시간 복잡도 : O(N)
5+
공간 복잡도 : O(N)
6+
*/
7+
class Solution {
8+
public List<List<String>> groupAnagrams(String[] strs) {
9+
List<List<String>> result = new ArrayList<>();
10+
Map<String, List<String>> hashMap = new HashMap<>();
11+
for(int i=0; i<strs.length;i++){
12+
char[] charArray = strs[i].toCharArray();
13+
Arrays.sort(charArray);
14+
String word = new String(charArray);
15+
List<String> list = hashMap.getOrDefault(word, new ArrayList<>());
16+
list.add(strs[i]);
17+
hashMap.put(word,list);
18+
}
19+
for(Map.Entry<String, List<String>> entry : hashMap.entrySet()){
20+
List<String> list = entry.getValue();
21+
result.add(list);
22+
}
23+
return result;
24+
}
25+
}
26+
27+
28+
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
/**
2+
연결 리스트를 통해, 트리 구조를 만들고 탐색하는 방식
3+
*/
4+
class Trie {
5+
public Map<Character, WordNode> wordMap;
6+
7+
public Trie() {
8+
wordMap = new HashMap<>();
9+
}
10+
11+
public void insert(String word) {
12+
WordNode wordNode = null;
13+
char ch = word.charAt(0);
14+
wordNode = wordMap.get(ch);
15+
16+
if(wordNode == null) {
17+
boolean isFirstWord = word.length() == 1;
18+
wordNode = new WordNode(ch, isFirstWord);
19+
wordMap.put(ch, wordNode);
20+
}
21+
22+
for(int idx = 1; idx < word.length(); idx++) {
23+
char target = word.charAt(idx);
24+
boolean isLeaf = word.length() - 1 == idx;
25+
wordNode = wordNode.next.computeIfAbsent(target, key -> new WordNode(key, isLeaf));
26+
}
27+
wordNode.isLeaf = true;
28+
}
29+
30+
public boolean search(String word) {
31+
32+
WordNode wordNode = null;
33+
char ch = word.charAt(0);
34+
wordNode = wordMap.get(ch);
35+
if (wordNode == null) return false;
36+
37+
38+
for(int idx = 1; idx < word.length(); idx++) {
39+
char target = word.charAt(idx);
40+
if (!wordNode.next.containsKey(target)) {
41+
return false;
42+
}
43+
wordNode = wordNode.next.get(target);
44+
}
45+
46+
return wordNode.isLeaf;
47+
}
48+
49+
public boolean startsWith(String word) {
50+
51+
WordNode wordNode = null;
52+
char ch = word.charAt(0);
53+
wordNode = wordMap.get(ch);
54+
if (wordNode == null) return false;
55+
56+
57+
for(int idx = 1; idx < word.length(); idx++) {
58+
char target = word.charAt(idx);
59+
if (!wordNode.next.containsKey(target)) {
60+
return false;
61+
}
62+
wordNode = wordNode.next.get(target);
63+
}
64+
65+
return true;
66+
}
67+
}
68+
69+
class WordNode {
70+
char ch;
71+
Map<Character, WordNode> next;
72+
boolean isLeaf;
73+
74+
public WordNode(char ch) {
75+
this(ch, false);
76+
}
77+
78+
public WordNode(char ch, boolean isLeaf) {
79+
next = new HashMap<>();
80+
this.ch = ch;
81+
this.isLeaf = isLeaf;
82+
}
83+
84+
}
85+
86+
/**
87+
* Your Trie object will be instantiated and called as such:
88+
* Trie obj = new Trie();
89+
* obj.insert(word);
90+
* boolean param_2 = obj.search(word);
91+
* boolean param_3 = obj.startsWith(prefix);
92+
*/

word-break/se6816.java

Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
/**
2+
Tries 자료구조를 구현하고, 이를 통해 단어를 탐색하는 방식
3+
visited[] 방문 배열을 추가하여, 이전에 방문했던 인덱스에 대해서는 무시
4+
*/
5+
class WordMap {
6+
public Map<Character, WordNode> wordMap;
7+
8+
public WordMap() {
9+
wordMap = new HashMap<>();
10+
}
11+
12+
public List<Integer> search(String word, int idx) {
13+
List<Integer> idxList = new ArrayList<>();
14+
15+
WordNode wordNode = null;
16+
char ch = word.charAt(idx);
17+
wordNode = wordMap.get(ch);
18+
if (wordNode == null) return idxList;
19+
20+
if(wordNode.isLeaf) {
21+
idxList.add(idx + 1);
22+
}
23+
idx++;
24+
25+
for(; idx < word.length(); idx++) {
26+
char target = word.charAt(idx);
27+
if (!wordNode.next.containsKey(target)) {
28+
break;
29+
}
30+
wordNode = wordNode.next.get(target);
31+
if (wordNode.isLeaf) {
32+
idxList.add(idx + 1);
33+
}
34+
}
35+
36+
return idxList;
37+
}
38+
39+
public void add(String word) {
40+
WordNode wordNode = null;
41+
char ch = word.charAt(0);
42+
wordNode = wordMap.get(ch);
43+
44+
if(wordNode == null) {
45+
boolean isFirstWord = word.length() == 1;
46+
wordNode = new WordNode(ch, isFirstWord);
47+
wordMap.put(ch, wordNode);
48+
}
49+
50+
for(int idx = 1; idx < word.length(); idx++) {
51+
char target = word.charAt(idx);
52+
boolean isLeaf = word.length() - 1 == idx;
53+
wordNode = wordNode.next.computeIfAbsent(target, key -> new WordNode(key, isLeaf));
54+
}
55+
56+
wordNode.isLeaf = true;
57+
58+
}
59+
60+
}
61+
62+
class WordNode {
63+
char ch;
64+
Map<Character, WordNode> next;
65+
boolean isLeaf;
66+
67+
public WordNode(char ch) {
68+
this(ch, false);
69+
}
70+
71+
public WordNode(char ch, boolean isLeaf) {
72+
next = new HashMap<>();
73+
this.ch = ch;
74+
this.isLeaf = isLeaf;
75+
}
76+
77+
}
78+
79+
class Solution {
80+
public static WordMap wordMap;
81+
public boolean wordBreak(String s, List<String> wordDict) {
82+
boolean[] visited = new boolean[s.length()];
83+
initWordMap(wordDict);
84+
if(s.length() == 1) return wordMap.wordMap.containsKey(s.charAt(0)) && wordMap.wordMap.get(s.charAt(0)).isLeaf;
85+
86+
Queue<Integer> que = new LinkedList<>();
87+
boolean result = false;
88+
que.add(0);
89+
visited[0] = true;
90+
loop:
91+
while(!que.isEmpty()) {
92+
int idx = que.poll();
93+
List<Integer> idxList = wordMap.search(s, idx);
94+
for(int i : idxList) {
95+
if(i == s.length()) {
96+
result = true;
97+
break loop;
98+
}
99+
100+
if(!visited[i]) {
101+
que.add(i);
102+
visited[i] = true;
103+
}
104+
}
105+
}
106+
107+
return result;
108+
109+
110+
111+
}
112+
113+
public void initWordMap(List<String> wordDict) {
114+
wordMap = new WordMap();
115+
for(String word : wordDict) {
116+
wordMap.add(word);
117+
}
118+
}
119+
}
120+

0 commit comments

Comments
 (0)