Skip to content

Commit 182eb68

Browse files
authored
Merge pull request #2200 from se6816/main
[se6816] WEEK 06 solutions
2 parents aa657db + 47c681e commit 182eb68

File tree

5 files changed

+314
-0
lines changed

5 files changed

+314
-0
lines changed
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+
}
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+
}

spiral-matrix/se6816.java

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
/**
2+
구현을 통해 배열을 읽어들이는 방식
3+
시간 복잡도 : O(N*M)
4+
공간 복잡도 : O(N*M)
5+
*/
6+
class Solution {
7+
int[] moveY = {1, 0, -1, 0};
8+
int[] moveX = {0, -1, 0, 1};
9+
int N;
10+
int M;
11+
public List<Integer> spiralOrder(int[][] matrix) {
12+
N = matrix.length;
13+
M = matrix[0].length;
14+
boolean[][] visited = new boolean[N][M];
15+
List<Integer> result = new ArrayList<>();
16+
int curX = 0;
17+
int curY = 0;
18+
int direction = 0;
19+
int count = 0;
20+
visited[curX][curY] = true;
21+
result.add(matrix[curX][curY]);
22+
23+
while(true) {
24+
if(count == 4) {
25+
break;
26+
}
27+
int tempX = curX + moveX[direction];
28+
int tempY = curY + moveY[direction];
29+
if(outOfIndex(tempX, tempY)) {
30+
direction = (direction + 1) % 4;
31+
count++;
32+
continue;
33+
}
34+
35+
if(visited[tempX][tempY]) {
36+
direction = (direction + 1) % 4;
37+
count++;
38+
continue;
39+
}
40+
41+
curX = tempX;
42+
curY = tempY;
43+
result.add(matrix[curX][curY]);
44+
visited[curX][curY] = true;
45+
46+
count = 0;
47+
}
48+
49+
return result;
50+
}
51+
52+
public boolean outOfIndex(int x, int y) {
53+
return x < 0 || x >= N || y < 0 || y >= M;
54+
}
55+
}

valid-parentheses/se6816.java

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/**
2+
Stack을 활용한 방식
3+
문자열 s의 길이 -> N
4+
시간 복잡도 : O(N)
5+
공간 복잡도 : O(N)
6+
*/
7+
class Solution {
8+
public boolean isValid(String s) {
9+
boolean result= true;
10+
Stack<Character> sta=new Stack<>();
11+
Map<Character,Character> map=new HashMap<>();
12+
map.put('}','{');
13+
map.put(']','[');
14+
map.put(')','(');
15+
for(int i=0; i<s.length();i++){
16+
char ch=s.charAt(i);
17+
if(ch == '(' || ch == '[' || ch == '{'){
18+
sta.push(ch);
19+
continue;
20+
}
21+
char target=map.get(ch);
22+
if(sta.isEmpty()){
23+
result=false;
24+
break;
25+
}
26+
if(!(sta.peek() == target)){
27+
result=false;
28+
break;
29+
}
30+
sta.pop();
31+
}
32+
if(!sta.isEmpty()){
33+
result=false;
34+
}
35+
return result;
36+
}
37+
}

0 commit comments

Comments
 (0)