Skip to content

Commit c7c4ea6

Browse files
authored
Merge pull request #1388 from haung921209/week05
[haung921209] Week 05 Solutions
2 parents 2e944b3 + 58f3ac6 commit c7c4ea6

File tree

2 files changed

+113
-0
lines changed

2 files changed

+113
-0
lines changed
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
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/best-time-to-buy-and-sell-stock/
7+
- ๋ฌธ์ œ ์ด๋ฆ„ : Best Time to Buy and Sell Stock
8+
- ๋ฌธ์ œ ๋ฒˆํ˜ธ : 121
9+
- ๋‚œ์ด๋„ : easy
10+
- ์นดํ…Œ๊ณ ๋ฆฌ :
11+
12+
# ๋ฌธ์ œ ์„ค๋ช…
13+
14+
15+
# ์•„์ด๋””์–ด
16+
- ์–ด๋–ค ๋ฐฉ๋ฒ•์œผ๋กœ ์ ‘๊ทผํ–ˆ๋Š”์ง€ ์„œ์ˆ 
17+
- ํฌ์Šค vs ์ตœ์ ํ™” ์•„์ด๋””์–ด ์ฐจ์ด ๋“ฑ
18+
- ์žก๋„์— ๋Œ€ํ•œ ๊ณ ๋ ค
19+
20+
# โœ… ์ฝ”๋“œ (Solution)
21+
22+
```cpp
23+
class Solution {
24+
public:
25+
int maxProfit(vector<int>& prices) {
26+
int localMax = prices[prices.size()-1];
27+
int res = 0;
28+
for(int i=prices.size()-2;i>=0;i--){
29+
localMax = max(localMax, prices[i]);
30+
res = max(res, localMax-prices[i]);
31+
}
32+
33+
return res;
34+
}
35+
};
36+
```
37+
38+
# ๐Ÿ” ์ฝ”๋“œ ์„ค๋ช…
39+
40+
- local max๋ฅผ ์„ค์ •
41+
42+
# ์ตœ์ ํ™” ํฌ์ธํŠธ (Optimality Discussion)
43+
โ€ข ์ตœ์ ํ™”ํ•œ ์ด์œ ์™€ ์›๋ฆฌ
44+
โ€ข ๋” ์ค„์ผ ์ˆ˜ ์žˆ๋Š” ์—ฌ์ง€๋Š” ์žˆ๋Š”๊ฐ€?
45+
โ€ข ๊ธฐ์กด ๋ฐฉ๋ฒ• ๋Œ€๋น„ ์–ผ๋งˆ๋‚˜ ํšจ์œจ์ ์ด์—ˆ๋Š”์ง€
46+
47+
# ๐Ÿงช ํ…Œ์ŠคํŠธ & ์—ฃ์ง€ ์ผ€์ด์Šค
48+
49+
# ๐Ÿ“š ๊ด€๋ จ ์ง€์‹ ๋ณต์Šต
50+
51+
# ๐Ÿ” ํšŒ๊ณ 
52+
53+

โ€Žgroup-anagrams/haung921209.md

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
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/group-anagrams/description/
7+
- ๋ฌธ์ œ ์ด๋ฆ„ : Group Anagrams
8+
- ๋ฌธ์ œ ๋ฒˆํ˜ธ : 49
9+
- ๋‚œ์ด๋„ : medium
10+
- ์นดํ…Œ๊ณ ๋ฆฌ :
11+
12+
# ๋ฌธ์ œ ์„ค๋ช…
13+
14+
15+
# ์•„์ด๋””์–ด
16+
- string์„ ์ •๋ ฌ -> key๋กœ ์‚ฌ์šฉ ๊ฐ€๋Šฅ
17+
- ์ •๋ ฌ์„ ์ด์šฉํ•ด ์„œ๋กœ ๋‹ค๋ฅธ permutation(anagram)๋“ค์„ ๊ฐ™์€ ๊ทธ๋ฃน์œผ๋กœ ๋ฌถ์Œ.
18+
- unordered_map์„ ์‚ฌ์šฉํ•ด์„œ key lookup์ด ํ‰๊ท ์ ์œผ๋กœ **O(1)**์— ๊ฐ€๋Šฅํ•˜๋ฏ€๋กœ ์ „์ฒด ์•Œ๊ณ ๋ฆฌ์ฆ˜์€ ๋งค์šฐ ํšจ์œจ์ ์ž„.
19+
20+
# โœ… ์ฝ”๋“œ (Solution)
21+
```
22+
class Solution {
23+
public:
24+
vector<vector<string>> groupAnagrams(vector<string>& strs) {
25+
vector<vector<string>> res;
26+
unordered_map<string, vector<string>> um;
27+
for(auto str: strs){
28+
string key= str;
29+
sort(key.begin(), key.end());
30+
um[key].push_back(str);
31+
}
32+
for(auto vec = um.begin();vec != um.end(); vec++){
33+
res.push_back(vec->second);
34+
}
35+
return res;
36+
}
37+
};
38+
39+
```
40+
# ๐Ÿ” ์ฝ”๋“œ ์„ค๋ช…
41+
- length๊ฐ€ 10^4 ์ดํ•˜ + sort -> input ์ƒ ์ƒ์ˆ˜ ์‹œ๊ฐ„ ๋‚ด ์ฒ˜๋ฆฌ ๊ฐ€๋Šฅ
42+
- ์ •๋ ฌ์„ ์ด์šฉํ•ด ์„œ๋กœ ๋‹ค๋ฅธ permutation(anagram)๋“ค์„ ๊ฐ™์€ ๊ทธ๋ฃน์œผ๋กœ ๋ฌถ์Œ.
43+
- unordered_map์„ ์‚ฌ์šฉํ•ด์„œ key lookup์ด ํ‰๊ท ์ ์œผ๋กœ **O(1)**์— ๊ฐ€๋Šฅํ•˜๋ฏ€๋กœ ์ „์ฒด ์•Œ๊ณ ๋ฆฌ์ฆ˜์€ ๋งค์šฐ ํšจ์œจ์ ์ž„.
44+
-
45+
46+
# ์ตœ์ ํ™” ํฌ์ธํŠธ (Optimality Discussion)
47+
- ๋ถˆํ•„์š”ํ•œ ๋ฌธ์ž์—ด ๋ณต์‚ฌ ๋ฐฉ์ง€:
48+
- for(auto& str : strs) ์ฒ˜๋Ÿผ auto ๋’ค์— &(๋ ˆํผ๋Ÿฐ์Šค) ๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด, str์„ ๋ณต์‚ฌํ•˜์ง€ ์•Š๊ณ  ์ฐธ์กฐ๋กœ ๋ฐ›์•„ ๋ฉ”๋ชจ๋ฆฌ ์‚ฌ์šฉ์„ ์ค„์ผ ์ˆ˜ ์žˆ์Œ.
49+
- key string ์ƒ์„ฑ ๋น„์šฉ ์ค„์ด๊ธฐ:
50+
- ๋ฌธ์ž์—ด์„ ๋งค๋ฒˆ ์ •๋ ฌํ•˜๋ฉด O(K log K) (K๋Š” ๋ฌธ์ž์—ด ๊ธธ์ด) ๋น„์šฉ์ด ๋“ฆ.
51+
- ๋งŒ์•ฝ ๋ชจ๋“  ์ž…๋ ฅ ๋ฌธ์ž์—ด์ด ์˜์–ด ์†Œ๋ฌธ์ž(a~z)๋กœ ์ œํ•œ๋œ๋‹ค๋ฉด, 26๊ฐœ ์•ŒํŒŒ๋ฒณ์˜ ๋นˆ๋„์ˆ˜ ๋ฐฐ์—ด์„ ์‚ฌ์šฉํ•ด์„œ O(K)๋กœ key๋ฅผ ๋งŒ๋“ค ์ˆ˜๋„ ์žˆ์Œ.
52+
- ์ด ๊ฒฝ์šฐ ์ •๋ ฌ์ด ์•„๋‹ˆ๋ผ counting ๊ธฐ๋ฐ˜ key ์ƒ์„ฑ.
53+
54+
# ๐Ÿงช ํ…Œ์ŠคํŠธ & ์—ฃ์ง€ ์ผ€์ด์Šค
55+
56+
# ๐Ÿ“š ๊ด€๋ จ ์ง€์‹ ๋ณต์Šต
57+
58+
# ๐Ÿ” ํšŒ๊ณ 
59+
60+

0 commit comments

Comments
ย (0)