Skip to content

Commit c2ae73d

Browse files
authored
Merge pull request #1337 from haung921209/week04
[haung921209] Week 04 solutions
2 parents 52fb2a1 + 4d7c6cd commit c2ae73d

File tree

5 files changed

+411
-0
lines changed

5 files changed

+411
-0
lines changed

β€Žcoin-change/haung921209.md

+76
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
# μ—°κ΄€ 링크
2+
- [문제 풀이 μŠ€μΌ€μ€„](https://github.com/orgs/DaleStudy/projects/6/views/5)
3+
- [λ‹΅μ•ˆ μ½”λ“œ μ œμΆœλ²•](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)
4+
5+
# Problem
6+
- 문제 링크 : https://leetcode.com/problems/coin-change/description/
7+
- 문제 이름 : Coin Change
8+
- 문제 번호 : 322
9+
- λ‚œμ΄λ„ : Medium
10+
- μΉ΄ν…Œκ³ λ¦¬ : BFS
11+
12+
# 문제 μ„€λͺ…
13+
14+
15+
# 아이디어
16+
- bfs
17+
18+
# βœ… μ½”λ“œ (Solution)
19+
```
20+
class Solution {
21+
public:
22+
int coinChange(vector<int>& coins, int amount) {
23+
if(amount==0){
24+
return 0;
25+
}
26+
vector<int> res(amount + 1, -1);
27+
queue<int> q;
28+
for (auto coin : coins) {
29+
if (coin > amount) {
30+
continue;
31+
}
32+
if (coin == amount) {
33+
return 1;
34+
}
35+
res[coin] = 1;
36+
q.push(coin);
37+
}
38+
while (q.size()) {
39+
int curCoin = q.front();
40+
q.pop();
41+
int nextCount = res[curCoin] + 1;
42+
for (auto coin : coins) {
43+
int nextCoin = curCoin + coin;
44+
if (nextCoin > amount) {
45+
continue;
46+
}
47+
if (res[nextCoin] != -1) {
48+
continue;
49+
}
50+
if (nextCoin == amount) {
51+
return nextCount;
52+
}
53+
res[nextCoin] = nextCount;
54+
q.push(nextCoin);
55+
}
56+
}
57+
return res[amount];
58+
}
59+
};
60+
61+
```
62+
# πŸ” μ½”λ“œ μ„€λͺ…
63+
64+
65+
# μ΅œμ ν™” 포인트 (Optimality Discussion)
66+
β€’ μ΅œμ ν™”ν•œ μ΄μœ μ™€ 원리
67+
β€’ 더 쀄일 수 μžˆλŠ” μ—¬μ§€λŠ” μžˆλŠ”κ°€?
68+
β€’ κΈ°μ‘΄ 방법 λŒ€λΉ„ μ–Όλ§ˆλ‚˜ νš¨μœ¨μ μ΄μ—ˆλŠ”μ§€
69+
70+
# πŸ§ͺ ν…ŒμŠ€νŠΈ & μ—£μ§€ μΌ€μ΄μŠ€
71+
72+
# πŸ“š κ΄€λ ¨ 지식 볡슡
73+
74+
# πŸ” 회고
75+
76+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
# μ—°κ΄€ 링크
2+
- [문제 풀이 μŠ€μΌ€μ€„](https://github.com/orgs/DaleStudy/projects/6/views/5)
3+
- [λ‹΅μ•ˆ μ½”λ“œ μ œμΆœλ²•](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)
4+
5+
# Problem
6+
- 문제 링크 :
7+
- 문제 이름 :
8+
- 문제 번호 :
9+
- λ‚œμ΄λ„ :
10+
- μΉ΄ν…Œκ³ λ¦¬ :
11+
12+
# 문제 μ„€λͺ…
13+
14+
15+
# 아이디어
16+
- μ–΄λ–€ λ°©λ²•μœΌλ‘œ μ ‘κ·Όν–ˆλŠ”μ§€ μ„œμˆ 
17+
- 포슀 vs μ΅œμ ν™” 아이디어 차이 λ“±
18+
- μž‘λ„μ— λŒ€ν•œ κ³ λ €
19+
20+
# βœ… μ½”λ“œ (Solution)
21+
## Linear Search
22+
```cpp
23+
class Solution {
24+
public:
25+
int findMin(vector<int>& nums) {
26+
int res = nums[0];
27+
for(int i=1;i<nums.size();i++){
28+
res = min(res, nums[i]);
29+
}
30+
return res;
31+
}
32+
};
33+
```
34+
35+
- O(n) - pass
36+
- n이 μž‘μ•„μ„œ 패슀
37+
38+
## Binary Search
39+
40+
41+
```cpp
42+
class Solution {
43+
public:
44+
int findMin(vector<int>& nums) {
45+
int left = 0, right = nums.size() - 1;
46+
47+
while (left < right) {
48+
int mid = left + (right - left) / 2;
49+
50+
if (nums[mid] > nums[right]) {
51+
left = mid + 1;
52+
} else {
53+
right = mid;
54+
}
55+
}
56+
57+
return nums[left];
58+
}
59+
};
60+
```
61+
62+
- O(log n)
63+
- The Exact Solution they want...!
64+
65+
# πŸ” μ½”λ“œ μ„€λͺ…
66+
67+
68+
# μ΅œμ ν™” 포인트 (Optimality Discussion)
69+
- Binary Search λ₯Ό ν†΅ν•œ O(log n) 처리
70+
- Binary Search에 λŒ€ν•œ μ—°μŠ΅μ΄ ν•„μš”ν•˜λ‹€
71+
72+
73+
# πŸ§ͺ ν…ŒμŠ€νŠΈ & μ—£μ§€ μΌ€μ΄μŠ€
74+
75+
# πŸ“š κ΄€λ ¨ 지식 볡슡
76+
- Binary Search에 λŒ€ν•œ μ—°μŠ΅μ΄ ν•„μš”ν•˜λ‹€
77+
78+
79+
# πŸ” 회고
80+
81+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
# μ—°κ΄€ 링크
2+
- [문제 풀이 μŠ€μΌ€μ€„](https://github.com/orgs/DaleStudy/projects/6/views/5)
3+
- [λ‹΅μ•ˆ μ½”λ“œ μ œμΆœλ²•](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)
4+
5+
# Problem
6+
- 문제 링크 : https://leetcode.com/problems/maximum-depth-of-binary-tree/description/
7+
- 문제 이름 : Maximum Depth of Binary Tree
8+
- 문제 번호 : 104
9+
- λ‚œμ΄λ„ : easy
10+
- μΉ΄ν…Œκ³ λ¦¬ :
11+
12+
# 문제 μ„€λͺ…
13+
14+
15+
# 아이디어
16+
- μž¬κ·€
17+
18+
# βœ… μ½”λ“œ (Solution)
19+
20+
```cpp
21+
/**
22+
* Definition for a binary tree node.
23+
* struct TreeNode {
24+
* int val;
25+
* TreeNode *left;
26+
* TreeNode *right;
27+
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
28+
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
29+
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
30+
* };
31+
*/
32+
class Solution {
33+
public:
34+
int maxDepth(TreeNode* root) {
35+
if(!root)
36+
return 0;
37+
return 1 + max(maxDepth(root->left), maxDepth(root->right));
38+
}
39+
};
40+
```
41+
42+
43+
# πŸ” μ½”λ“œ μ„€λͺ…
44+
45+
46+
# μ΅œμ ν™” 포인트 (Optimality Discussion)
47+
β€’ μ΅œμ ν™”ν•œ μ΄μœ μ™€ 원리
48+
β€’ 더 쀄일 수 μžˆλŠ” μ—¬μ§€λŠ” μžˆλŠ”κ°€?
49+
β€’ κΈ°μ‘΄ 방법 λŒ€λΉ„ μ–Όλ§ˆλ‚˜ νš¨μœ¨μ μ΄μ—ˆλŠ”μ§€
50+
51+
# πŸ§ͺ ν…ŒμŠ€νŠΈ & μ—£μ§€ μΌ€μ΄μŠ€
52+
53+
# πŸ“š κ΄€λ ¨ 지식 볡슡
54+
55+
# πŸ” 회고
56+
57+
+121
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
# μ—°κ΄€ 링크
2+
- [문제 풀이 μŠ€μΌ€μ€„](https://github.com/orgs/DaleStudy/projects/6/views/5)
3+
- [λ‹΅μ•ˆ μ½”λ“œ μ œμΆœλ²•](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)
4+
5+
# Problem
6+
- 문제 링크 : http://leetcode.com/problems/merge-two-sorted-lists/
7+
- 문제 이름 : Merge Two Sorted Lists
8+
- 문제 번호 : 21
9+
- λ‚œμ΄λ„ : easy
10+
- μΉ΄ν…Œκ³ λ¦¬ :
11+
12+
# 문제 μ„€λͺ…
13+
14+
15+
# 아이디어
16+
- μ–΄λ–€ λ°©λ²•μœΌλ‘œ μ ‘κ·Όν–ˆλŠ”μ§€ μ„œμˆ 
17+
- 포슀 vs μ΅œμ ν™” 아이디어 차이 λ“±
18+
- μž‘λ„μ— λŒ€ν•œ κ³ λ €
19+
20+
# βœ… μ½”λ“œ (Solution)
21+
##
22+
```cpp
23+
/**
24+
* Definition for singly-linked list.
25+
* struct ListNode {
26+
* int val;
27+
* ListNode *next;
28+
* ListNode() : val(0), next(nullptr) {}
29+
* ListNode(int x) : val(x), next(nullptr) {}
30+
* ListNode(int x, ListNode *next) : val(x), next(next) {}
31+
* };
32+
*/
33+
class Solution {
34+
public:
35+
ListNode* mergeTwoLists(ListNode* list1, ListNode* list2) {
36+
auto head = new ListNode();
37+
auto cur = head;
38+
39+
while(true){
40+
if(list1 == nullptr && list2 == nullptr){
41+
break;
42+
}
43+
if(list1==nullptr){
44+
cur->next = new ListNode(list2->val);
45+
list2 = list2->next;
46+
cur = cur->next;
47+
continue;
48+
}
49+
50+
if(list2 == nullptr){
51+
cur->next = new ListNode(list1->val);
52+
list1 = list1->next;
53+
cur = cur->next;
54+
continue;
55+
}
56+
57+
if(list1->val > list2->val){
58+
cur->next = new ListNode(list2->val);
59+
list2 = list2->next;
60+
cur = cur->next;
61+
continue;
62+
}else{
63+
cur->next = new ListNode(list1->val);
64+
list1 = list1->next;
65+
cur = cur->next;
66+
continue;
67+
}
68+
69+
70+
}
71+
72+
return head->next;
73+
}
74+
};
75+
76+
```
77+
78+
## κ°„μ†Œν™” 버전
79+
```cpp
80+
class Solution {
81+
public:
82+
ListNode* mergeTwoLists(ListNode* list1, ListNode* list2) {
83+
ListNode dummy;
84+
ListNode* cur = &dummy;
85+
86+
while (list1 && list2) {
87+
if (list1->val < list2->val) {
88+
cur->next = list1;
89+
list1 = list1->next;
90+
} else {
91+
cur->next = list2;
92+
list2 = list2->next;
93+
}
94+
cur = cur->next;
95+
}
96+
97+
cur->next = list1 ? list1 : list2;
98+
99+
return dummy.next;
100+
}
101+
};
102+
103+
```
104+
105+
- 인자 μˆ˜μ • -> λ©”λͺ¨λ¦¬ μž¬ν™œμš©
106+
107+
# πŸ” μ½”λ“œ μ„€λͺ…
108+
109+
110+
# μ΅œμ ν™” 포인트 (Optimality Discussion)
111+
β€’ μ΅œμ ν™”ν•œ μ΄μœ μ™€ 원리
112+
β€’ 더 쀄일 수 μžˆλŠ” μ—¬μ§€λŠ” μžˆλŠ”κ°€?
113+
β€’ κΈ°μ‘΄ 방법 λŒ€λΉ„ μ–Όλ§ˆλ‚˜ νš¨μœ¨μ μ΄μ—ˆλŠ”μ§€
114+
115+
# πŸ§ͺ ν…ŒμŠ€νŠΈ & μ—£μ§€ μΌ€μ΄μŠ€
116+
117+
# πŸ“š κ΄€λ ¨ 지식 볡슡
118+
119+
# πŸ” 회고
120+
121+

0 commit comments

Comments
Β (0)