Skip to content

Commit ab500f9

Browse files
committed
2 parents 1ff586c + 47c681e commit ab500f9

9 files changed

Lines changed: 597 additions & 0 deletions

File tree

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+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/**
2+
브루트포스를 통해 모든 경우의 수를 연산하여 찾는 방식
3+
배열 height의 길이 -> N
4+
시간 복잡도 : O(N^2) -> 시간 초과
5+
공간 복잡도 : O(1)
6+
*/
7+
class FailSolution {
8+
public int maxArea(int[] height) {
9+
int max=0;
10+
int area=0;
11+
for(int i=0;i<height.length;i++){
12+
for(int j=i+1;j<height.length;j++){
13+
area= Math.max(area,Math.min(height[j],height[i])*(j-i));
14+
if(area>max){
15+
max=area;
16+
}
17+
}
18+
}
19+
return max;
20+
}
21+
}
22+
23+
/**
24+
투포인터를 활용하여, 낮은 높이의 포인터를 갱신하면서 최대 넓이를 구하는 방식
25+
배열 height의 길이 -> N
26+
시간 복잡도 : O(N)
27+
공간 복잡도 : O(1)
28+
*/
29+
class Solution {
30+
public int maxArea(int[] height) {
31+
int left = 0;
32+
int right = height.length - 1;
33+
int max = 0;
34+
while(left < right) {
35+
int h = Math.min(height[right], height[left]);
36+
int w = right - left;
37+
max = Math.max(max, w * h);
38+
39+
if(h == height[right]) {
40+
right--;
41+
} else {
42+
left++;
43+
}
44+
}
45+
return max;
46+
}
47+
}
Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
/**
2+
Tries + BFS를 활용한 방식
3+
addWord()
4+
문자열 word의 길이 -> N
5+
시간 복잡도 : O(N)
6+
공간 복잡도 : O(N)
7+
search()
8+
Tries 내부의 노드 개수 -> M
9+
시간 복잡도 : O(M)
10+
공간 복잡도 : O(M)
11+
*/
12+
class WordDictionary {
13+
public Map<Character, WordNode> wordMap;
14+
15+
public WordDictionary() {
16+
wordMap = new HashMap<>();
17+
}
18+
19+
public void addWord(String word) {
20+
WordNode wordNode = null;
21+
char ch = word.charAt(0);
22+
wordNode = wordMap.get(ch);
23+
24+
if(wordNode == null) {
25+
boolean isFirstWord = word.length() == 1;
26+
wordNode = new WordNode(ch, isFirstWord);
27+
wordMap.put(ch, wordNode);
28+
}
29+
30+
for(int idx = 1; idx < word.length(); idx++) {
31+
char target = word.charAt(idx);
32+
boolean isLeaf = word.length() - 1 == idx;
33+
wordNode = wordNode.next.computeIfAbsent(target, key -> new WordNode(key, isLeaf));
34+
}
35+
wordNode.isLeaf = true;
36+
37+
38+
}
39+
40+
public boolean search(String word) {
41+
Queue<Node> que = new ArrayDeque<>();
42+
boolean result = false;
43+
char ch = word.charAt(0);
44+
WordNode wordNode = wordMap.get(ch);
45+
int len = word.length();
46+
47+
if(ch == '.' && wordMap.size() != 0) {
48+
for(Map.Entry<Character, WordNode> entry : wordMap.entrySet()) {
49+
que.add(new Node(entry.getValue(), 1));
50+
}
51+
}
52+
53+
if (wordNode != null) {
54+
que.add(new Node(wordNode, 1));
55+
}
56+
57+
58+
59+
while(!que.isEmpty()) {
60+
Node node = que.poll();
61+
if(node.idx == len && node.wordNode.isLeaf) {
62+
result = true;
63+
break;
64+
}
65+
66+
if(node.idx == len) {
67+
continue;
68+
}
69+
70+
char target = word.charAt(node.idx);
71+
72+
if(target == '.' && node.wordNode.next.size() != 0) {
73+
for(Map.Entry<Character, WordNode> entry : node.wordNode.next.entrySet()) {
74+
que.add(new Node(entry.getValue(), node.idx + 1));
75+
}
76+
continue;
77+
}
78+
79+
80+
if (!node.wordNode.next.containsKey(target)) {
81+
continue;
82+
}
83+
84+
que.add(new Node(node.wordNode.next.get(target), node.idx + 1));
85+
}
86+
87+
return result;
88+
89+
}
90+
}
91+
92+
class Node {
93+
WordNode wordNode;
94+
int idx;
95+
public Node(WordNode wordNode, int idx) {
96+
this.wordNode = wordNode;
97+
this.idx = idx;
98+
}
99+
}
100+
101+
class WordNode {
102+
char ch;
103+
Map<Character, WordNode> next;
104+
boolean isLeaf;
105+
106+
public WordNode(char ch) {
107+
this(ch, false);
108+
}
109+
110+
public WordNode(char ch, boolean isLeaf) {
111+
next = new HashMap<>();
112+
this.ch = ch;
113+
this.isLeaf = isLeaf;
114+
}
115+
116+
}

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+
*/
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
/**
2+
dp를 활용한 방식
3+
nums의 길이 -> N
4+
시간 복잡도 : O(N)
5+
공간 복잡도 : O(N)
6+
*/
7+
class Solution {
8+
public int lengthOfLIS(int[] nums) {
9+
int[] dp =new int[nums.length];
10+
for(int i = 0; i < dp.length; i++) {
11+
for(int j = 0; j < i; j++) {
12+
if(nums[i] > nums[j]) {
13+
dp[i] = Math.max(dp[i], dp[j] + 1);
14+
}
15+
}
16+
}
17+
return Arrays.stream(dp)
18+
.max()
19+
.getAsInt() + 1;
20+
}
21+
}
22+
23+
/**
24+
이분탐색 활용한 방식
25+
nums의 길이 -> N
26+
시간 복잡도 : O(NlogN)
27+
공간 복잡도 : O(N)
28+
*/
29+
class Solution {
30+
public int[] list;
31+
public int lengthOfLIS(int[] nums) {
32+
int result = 0;
33+
list =new int[nums.length];
34+
Arrays.fill(list, Integer.MAX_VALUE);
35+
for(int i = 0; i < list.length; i++) {
36+
int idx = binarySearch(list, nums[i]);
37+
list[idx] = nums[i];
38+
result = Math.max(result, idx + 1);
39+
40+
}
41+
return result;
42+
}
43+
44+
public int binarySearch(int[] list, int target) {
45+
int start = 0;
46+
int end = list.length - 1;
47+
48+
while(start <= end) {
49+
int mid = (start + end) / 2;
50+
if(list[mid] >= target) {
51+
end = mid - 1;
52+
} else {
53+
start = mid + 1;
54+
}
55+
}
56+
57+
return start;
58+
}
59+
}

0 commit comments

Comments
 (0)