Skip to content

Commit 215d994

Browse files
authored
Merge pull request #1426 from Tessa1217/main
[Tessa1217] Week 06 Solutions
2 parents 195d853 + 43f4b16 commit 215d994

File tree

5 files changed

+261
-0
lines changed

5 files changed

+261
-0
lines changed
+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/**
2+
* 정수 배열 height가 주어질 때 물을 최대한 담을 수 있는 두 개의 라인을 찾으세요.
3+
힌트1. 투 포인터를 활용해서 찾으세요.
4+
*/
5+
class Solution {
6+
7+
// 시간 복잡도: O(n)
8+
public int maxArea(int[] height) {
9+
10+
int max = 0;
11+
int start = 0;
12+
int end = height.length - 1;
13+
14+
while (start < end) {
15+
16+
int area = Math.min(height[start], height[end]) * (end - start);
17+
max = Math.max(area, max);
18+
19+
if (height[start] < height[end]) {
20+
start++;
21+
} else {
22+
end--;
23+
}
24+
}
25+
26+
return max;
27+
}
28+
}
29+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
import java.util.HashMap;
2+
import java.util.Map;
3+
4+
/**
5+
* WordDictionary
6+
* WordDictionary() - 객체 초기화
7+
* void addWord(word) - 자료 구조에 word 추가
8+
* boolean search(word) - 자료 구조에 word 있으면 true 아니면 false 반환
9+
- '.'는 와일드카드로 활용 가능
10+
*/
11+
class WordDictionary {
12+
13+
private Node root;
14+
15+
public WordDictionary() {
16+
root = new Node();
17+
}
18+
19+
public void addWord(String word) {
20+
Node current = root;
21+
for (char c : word.toCharArray()) {
22+
if (!current.nodeCharacters.containsKey(c)) {
23+
current.nodeCharacters.put(c, new Node());
24+
}
25+
current = current.nodeCharacters.get(c);
26+
}
27+
current.isEnd = true;
28+
}
29+
30+
public boolean search(String word) {
31+
return searchInNode(word, 0, root);
32+
}
33+
34+
private boolean searchInNode(String word, int idx, Node node) {
35+
36+
if (node == null) {
37+
return false;
38+
}
39+
40+
if (idx == word.length()) {
41+
return node.isEnd;
42+
}
43+
44+
char c = word.charAt(idx);
45+
// 와일드카드는 Node에 있는 전체 값 다 탐색
46+
if (c == '.') {
47+
for (Node child : node.nodeCharacters.values()) {
48+
if (searchInNode(word, idx + 1, child)) {
49+
return true;
50+
}
51+
}
52+
return false;
53+
} else {
54+
Node current = node.nodeCharacters.get(c);
55+
return searchInNode(word, idx + 1, current);
56+
}
57+
}
58+
59+
static class Node {
60+
61+
boolean isEnd;
62+
63+
Map<Character, Node> nodeCharacters;
64+
65+
Node() {
66+
nodeCharacters = new HashMap<>();
67+
isEnd = false;
68+
}
69+
70+
}
71+
}
72+
73+
/**
74+
* Your WordDictionary object will be instantiated and called as such:
75+
* WordDictionary obj = new WordDictionary();
76+
* obj.addWord(word);
77+
* boolean param_2 = obj.search(word);
78+
*/
79+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/**
2+
* 정수 배열 nums가 주어질 때 가장 길게 증가하는 부분 수열의 길이를 찾으세요. (LIS)
3+
*/
4+
class Solution {
5+
6+
// 시간복잡도: O(n^2)
7+
public int lengthOfLIS(int[] nums) {
8+
9+
int[] dp = new int[nums.length];
10+
int n = nums.length;
11+
int max = 0;
12+
13+
// 원소의 위치에서 가질 수 있는 LIS의 값 계산
14+
for (int i = 0; i < n; i++) {
15+
dp[i] = 1;
16+
for (int j = 0; j < i; j++) {
17+
if (nums[j] < nums[i]) {
18+
dp[i] = Math.max(dp[i], dp[j] + 1);
19+
}
20+
}
21+
max = Math.max(dp[i], max);
22+
}
23+
24+
return max;
25+
}
26+
}
27+

spiral-matrix/Tessa1217.java

+53
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
import java.util.ArrayList;
2+
import java.util.List;
3+
4+
/**
5+
* m * n 행렬을 시계 방향 순대로 요소를 정렬하여 반환하세요.
6+
*/
7+
class Solution {
8+
9+
public List<Integer> spiralOrder(int[][] matrix) {
10+
11+
List<Integer> spirals = new ArrayList<>();
12+
13+
int top = 0;
14+
int bottom = matrix.length - 1;
15+
int start = 0;
16+
int end = matrix[0].length - 1;
17+
18+
while (top <= bottom && start <= end) {
19+
// 우측
20+
for (int i = start; i <= end; i++) {
21+
spirals.add(matrix[top][i]);
22+
}
23+
top++;
24+
25+
// 아래
26+
for (int i = top; i <= bottom; i++) {
27+
spirals.add(matrix[i][end]);
28+
}
29+
end--;
30+
31+
// 좌측
32+
if (top <= bottom) {
33+
for (int i = end; i >= start; i--) {
34+
spirals.add(matrix[bottom][i]);
35+
}
36+
bottom--;
37+
}
38+
39+
// 위
40+
if (start <= end) {
41+
for (int i = bottom; i >= top; i--) {
42+
spirals.add(matrix[i][start]);
43+
}
44+
start++;
45+
}
46+
47+
}
48+
49+
return spirals;
50+
}
51+
52+
}
53+

valid-parentheses/Tessa1217.java

+73
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
/**
2+
* 유효한 괄호: '(', ')', '{', '}', '[', ']'로 이루어진 문자열 s가 주어질 때 문자열 s가 유효한 괄호로 이루어진 문자열인지 판별하세요.
3+
유요한 문자열인지의 판별 방법
4+
1. 여는 괄호는 반드시 같은 종류의 닫는 괄호에 의해 닫혀야 한다.
5+
2. 여는 괄호는 반드시 정확한 순서로 닫혀야 한다.
6+
3. 모든 닫는 괄호는 같은 종류의 여는 괄호에 의해서 열려야 한다.
7+
*/
8+
class Solution {
9+
10+
// 시간복잡도: O(n)
11+
public boolean isValid(String s) {
12+
13+
Stack<Character> parentheses = new Stack<>();
14+
// 맵으로 조건문 간소화
15+
Map<Character, Character> parenthesisMap = Map.of(')', '(', '}', '{', ']', '[');
16+
17+
for (char c : s.toCharArray()) {
18+
if (c == '(' || c == '{' || c == '[') {
19+
parentheses.push(c);
20+
} else if (parentheses.isEmpty() || parentheses.pop() != parenthesisMap.get(c)) { // 전체 다 돌지 않고 유효한 괄호 아니면 사전에 불린 반환
21+
return false;
22+
}
23+
}
24+
25+
return parentheses.size() == 0;
26+
27+
}
28+
29+
// public boolean isValid(String s) {
30+
31+
// Stack<Character> parentheses = new Stack<>();
32+
// // 맵으로 조건문 간소화
33+
// Map<Character, Character> parenthesisMap = Map.of(')', '(', '}', '{', ']', '[');
34+
35+
// for (char c : s.toCharArray()) {
36+
// if (parentheses.isEmpty()) {
37+
// parentheses.push(c);
38+
// continue;
39+
// }
40+
// if (parentheses.peek() == parenthesisMap.get(c)) {
41+
// parentheses.pop();
42+
// } else {
43+
// parentheses.push(c);
44+
// }
45+
// }
46+
47+
// return parentheses.size() == 0;
48+
49+
// }
50+
51+
// public boolean isValid(String s) {
52+
53+
// Stack<Character> parentheses = new Stack<>();
54+
55+
// for (char c : s.toCharArray()) {
56+
// if (parentheses.isEmpty()) {
57+
// parentheses.push(c);
58+
// continue;
59+
// }
60+
// if ((parentheses.peek() == '(' && c == ')') ||
61+
// (parentheses.peek() == '{' && c == '}') ||
62+
// (parentheses.peek() == '[' && c == ']')) {
63+
// parentheses.pop();
64+
// } else {
65+
// parentheses.push(c);
66+
// }
67+
// }
68+
69+
// return parentheses.size() == 0;
70+
71+
// }
72+
}
73+

0 commit comments

Comments
 (0)