Skip to content

[haung921209] Week 04 solutions #1337

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 5 commits into from
Apr 27, 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
76 changes: 76 additions & 0 deletions coin-change/haung921209.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
# 연관 링크
- [문제 풀이 스케줄](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/coin-change/description/
- 문제 이름 : Coin Change
- 문제 번호 : 322
- 난이도 : Medium
- 카테고리 : BFS

# 문제 설명


# 아이디어
- bfs

# ✅ 코드 (Solution)
```
class Solution {
public:
int coinChange(vector<int>& coins, int amount) {
if(amount==0){
return 0;
}
vector<int> res(amount + 1, -1);
queue<int> q;
for (auto coin : coins) {
if (coin > amount) {
continue;
}
if (coin == amount) {
return 1;
}
res[coin] = 1;
q.push(coin);
}
while (q.size()) {
int curCoin = q.front();
q.pop();
int nextCount = res[curCoin] + 1;
for (auto coin : coins) {
int nextCoin = curCoin + coin;
if (nextCoin > amount) {
continue;
}
if (res[nextCoin] != -1) {
continue;
}
if (nextCoin == amount) {
return nextCount;
}
res[nextCoin] = nextCount;
q.push(nextCoin);
}
}
return res[amount];
}
};

```
# 🔍 코드 설명


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

# 🧪 테스트 & 엣지 케이스

# 📚 관련 지식 복습

# 🔁 회고


81 changes: 81 additions & 0 deletions find-minimum-in-rotated-sorted-array/haung921209.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
# 연관 링크
- [문제 풀이 스케줄](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
- 문제 링크 :
- 문제 이름 :
- 문제 번호 :
- 난이도 :
- 카테고리 :

# 문제 설명


# 아이디어
- 어떤 방법으로 접근했는지 서술
- 포스 vs 최적화 아이디어 차이 등
- 잡도에 대한 고려

# ✅ 코드 (Solution)
## Linear Search
```cpp
class Solution {
public:
int findMin(vector<int>& nums) {
int res = nums[0];
for(int i=1;i<nums.size();i++){
res = min(res, nums[i]);
}
return res;
}
};
```

- O(n) - pass
- n이 작아서 패스

## Binary Search


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

while (left < right) {
int mid = left + (right - left) / 2;

if (nums[mid] > nums[right]) {
left = mid + 1;
} else {
right = mid;
}
}

return nums[left];
}
};
```

- O(log n)
- The Exact Solution they want...!

# 🔍 코드 설명


# 최적화 포인트 (Optimality Discussion)
- Binary Search 를 통한 O(log n) 처리
- Binary Search에 대한 연습이 필요하다


# 🧪 테스트 & 엣지 케이스

# 📚 관련 지식 복습
- Binary Search에 대한 연습이 필요하다


# 🔁 회고


57 changes: 57 additions & 0 deletions maximum-depth-of-binary-tree/haung921209.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
# 연관 링크
- [문제 풀이 스케줄](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/maximum-depth-of-binary-tree/description/
- 문제 이름 : Maximum Depth of Binary Tree
- 문제 번호 : 104
- 난이도 : easy
- 카테고리 :

# 문제 설명


# 아이디어
- 재귀

# ✅ 코드 (Solution)

```cpp
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
* };
*/
class Solution {
public:
int maxDepth(TreeNode* root) {
if(!root)
return 0;
return 1 + max(maxDepth(root->left), maxDepth(root->right));
}
};
```


# 🔍 코드 설명


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

# 🧪 테스트 & 엣지 케이스

# 📚 관련 지식 복습

# 🔁 회고


121 changes: 121 additions & 0 deletions merge-two-sorted-lists/haung921209.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
# 연관 링크
- [문제 풀이 스케줄](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
- 문제 링크 : http://leetcode.com/problems/merge-two-sorted-lists/
- 문제 이름 : Merge Two Sorted Lists
- 문제 번호 : 21
- 난이도 : easy
- 카테고리 :

# 문제 설명


# 아이디어
- 어떤 방법으로 접근했는지 서술
- 포스 vs 최적화 아이디어 차이 등
- 잡도에 대한 고려

# ✅ 코드 (Solution)
##
```cpp
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode() : val(0), next(nullptr) {}
* ListNode(int x) : val(x), next(nullptr) {}
* ListNode(int x, ListNode *next) : val(x), next(next) {}
* };
*/
class Solution {
public:
ListNode* mergeTwoLists(ListNode* list1, ListNode* list2) {
auto head = new ListNode();
auto cur = head;

while(true){
if(list1 == nullptr && list2 == nullptr){
break;
}
if(list1==nullptr){
cur->next = new ListNode(list2->val);
list2 = list2->next;
cur = cur->next;
continue;
}

if(list2 == nullptr){
cur->next = new ListNode(list1->val);
list1 = list1->next;
cur = cur->next;
continue;
}

if(list1->val > list2->val){
cur->next = new ListNode(list2->val);
list2 = list2->next;
cur = cur->next;
continue;
}else{
cur->next = new ListNode(list1->val);
list1 = list1->next;
cur = cur->next;
continue;
}


}

return head->next;
}
};

```

## 간소화 버전
```cpp
class Solution {
public:
ListNode* mergeTwoLists(ListNode* list1, ListNode* list2) {
ListNode dummy;
ListNode* cur = &dummy;

while (list1 && list2) {
if (list1->val < list2->val) {
cur->next = list1;
list1 = list1->next;
} else {
cur->next = list2;
list2 = list2->next;
}
cur = cur->next;
}

cur->next = list1 ? list1 : list2;

return dummy.next;
}
};

```

- 인자 수정 -> 메모리 재활용

# 🔍 코드 설명


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

# 🧪 테스트 & 엣지 케이스

# 📚 관련 지식 복습

# 🔁 회고


Loading