Skip to content

Commit 227f7d8

Browse files
committed
feat : week 6
1 parent c026aed commit 227f7d8

File tree

5 files changed

+239
-0
lines changed

5 files changed

+239
-0
lines changed
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// 투포인터를 활용한 시간복잡도 O(N)으로 풀면된다.
2+
// 중요한건 언제 이동하냐?를 정의하는건데, 전체 물양이 줄어들면 상대적으로 작은 쪽을 이동시키면된다.
3+
class Solution {
4+
public int maxArea(int[] height) {
5+
int left = 0;
6+
int right = height.length-1;
7+
int maxWater = 0;
8+
while(left < right) {
9+
int max = Math.min(height[left], height[right]) * (right - left);
10+
maxWater = Math.max(max,maxWater);
11+
if (height[left] < height[right]) {
12+
left++;
13+
} else {
14+
right--;
15+
}
16+
}
17+
return maxWater;
18+
}
19+
}
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
// GPT가 거의 풀어준 Trie를 이용한 풀이. 알파벳의 존재 여부를 26개의 노드에 저장한다.
2+
// 다음 노드를 찾아가는 방식은 재귀적으로 구성된다. 단어의 끝은 endOfWord로 관리됨.
3+
// 혼자서는 절대 못 풀었을 문제
4+
class WordDictionary {
5+
private TrieNode root;
6+
7+
private static class TrieNode {
8+
private TrieNode[] children;
9+
private boolean isEndOfWord;
10+
11+
public TrieNode() {
12+
this.children = new TrieNode[26];
13+
this.isEndOfWord = false;
14+
}
15+
}
16+
17+
public WordDictionary() {
18+
root = new TrieNode();
19+
}
20+
21+
public void addWord(String word) {
22+
TrieNode current = root;
23+
for (char ch : word.toCharArray()) {
24+
int index = ch - 'a';
25+
if (current.children[index] == null) {
26+
current.children[index] = new TrieNode();
27+
}
28+
current = current.children[index];
29+
}
30+
current.isEndOfWord = true;
31+
}
32+
33+
public boolean search(String word) {
34+
return searchInNode(word, root, 0);
35+
}
36+
37+
private boolean searchInNode(String word, TrieNode node, int index) {
38+
if (index == word.length()) {
39+
return node.isEndOfWord;
40+
}
41+
42+
char ch = word.charAt(index);
43+
if (ch == '.') {
44+
for (TrieNode child : node.children) {
45+
if (child != null && searchInNode(word, child, index + 1)) {
46+
return true;
47+
}
48+
}
49+
return false;
50+
} else {
51+
int charIndex = ch - 'a';
52+
TrieNode child = node.children[charIndex];
53+
if (child == null) {
54+
return false;
55+
}
56+
return searchInNode(word, child, index + 1);
57+
}
58+
}
59+
}
60+
61+
// 내가 생각한 첫번째 풀이. 정규표현식을 이용하면 될거라 생각했는데..
62+
class WordDictionary {
63+
private List<String> words;
64+
65+
public WordDictionary() {
66+
words = new ArrayList<>();
67+
}
68+
69+
public void addWord(String word) {
70+
words.add(word);
71+
}
72+
73+
public boolean search(String word) {
74+
String regex = word.replace(".", "[a-z]");
75+
for (String w : words) {
76+
if (w.matches(regex)) {
77+
return true;
78+
}
79+
}
80+
return false;
81+
}
82+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// 이진 트리로 값을 변경해가면서 해결하면된다.
2+
// 기존에 있던 노드에 위치한 값보다 작은 값이 등장하면 값이 치환된다는 개념만 이해하면 쉽다.
3+
// 자바에서는 콜렉션에서 바이너리 서치를 제공해서 편하게 구현가능
4+
class Solution {
5+
public int lengthOfLIS(int[] nums) {
6+
List<Integer> sub = new ArrayList<>();
7+
8+
for (int num : nums) {
9+
int pos = Collections.binarySearch(sub, num);
10+
if (pos < 0) {
11+
pos = -(pos + 1);
12+
}
13+
if (pos < sub.size()) {
14+
sub.set(pos, num);
15+
} else {
16+
sub.add(num);
17+
}
18+
}
19+
return sub.size();
20+
}
21+
}

spiral-matrix/imsosleepy.java

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
// 처음보는 시뮬레이션 문제라 그냥 풀었음
2+
// 별다른 알고리즘이 필요없다.
3+
public class Solution {
4+
public List<Integer> spiralOrder(int[][] matrix) {
5+
List<Integer> result = new ArrayList<>();
6+
if (matrix == null || matrix.length == 0) return result;
7+
8+
int rows = matrix.length;
9+
int cols = matrix[0].length;
10+
boolean[][] visited = new boolean[rows][cols];
11+
12+
int[] dRow = {0, 1, 0, -1};
13+
int[] dCol = {1, 0, -1, 0};
14+
15+
int row = 0, col = 0, dir = 0;
16+
17+
for (int i = 0; i < rows * cols; i++) {
18+
result.add(matrix[row][col]);
19+
visited[row][col] = true;
20+
21+
int nextRow = row + dRow[dir];
22+
int nextCol = col + dCol[dir];
23+
24+
if (nextRow < 0 || nextRow >= rows || nextCol < 0 || nextCol >= cols || visited[nextRow][nextCol]) {
25+
dir = (dir + 1) % 4;
26+
nextRow = row + dRow[dir];
27+
nextCol = col + dCol[dir];
28+
}
29+
30+
row = nextRow;
31+
col = nextCol;
32+
}
33+
34+
return result;
35+
}
36+
}

valid-parentheses/imsosleepy.java

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
// 스택을 사용해 시간복잡도를 O(N)으로 풀어야하는 문제.
2+
// toCharArray로 문자열을 자르는 시간을 대폭 줄일 수 있다. charAt도 그닥 좋은 방법은 아닌듯
3+
class Solution {
4+
public boolean isValid(String s) {
5+
Stack<Character> stack = new Stack<>();
6+
7+
if(s.length() == 1) return false;
8+
boolean valid = false;
9+
for(char c : s.toCharArray()) {
10+
valid = false;
11+
if(c == '(' || c == '{' || c == '[') {
12+
stack.push(c);
13+
continue;
14+
}
15+
16+
if(!stack.isEmpty() && c == ')' && stack.peek() == '(') {
17+
valid = true;
18+
stack.pop();
19+
continue;
20+
}
21+
22+
if(!stack.isEmpty() && c == '}' && stack.peek() == '{') {
23+
valid = true;
24+
stack.pop();
25+
continue;
26+
}
27+
28+
if(!stack.isEmpty() && c == ']' && stack.peek() == '[') {
29+
valid = true;
30+
stack.pop();
31+
continue;
32+
}
33+
break;
34+
}
35+
36+
if (!stack.isEmpty()) return false;
37+
38+
return valid;
39+
}
40+
}
41+
42+
// 첫 풀이. s.split("") 하나가 제법 많은 시간 복잡도를 사용한다.
43+
// String 객체할당과 배열 재생성 등의 과정을 거친다. 그래서... 다른 방법을 찾아봄
44+
class Solution {
45+
public boolean isValid(String s) {
46+
Stack<String> stack = new Stack<>();
47+
48+
if(s.length() == 1) return false;
49+
boolean valid = false;
50+
for(String str : s.split("")) {
51+
valid = false;
52+
if(str.equals("(") || str.equals("{") || str.equals("[") ) {
53+
stack.push(str);
54+
continue;
55+
}
56+
57+
if(!stack.isEmpty() && str.equals(")") && stack.peek().equals("(")) {
58+
valid = true;
59+
stack.pop();
60+
continue;
61+
}
62+
63+
if(!stack.isEmpty() && str.equals("]") && stack.peek().equals("[")) {
64+
valid = true;
65+
stack.pop();
66+
continue;
67+
}
68+
69+
if(!stack.isEmpty() && str.equals("}") && stack.peek().equals("{")) {
70+
valid = true;
71+
stack.pop();
72+
continue;
73+
}
74+
break;
75+
}
76+
77+
if (!stack.isEmpty()) return false;
78+
79+
return valid;
80+
}
81+
}

0 commit comments

Comments
 (0)