-
Notifications
You must be signed in to change notification settings - Fork 126
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #466 from jaejeong1/main
[jaejeong1] Week 6
- Loading branch information
Showing
3 changed files
with
139 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
class SolutionMaxArea { | ||
public int maxArea(int[] height) { | ||
// A์ B ๊ฐ ๋ฉด์ = (A์ B์ X์ถ ์ฐจ์ด) * (A์ B Y์ถ ์ค ๋ ์์ Y์ถ ๊ธธ์ด) | ||
// 1๋ฒ์งธ ํ์ด ๋ฐฉ๋ฒ: ์ ํํ ์ ์๋ ๋ชจ๋ ๊ฒฝ์ฐ์ ๋ํด ์ ๊ฐ์ ๊ตฌํ๋ค, ์๊ฐ๋ณต์ก๋: O(N^2) / ๊ณต๊ฐ๋ณต์ก๋: O(1) | ||
// (์ต์ข ) 2๋ฒ์งธ ํ์ด ๋ฐฉ๋ฒ: ์์ชฝ ๋์์ ํฌํฌ์ธํฐ๋ก ์ขํ์ค๋ฉด์ ๋ฉด์ ์ ๊ตฌํ๋ค, ์๊ฐ๋ณต์ก๋: O(N) / ๊ณต๊ฐ๋ณต์ก๋: O(1) | ||
// ์ ์ฒด ๋๋น๋ฅผ ๊ณ ๋ คํ๋ฉด์ ์์ง์ฌ์ผํ๋ค. | ||
// ๋งค๋ฒ ๋ฉด์ ์ ๊ณ์ฐํ๊ณ , ๋ ํฌ๋ฉด ์ ์ฅํ๋ค. | ||
// lt์ rt ์ค ๋ ๋ฎ์์ชฝ์ ์ขํ๋ค | ||
|
||
var lt = 0; | ||
var rt = height.length-1; | ||
var maxArea = 0; | ||
|
||
while(lt < rt) { | ||
var x = rt - lt; | ||
var y = Math.min(height[lt], height[rt]); | ||
var currentArea = x * y; | ||
if (maxArea < currentArea) { | ||
maxArea = currentArea; | ||
} | ||
|
||
if (height[lt] < height[rt]) { | ||
lt++; | ||
} else { | ||
rt--; | ||
} | ||
} | ||
|
||
return maxArea; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
import java.util.HashMap; | ||
import java.util.Map; | ||
|
||
class WordDictionary { | ||
// ์๊ฐ๋ณต์ก๋: O(N), N(word์ ๊ธธ์ด) | ||
// ๊ณต๊ฐ๋ณต์ก๋: O(N) | ||
Map<Character, WordDictionary> child; | ||
boolean isEnd; | ||
|
||
public WordDictionary() { | ||
child = new HashMap<>(); | ||
isEnd = false; | ||
} | ||
|
||
public void addWord(String word) { | ||
var node = this; | ||
for (int i = 0; i < word.length(); i++) { | ||
char c = word.charAt(i); | ||
|
||
node.child.putIfAbsent(c, new WordDictionary()); | ||
node = node.child.get(c); | ||
|
||
if (i == word.length() - 1) { | ||
node.isEnd = true; | ||
return; | ||
} | ||
} | ||
} | ||
|
||
public boolean search(String word) { | ||
return searchHelper(word, 0, this); | ||
} | ||
|
||
private boolean searchHelper(String word, int index, WordDictionary node) { | ||
if (index == word.length()) { | ||
return node.isEnd; | ||
} | ||
|
||
char c = word.charAt(index); | ||
// . ์ด ๋์ค๋ฉด ํด๋น ๋ ธ๋์ ์์ ๋ ธ๋๋ค์ ๋ํด ๋ชจ๋ ์ฌ๊ท๋ฅผ ๋๋ฆฐ๋ค | ||
if (c == '.') { | ||
for (WordDictionary childNode : node.child.values()) { | ||
if (searchHelper(word, index + 1, childNode)) { | ||
return true; | ||
} | ||
} | ||
return false; | ||
} else { | ||
var childNode = node.child.get(c); | ||
if (childNode == null) { | ||
return false; | ||
} | ||
return searchHelper(word, index + 1, childNode); | ||
} | ||
} | ||
} | ||
|
||
/** | ||
* Your WordDictionary object will be instantiated and called as such: | ||
* WordDictionary obj = new WordDictionary(); | ||
* obj.addWord(word); | ||
* boolean param_2 = obj.search(word); | ||
*/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
import java.util.HashMap; | ||
import java.util.Stack; | ||
import java.util.Map; | ||
|
||
class SolutionValidParentheses { | ||
|
||
public boolean isValid(String s) { | ||
// last in first out ๋ฐฉ์์ผ๋ก ์ฒ๋ฆฌํด์ผํ๋ฏ๋ก ์คํ ์ฌ์ฉ | ||
// ์ฌ๋ ๋ฌธ์๋ฉด put | ||
// ๋ซ๋ ๋ฌธ์๋ฉด pop | ||
// ์ด ๋ pop ๋์์ด ์ฌ๋ฐ๋ฅธ ์ง์ธ์ง ํ์ธํ๋ค. ์๋๋ฉด false ๋ฐํ | ||
// ์ง ํ์ธ ์ O(1)๋ก ์ฒ๋ฆฌํ๊ธฐ ์ํด Map์ ์ฌ์ฉ, ์ฌ๋ ๋ฌธ์์ ๋ซ๋ ๋ฌธ์๋ฅผ key:value๋ก ๋งคํ | ||
// ๋๊น์ง ๋์๊ณ , ์คํ์ด ๋น์ด์์ผ๋ฉด return true, ์๋๋ฉด false ๋ฐํ | ||
// ์๊ฐ๋ณต์ก๋: O(N), ๊ณต๊ฐ๋ณต์ก๋: O(N) | ||
|
||
Stack<Character> stack = new Stack<>(); | ||
|
||
Map<Character, Character> matchedMap = new HashMap<>(); | ||
matchedMap.put('(', ')'); | ||
matchedMap.put('{', '}'); | ||
matchedMap.put('[', ']'); | ||
|
||
for (int i=0; i<s.length(); i++) { | ||
var currentChar = s.charAt(i); | ||
|
||
// ์ฌ๋ ๋ฌธ์ | ||
if (matchedMap.containsKey(currentChar)) { | ||
stack.push(currentChar); | ||
} else { // ๋ซ๋ ๋ฌธ์ | ||
if (stack.isEmpty()) { // ์ฌ๋ ๋ฌธ์๊ฐ ์์ ๊ฒฝ์ฐ false | ||
return false; | ||
} | ||
|
||
var prevChar = stack.peek(); | ||
if (matchedMap.get(prevChar).equals(currentChar)) { | ||
stack.pop(); | ||
} else { // ๋ซ๋ ๋ฌธ์์ ์ฌ๋ ๋ฌธ์ ์ง์ด ์๋ง์ ๊ฒฝ์ฐ false | ||
return false; | ||
} | ||
} | ||
} | ||
|
||
return stack.isEmpty(); // ์คํ์ด ๋น์ด์์ผ๋ฉด ๋ชจ๋ ์ง ๋งค์นญ์ด ์๋ฃ๋ ๊ฒ์ผ๋ก true ๋ฐํ | ||
} | ||
} |