Skip to content

[haung921209] Week 06 Solutions #1439

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 4 commits into from
May 18, 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
79 changes: 79 additions & 0 deletions container-with-most-water/haung921209.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
# 연관 링크
- [문제 풀이 스케줄](https://github.com/orgs/DaleStudy/projects/6/views/5)
- [답안 코드 제출법](https://github.com/DaleStudy/leetcode-study/wiki/%EB%8B%B5%EC%95%88-%EC%A0%9C%EC%B6%9C-%EA%B0%80%EC%9D%B4%EB%93%9C)

# Problem
- 문제 링크 : https://leetcode.com/problems/container-with-most-water/description/
- 문제 이름 : Container with most water
- 문제 번호 : 11
- 난이도 : medium
- 카테고리 :

# 문제 설명

두 지점을 벽으로 선정해, 최대의 물이 담기는 경우를 찾는 문제


# 아이디어
- two pointer

# ✅ 코드 (Solution)

## brute force

```cpp
class Solution {
public:
int maxArea(vector<int>& height) {
int maxSize = 0;
for(int i=0;i<height.size();i++){
for(int j=i+1;j<height.size();j++){
maxSize = max(maxSize, (j-i)*min(height[j], height[i]));
}
}
return maxSize;
}
};
```
- brute force
- O(n ^ 2)
- tle
## two pointer

```cpp
class Solution {
public:
int maxArea(vector<int>& height) {
int left = 0, right = height.size()-1;
int maxArea = 0;

while(left<right){
maxArea = max((right-left) * min(height[left], height[right]), maxArea);

if(height[left]>height[right]){
right--;
}else{
left++;
}
}
return maxArea;
}
};

```
- two pointer
- O(n)

# 🔍 코드 설명


# 최적화 포인트 (Optimality Discussion)
- two pointer

# 🧪 테스트 & 엣지 케이스

# 📚 관련 지식 복습

# 🔁 회고


164 changes: 164 additions & 0 deletions design-add-and-search-words-data-structure/haung921209.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,164 @@
# 연관 링크
- [문제 풀이 스케줄](https://github.com/orgs/DaleStudy/projects/6/views/5)
- [답안 코드 제출법](https://github.com/DaleStudy/leetcode-study/wiki/%EB%8B%B5%EC%95%88-%EC%A0%9C%EC%B6%9C-%EA%B0%80%EC%9D%B4%EB%93%9C)

# Problem
- 문제 링크 : https://leetcode.com/problems/design-add-and-search-words-data-structure/description/
- 문제 이름 : design add and search words data structure
- 문제 번호 : 211
- 난이도 : medium
- 카테고리 :

# 문제 설명
- 데이터베이스에 input word를 더하고 찾는 자료구조 디자인하기

# 아이디어
- 이전 주차 word search와 동일한 접근 방법으로 풀이 가능
### Trie
```cpp
class TrieNode{
public:
bool isCompleteWord;
TrieNode* children[26];

TrieNode() {
isCompleteWord = false;
memset(children, 0, sizeof(children));
}
};
```

- 가지 중간인지, 아니면 완결된 단어인지를 확인할 수 있음.
- 해당 로직을 통해, Prefix 확인하는 메서드 또한 쉽게 만들 수 있음.

# ✅ 코드 (Solution)

## Trie - wrong answer
```cpp
class TrieNode{
public:
bool isCompleteWord;
TrieNode* children[26];

TrieNode() {
isCompleteWord = false;
memset(children, 0, sizeof(children));
}
};

class WordDictionary {
TrieNode* root;
public:
WordDictionary() {
root = new TrieNode();
}

void addWord(string word) {
auto cur = root;
for(auto c: word){
if(!cur->children[c-'a']){
cur->children[c-'a'] = new TrieNode();
}
cur = cur->children[c-'a'];
}
cur->isCompleteWord = true;
}

bool search(string word) {
auto cur = root;
for(auto c: word){
if(!cur->children[c-'a']){
return false;
}
cur = cur->children[c-'a'];
}
return cur->isCompleteWord;
}
};

/**
* Your WordDictionary object will be instantiated and called as such:
* WordDictionary* obj = new WordDictionary();
* obj->addWord(word);
* bool param_2 = obj->search(word);
*/
```

- 잘못 작성한 코드
- 자료구조 적용 중, edge case(`.` - dot)을 고려하지 않음
- 아는 문제라고 쉽게 생각한 것이 잘못.

```cpp
class TrieNode {
public:
bool isCompleteWord;
TrieNode* children[26];

TrieNode() {
isCompleteWord = false;
memset(children, 0, sizeof(children));
}
};

class WordDictionary {
TrieNode* root;

bool dfs(TrieNode* node, const string& word, int index) {
if (!node) return false;
if (index == word.size()) return node->isCompleteWord;

char c = word[index];
if (c == '.') {
// '.'이므로 모든 자식을 시도해본다
for (int i = 0; i < 26; ++i) {
if (dfs(node->children[i], word, index + 1)) {
return true;
}
}
return false;
} else {
return dfs(node->children[c - 'a'], word, index + 1);
}
}

public:
WordDictionary() {
root = new TrieNode();
}

void addWord(string word) {
TrieNode* cur = root;
for (char c : word) {
int idx = c - 'a';
if (!cur->children[idx]) {
cur->children[idx] = new TrieNode();
}
cur = cur->children[idx];
}
cur->isCompleteWord = true;
}

bool search(string word) {
return dfs(root, word, 0);
}
};

```

- dfs로 구현

# 🔍 코드 설명


# 최적화 포인트 (Optimality Discussion)
• 최적화한 이유와 원리
• 더 줄일 수 있는 여지는 있는가?
• 기존 방법 대비 얼마나 효율적이었는지

# 🧪 테스트 & 엣지 케이스

# 📚 관련 지식 복습

# 🔁 회고


99 changes: 99 additions & 0 deletions valid-parentheses/haung921209.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
# 연관 링크
- [문제 풀이 스케줄](https://github.com/orgs/DaleStudy/projects/6/views/5)
- [답안 코드 제출법](https://github.com/DaleStudy/leetcode-study/wiki/%EB%8B%B5%EC%95%88-%EC%A0%9C%EC%B6%9C-%EA%B0%80%EC%9D%B4%EB%93%9C)

# Problem
- 문제 링크 : https://leetcode.com/problems/valid-parentheses/description/
- 문제 이름 : valid parentheses
- 문제 번호 : 20
- 난이도 : easy
- 카테고리 :

# 문제 설명



# 아이디어
- stack을 통한 유효성 체크
- brute force

# ✅ 코드 (Solution)


```cpp
class Solution {
public:
bool isValid(string s) {
stack<char> st;
for(int i=0;i<s.size();i++){
auto cur = s[i];
if(cur == '(' || cur == '{' || cur == '['){
st.push(cur);
continue;
}else{
if(st.size()==0){
return false;
}
if(cur == ']'){
if(st.top() == '['){
st.pop();
continue;
}else{
return false;
}
}else if(cur == '}'){
if(st.top()=='{'){
st.pop();
continue;
}else{
return false;
}
}else {
if(st.top()=='('){
st.pop();
continue;
}else{
return false;
}
}
}
}
return st.size()==0;
}
};


```

### 간략화
```cpp
class Solution {
public:
bool isValid(string s) {
stack<char> st;
for (char c : s) {
if (c == '(') st.push(')');
else if (c == '{') st.push('}');
else if (c == '[') st.push(']');
else {
if (st.empty() || st.top() != c) return false;
st.pop();
}
}
return st.empty();
}
};
```

# 🔍 코드 설명


# 최적화 포인트 (Optimality Discussion)
- 깔끔하게 만듦
# 🧪 테스트 & 엣지 케이스

# 📚 관련 지식 복습

# 🔁 회고