Skip to content

Commit cd07110

Browse files
authored
Merge pull request DaleStudy#469 from taekwon-dev/main
[์œคํƒœ๊ถŒ] Week6 ๋ฌธ์ œ ํ’€์ด
2 parents 5da469b + 040b881 commit cd07110

File tree

3 files changed

+116
-0
lines changed

3 files changed

+116
-0
lines changed
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/**
2+
* ์‹œ๊ฐ„ ๋ณต์žก๋„: O(n)
3+
* ๊ณต๊ฐ„ ๋ณต์žก๋„: O(1)
4+
*
5+
* ์ตœ๋Œ€๋กœ ๋งŽ์€ ๋ฌผ์„ ์ €์žฅํ•˜๊ธฐ ์œ„ํ•ด ๊ฐ€์žฅ ์šฐ์„ ์ ์œผ๋กœ ๊ณ ๋ คํ•ด์•ผ ํ•˜๋Š” ๊ฒƒ์€ ์งง์€ ๋ง‰๋Œ€์˜ ๊ธธ์ด
6+
* ๋”ฐ๋ผ์„œ, ์งง์€ ๋†’์ด๋ฅผ ๊ฐ€์ง€๋Š” ์ชฝ์˜ ํฌ์ธํ„ฐ๋ฅผ ์ด๋™ํ•˜๋ฉด์„œ ์ตœ๋Œ€ ์ €์žฅ ๊ฐ€๋Šฅํ•œ ๊ฐ’์„ ๋น„๊ต
7+
*
8+
*/
9+
class Solution {
10+
public int maxArea(int[] height) {
11+
int answer = 0;
12+
int left = 0;
13+
int right = height.length - 1;
14+
15+
while (left < right) {
16+
int water = Math.min(height[left], height[right]) * (right - left);
17+
answer = Math.max(answer, water);
18+
19+
if (height[left] < height[right]) {
20+
left++;
21+
} else {
22+
right--;
23+
}
24+
}
25+
26+
return answer;
27+
}
28+
}
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
class WordDictionary {
2+
3+
private class TrieNode {
4+
Map<Character, TrieNode> children = new HashMap<>();
5+
boolean isEndOfWord = false;
6+
}
7+
8+
private TrieNode root;
9+
10+
public WordDictionary() {
11+
root = new TrieNode();
12+
}
13+
14+
/**
15+
* ์‹œ๊ฐ„ ๋ณต์žก๋„: O(n), n = ๋‹จ์–ด์˜ ๊ธธ์ด
16+
* ๊ณต๊ฐ„ ๋ณต์žก๋„: O(n)
17+
*/
18+
public void addWord(String word) {
19+
TrieNode node = root;
20+
21+
for (int i = 0; i < word.length(); i++) {
22+
char c = word.charAt(i);
23+
node.children.putIfAbsent(c, new TrieNode());
24+
node = node.children.get(c);
25+
}
26+
node.isEndOfWord = true;
27+
}
28+
29+
/**
30+
* ์‹œ๊ฐ„ ๋ณต์žก๋„: O(26^n), n = ๋‹จ์–ด์˜ ๊ธธ์ด
31+
* ๊ณต๊ฐ„ ๋ณต์žก๋„: O(n)
32+
*/
33+
public boolean search(String word) {
34+
return searchInNode(word, 0, root);
35+
}
36+
37+
private boolean searchInNode(String word, int index, TrieNode node) {
38+
if (index == word.length()) {
39+
return node.isEndOfWord;
40+
}
41+
42+
char c = word.charAt(index);
43+
if (c != '.') {
44+
if (node.children.containsKey(c)) {
45+
return searchInNode(word, index + 1, node.children.get(c));
46+
} else {
47+
return false;
48+
}
49+
} else {
50+
for (TrieNode childNode : node.children.values()) {
51+
if (searchInNode(word, index + 1, childNode)) {
52+
return true;
53+
}
54+
}
55+
return false;
56+
}
57+
}
58+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/**
2+
* ์‹œ๊ฐ„ ๋ณต์žก๋„: O(n)
3+
* ๊ณต๊ฐ„ ๋ณต์žก๋„: O(n)
4+
*/
5+
class Solution {
6+
public boolean isValid(String s) {
7+
Map<Character, Character> map = new HashMap<>(){{
8+
put(')', '(');
9+
put('}', '{');
10+
put(']', '[');
11+
}};
12+
13+
Deque<Character> stack = new ArrayDeque<>();
14+
15+
for (int i = 0; i < s.length(); i++) {
16+
char c = s.charAt(i);
17+
18+
if (!map.containsKey(c)) {
19+
stack.push(c);
20+
continue;
21+
}
22+
23+
if (stack.isEmpty() || stack.removeFirst() != map.get(c)) {
24+
return false;
25+
}
26+
}
27+
28+
return stack.size() == 0;
29+
}
30+
}

0 commit comments

Comments
ย (0)