Skip to content

[YoungSeok-Choi] week 6 solutions #1409

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
May 9, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 63 additions & 0 deletions container-with-most-water/YoungSeok-Choi.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
// tc: O(n)
// 투 포인터라는 문제풀이 방법으로 간단히 (답지보고) 해결...
class Solution {
public int maxArea(int[] h) {
int start = 0;
int end = h.length - 1;
int mx = -987654321;

while(start < end) {
mx = Math.max(mx, (end - start) * Math.min(h[start], h[end]));

if(h[start] < h[end]) {
start++;
} else {
end--;
}
}

return mx;
}
}


// 예외처리가 덕지덕지 붙기 시작할 때. (잘못됨을 깨닫고)다른 방법으로 풀어햐 하는건 아닌지 생각하는 습관 필요함..ㅠ
class WrongSolution {
public int maxArea(int[] h) {
int mx = Math.min(h[0], h[1]);
int idx = 0;

for(int i = 1; i < h.length; i++) {
int offset = i - idx;

int prevCalc = Math.min(h[i - 1], h[i]);
int calc = Math.min(h[idx], h[i]);
int newMx = calc * offset;

// 새롭게 인덱스를 바꿔버리는게 더 나을때.
if(prevCalc > newMx) {
idx = i - 1;
mx = Math.max(mx, prevCalc);
continue;
}

// 물을 더 많이 담을 수 있을 때.
if(h[idx] < h[i] && newMx > mx) {
if(i == 2) {
int exc = Math.min(h[1], h[i]) * offset;
if(exc > newMx) {
idx = 1;
mx = Math.max(exc, mx);
continue;
}
}

idx = i;
}

mx = Math.max(newMx, mx);
}

return mx;
}
}
170 changes: 170 additions & 0 deletions design-add-and-search-words-data-structure/YoungSeok-Choi.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,170 @@
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


// NOTE: dot에 대한 wildcard 검색이 핵심이었던 문제.
// NOTE: tc -> 문자 입력 O(n), 패턴검색 O(26^n), 패턴X검색 O(1)

/**
검색어 | 의미
"." | 길이가 1인 아무 문자와 매치
".a" | 길이 2: 첫 글자는 아무 문자, 두 번째는 a
"..d" | 길이 3: 처음 두 글자는 아무 문자, 마지막은 d
"..." | 길이 3인 모든 단어와 매치
*/
class WordDictionary {

private WordDictionary[] child;
private Character val;
private Map<String, Boolean> cMap;

public WordDictionary() {
this.cMap = new HashMap<>();
this.val = null;
}

public void addWord(String word) {
if(this.cMap.containsKey(word)) return;

this.cMap.put(word, true);
this.innerInsert(word);
}

public void innerInsert(String word) {
if(word.length() == 0) return;

if(this.child == null) {
this.child = new WordDictionary[26];
}

char c = word.charAt(0);
int idx = c - 97;

if(this.child[idx] == null) {
this.child[idx] = new WordDictionary();
this.child[idx].val = c;
}

this.child[idx].innerInsert(word.substring(1));
}

public boolean search(String word) {
if(!word.contains(".")) return this.cMap.containsKey(word);


return this.patternSearch(word);
}

private boolean patternSearch(String wildcard) {
if(wildcard.length() == 0) return true;

char c = wildcard.charAt(0);
if(c == '.') { // NOTE: wildcard를 만났을 때, 해당 depth는 검사하지 않고, 다음 번(자식) 문자들에 대해서 검색 이어나가기.
List<Boolean> res = new ArrayList<>();

for(WordDictionary children : this.child) {
if(children != null) {
res.add(children.patternSearch(wildcard.substring(1)));
}
}

for(boolean b : res) {
if(b) return true;
}

return false;
}

int idx = c - 97;
if(this.child == null || this.child[idx] == null || this.child[idx].val == null) return false;

return this.child[idx].patternSearch(wildcard.substring(1));
}
}

/**
* Your WordDictionary object will be instantiated and called as such:
* WordDictionary obj = new WordDictionary();
* obj.addWord(word);
* boolean param_2 = obj.search(word);
*/


// 이전 TRIE 자료구조를 모른 채로 풀이했던 문제..
class PrevWordDictionary {

private String word;
private PrevWordDictionary leftChild;
private PrevWordDictionary rightChild;

public PrevWordDictionary() {
this.leftChild = null;
this.rightChild = null;
this.word = null;
}

public void addWord(String word) {
if(this.word == null) {
this.word = word;
return;
}

// NOTE: 사전 순으로 크고 작음을 두어 이진처리.
if(this.word.compareTo(word) == 1) {

this.rightChild = new PrevWordDictionary();
this.rightChild.addWord(word);
} else if (this.word.compareTo(word) == -1) {

this.leftChild = new PrevWordDictionary();
this.leftChild.addWord(word);
} else {
// already exists. just return
}
}

public boolean search(String word) {
if(word.contains(".")) {
// String replaced = word.replace(".", "");

// return word.startsWith(".") ? this.startsWith(replaced) : this.endsWith(replaced);
return this.patternSearch(word.replace(".", ""));
}

if(this.word == null) {
return false;
}

if(this.word.equals(word)) {
return true;
}

if(this.rightChild == null && this.leftChild == null) {
return false;
}

if(this.word.compareTo(word) == 1) {
return this.rightChild == null ? false : this.rightChild.search(word);
} else {
return this.leftChild == null ? false : this.leftChild.search(word);
}
}

private boolean patternSearch(String wildcard) {
if(this.word.contains(wildcard)) return true;

boolean left = false;
boolean right = false;
if(this.leftChild != null) {
left = this.leftChild.patternSearch(wildcard);
}

if(this.rightChild != null) {
right = this.rightChild.patternSearch(wildcard);
}

return left || right;
}
}
82 changes: 72 additions & 10 deletions implement-trie-prefix-tree/YoungSeok-Choi.java
Original file line number Diff line number Diff line change
@@ -1,30 +1,63 @@
import java.util.HashMap;
import java.util.Map;

// Map으로 풀려버려서 당황..
// 이진트리? 어떤식으로 풀어야 할지 자료구조 정하고 다시 풀어보기..
// 한 글자씩 잘라서 하위 자식 노드들로 관리
// search 동작은 기존 그대로 Map자료구조에서 찾고, 이후 prefix연산에서 속도를 개선 (230ms -> 30ms)
class Trie {

Map<String, Boolean> tMap;
private Trie[] child;
private Character val;
private Map<String, Boolean> cMap;

public Trie() {
this.tMap = new HashMap<>();
this.cMap = new HashMap<>();
this.val = null;
}

public void insert(String word) {
this.tMap.put(word, true);
if(this.cMap.containsKey(word)) return;

this.cMap.put(word, true);
this.innerInsert(word);
}

public void innerInsert(String word) {
if(word.length() == 0) return;

if(this.child == null) {
this.child = new Trie[26];
}

char c = word.charAt(0);
int idx = c - 97;

if(this.child[idx] == null) {
this.child[idx] = new Trie();
this.child[idx].val = c;
}

this.child[idx].innerInsert(word.substring(1));
}

public boolean search(String word) {
return this.tMap.containsKey(word);
return this.cMap.containsKey(word);
}

// public boolean search(String word) {

// }

public boolean startsWith(String prefix) {
for(String key : this.tMap.keySet()) {
if(key.startsWith(prefix)) return true;
public boolean startsWith(String word) {
if(word.length() == 0) {
return true;
}

return false;
char c = word.charAt(0);
int idx = c - 97;
if(this.child == null || this.child[idx] == null || this.child[idx].val == null) return false;


return this.child[idx].startsWith(word.substring(1));
}
}

Expand All @@ -35,3 +68,32 @@ public boolean startsWith(String prefix) {
* boolean param_2 = obj.search(word);
* boolean param_3 = obj.startsWith(prefix);
*/


// Map으로 풀려버려서 당황..
// 이진트리? 어떤식으로 풀어야 할지 자료구조 정하고 다시 풀어보기..
class BeforeTrie {

Map<String, Boolean> tMap;

public BeforeTrie() {
this.tMap = new HashMap<>();
}

public void insert(String word) {
this.tMap.put(word, true);
}

public boolean search(String word) {
return this.tMap.containsKey(word);
}

public boolean startsWith(String prefix) {
for(String key : this.tMap.keySet()) {
if(key.startsWith(prefix)) return true;
}

return false;
}
}

Loading