Skip to content

Commit 5e24497

Browse files
authored
Merge pull request #466 from jaejeong1/main
[jaejeong1] Week 6
2 parents 879ecd7 + 601ed62 commit 5e24497

File tree

3 files changed

+139
-0
lines changed

3 files changed

+139
-0
lines changed
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
class SolutionMaxArea {
2+
public int maxArea(int[] height) {
3+
// A์™€ B ๊ฐ„ ๋ฉด์  = (A์™€ B์˜ X์ถ• ์ฐจ์ด) * (A์™€ B Y์ถ• ์ค‘ ๋” ์ž‘์€ Y์ถ• ๊ธธ์ด)
4+
// 1๋ฒˆ์งธ ํ’€์ด ๋ฐฉ๋ฒ•: ์„ ํƒํ•  ์ˆ˜ ์žˆ๋Š” ๋ชจ๋“  ๊ฒฝ์šฐ์— ๋Œ€ํ•ด ์œ„ ๊ฐ’์„ ๊ตฌํ•œ๋‹ค, ์‹œ๊ฐ„๋ณต์žก๋„: O(N^2) / ๊ณต๊ฐ„๋ณต์žก๋„: O(1)
5+
// (์ตœ์ข…) 2๋ฒˆ์งธ ํ’€์ด ๋ฐฉ๋ฒ•: ์–‘์ชฝ ๋์—์„œ ํˆฌํฌ์ธํ„ฐ๋กœ ์ขํ˜€์˜ค๋ฉด์„œ ๋ฉด์ ์„ ๊ตฌํ•œ๋‹ค, ์‹œ๊ฐ„๋ณต์žก๋„: O(N) / ๊ณต๊ฐ„๋ณต์žก๋„: O(1)
6+
// ์ „์ฒด ๋„ˆ๋น„๋ฅผ ๊ณ ๋ คํ•˜๋ฉด์„œ ์›€์ง์—ฌ์•ผํ•œ๋‹ค.
7+
// ๋งค๋ฒˆ ๋ฉด์ ์„ ๊ณ„์‚ฐํ•˜๊ณ , ๋” ํฌ๋ฉด ์ €์žฅํ•œ๋‹ค.
8+
// lt์™€ rt ์ค‘ ๋” ๋‚ฎ์€์ชฝ์„ ์ขํžŒ๋‹ค
9+
10+
var lt = 0;
11+
var rt = height.length-1;
12+
var maxArea = 0;
13+
14+
while(lt < rt) {
15+
var x = rt - lt;
16+
var y = Math.min(height[lt], height[rt]);
17+
var currentArea = x * y;
18+
if (maxArea < currentArea) {
19+
maxArea = currentArea;
20+
}
21+
22+
if (height[lt] < height[rt]) {
23+
lt++;
24+
} else {
25+
rt--;
26+
}
27+
}
28+
29+
return maxArea;
30+
}
31+
}
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
import java.util.HashMap;
2+
import java.util.Map;
3+
4+
class WordDictionary {
5+
// ์‹œ๊ฐ„๋ณต์žก๋„: O(N), N(word์˜ ๊ธธ์ด)
6+
// ๊ณต๊ฐ„๋ณต์žก๋„: O(N)
7+
Map<Character, WordDictionary> child;
8+
boolean isEnd;
9+
10+
public WordDictionary() {
11+
child = new HashMap<>();
12+
isEnd = false;
13+
}
14+
15+
public void addWord(String word) {
16+
var node = this;
17+
for (int i = 0; i < word.length(); i++) {
18+
char c = word.charAt(i);
19+
20+
node.child.putIfAbsent(c, new WordDictionary());
21+
node = node.child.get(c);
22+
23+
if (i == word.length() - 1) {
24+
node.isEnd = true;
25+
return;
26+
}
27+
}
28+
}
29+
30+
public boolean search(String word) {
31+
return searchHelper(word, 0, this);
32+
}
33+
34+
private boolean searchHelper(String word, int index, WordDictionary node) {
35+
if (index == word.length()) {
36+
return node.isEnd;
37+
}
38+
39+
char c = word.charAt(index);
40+
// . ์ด ๋‚˜์˜ค๋ฉด ํ•ด๋‹น ๋…ธ๋“œ์˜ ์ž์‹ ๋…ธ๋“œ๋“ค์— ๋Œ€ํ•ด ๋ชจ๋‘ ์žฌ๊ท€๋ฅผ ๋Œ๋ฆฐ๋‹ค
41+
if (c == '.') {
42+
for (WordDictionary childNode : node.child.values()) {
43+
if (searchHelper(word, index + 1, childNode)) {
44+
return true;
45+
}
46+
}
47+
return false;
48+
} else {
49+
var childNode = node.child.get(c);
50+
if (childNode == null) {
51+
return false;
52+
}
53+
return searchHelper(word, index + 1, childNode);
54+
}
55+
}
56+
}
57+
58+
/**
59+
* Your WordDictionary object will be instantiated and called as such:
60+
* WordDictionary obj = new WordDictionary();
61+
* obj.addWord(word);
62+
* boolean param_2 = obj.search(word);
63+
*/

โ€Žvalid-parentheses/jaejeong1.java

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
import java.util.HashMap;
2+
import java.util.Stack;
3+
import java.util.Map;
4+
5+
class SolutionValidParentheses {
6+
7+
public boolean isValid(String s) {
8+
// last in first out ๋ฐฉ์‹์œผ๋กœ ์ฒ˜๋ฆฌํ•ด์•ผํ•˜๋ฏ€๋กœ ์Šคํƒ ์‚ฌ์šฉ
9+
// ์—ฌ๋Š” ๋ฌธ์ž๋ฉด put
10+
// ๋‹ซ๋Š” ๋ฌธ์ž๋ฉด pop
11+
// ์ด ๋•Œ pop ๋Œ€์ƒ์ด ์˜ฌ๋ฐ”๋ฅธ ์ง์ธ์ง€ ํ™•์ธํ•œ๋‹ค. ์•„๋‹ˆ๋ฉด false ๋ฐ˜ํ™˜
12+
// ์ง ํ™•์ธ ์‹œ O(1)๋กœ ์ฒ˜๋ฆฌํ•˜๊ธฐ ์œ„ํ•ด Map์„ ์‚ฌ์šฉ, ์—ฌ๋Š” ๋ฌธ์ž์™€ ๋‹ซ๋Š” ๋ฌธ์ž๋ฅผ key:value๋กœ ๋งคํ•‘
13+
// ๋๊นŒ์ง€ ๋Œ์•˜๊ณ , ์Šคํƒ์ด ๋น„์–ด์žˆ์œผ๋ฉด return true, ์•„๋‹ˆ๋ฉด false ๋ฐ˜ํ™˜
14+
// ์‹œ๊ฐ„๋ณต์žก๋„: O(N), ๊ณต๊ฐ„๋ณต์žก๋„: O(N)
15+
16+
Stack<Character> stack = new Stack<>();
17+
18+
Map<Character, Character> matchedMap = new HashMap<>();
19+
matchedMap.put('(', ')');
20+
matchedMap.put('{', '}');
21+
matchedMap.put('[', ']');
22+
23+
for (int i=0; i<s.length(); i++) {
24+
var currentChar = s.charAt(i);
25+
26+
// ์—ฌ๋Š” ๋ฌธ์ž
27+
if (matchedMap.containsKey(currentChar)) {
28+
stack.push(currentChar);
29+
} else { // ๋‹ซ๋Š” ๋ฌธ์ž
30+
if (stack.isEmpty()) { // ์—ฌ๋Š” ๋ฌธ์ž๊ฐ€ ์—†์„ ๊ฒฝ์šฐ false
31+
return false;
32+
}
33+
34+
var prevChar = stack.peek();
35+
if (matchedMap.get(prevChar).equals(currentChar)) {
36+
stack.pop();
37+
} else { // ๋‹ซ๋Š” ๋ฌธ์ž์™€ ์—ฌ๋Š” ๋ฌธ์ž ์ง์ด ์•ˆ๋งž์„ ๊ฒฝ์šฐ false
38+
return false;
39+
}
40+
}
41+
}
42+
43+
return stack.isEmpty(); // ์Šคํƒ์ด ๋น„์–ด์žˆ์œผ๋ฉด ๋ชจ๋“  ์ง ๋งค์นญ์ด ์™„๋ฃŒ๋œ ๊ฒƒ์œผ๋กœ true ๋ฐ˜ํ™˜
44+
}
45+
}

0 commit comments

Comments
ย (0)