File tree Expand file tree Collapse file tree
best-time-to-buy-and-sell-stock
encode-and-decode-strings
implement-trie-prefix-tree Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -6,8 +6,8 @@ class Solution {
66 int len = prices.size ();
77 for (int i = 0 ; i < len; ++i)
88 {
9- res = max (res, prices[i] - minPrice);
10- minPrice = min (minPrice, prices[i]);
9+ res = max (res, prices[i] - minPrice); // 수익 최대화: 지금까지 가장 쌌던 날에 사서 오늘 팔기
10+ minPrice = min (minPrice, prices[i]); // '지금까지 가장 쌌던 날' 업데이트
1111 }
1212
1313 return res;
Original file line number Diff line number Diff line change @@ -7,7 +7,7 @@ class Codec {
77 for (auto & s : strs)
88 {
99 str += s;
10- str.push_back (3 );
10+ str.push_back (3 ); // 문제의 입력으로 오지 않는 값(ETX, End of Text)을 구분자로 사용
1111 }
1212
1313 return str;
@@ -27,7 +27,3 @@ class Codec {
2727 return v;
2828 }
2929};
30-
31- // Your Codec object will be instantiated and called as such:
32- // Codec codec;
33- // codec.decode(codec.encode(strs));
Original file line number Diff line number Diff line change 11class Solution {
22private:
3+ // 단어별로 알파벳 개수를 세는 구조체
34 struct chnum
45 {
56 int cnt[26 ];
@@ -34,16 +35,16 @@ struct chnumHash
3435
3536public:
3637 vector<vector<string>> groupAnagrams (vector<string>& strs) {
37- unordered_map<chnum, vector<string>, chnumHash> ns;
38+ unordered_map<chnum, vector<string>, chnumHash> ns; // 알파벳 개수를 넣으면 알파벳 구성을 갖는 문자열 배열이 나옴
3839 for (auto & str : strs)
3940 {
4041 chnum n;
4142 for (auto ch : str)
4243 {
43- n.cnt [ch - ' a' ]++;
44+ n.cnt [ch - ' a' ]++; // 알파벳 개수를 세고...
4445 }
4546
46- ns[n].push_back (str);
47+ ns[n].push_back (str); // 해시맵에 넣음
4748 }
4849
4950 vector<vector<string>> v;
Original file line number Diff line number Diff line change @@ -60,8 +60,8 @@ class Trie
6060private:
6161 struct Node
6262 {
63- bool isWord = false ;
64- unique_ptr<Node> children[26 ];
63+ bool isWord = false ; // 이 노드가 단어의 끝인지 저장
64+ unique_ptr<Node> children[26 ]; // 자식 노드 저장
6565 };
6666
6767 unique_ptr<Node> mRoot ;
Original file line number Diff line number Diff line change @@ -5,35 +5,42 @@ class Solution {
55 return solve (0 , wordDict);
66 }
77
8+ // substr 시작 인덱스와 wordDict를 입력으로 받음
89 bool solve (int start, vector<string>& wordDict)
910 {
11+ // 이미 계산한 적 있으면 메모이제이션으로 해결
1012 if (memo.contains (start))
1113 {
1214 return memo[start];
1315 }
1416
17+ // 베이스컨디션 -> 끝까지 도달했으면 true
1518 if (start == s.size ())
1619 {
1720 return true ;
1821 }
1922
2023 for (auto & str : wordDict)
2124 {
25+ // wordDict에 있는 단어들로 시작하는지 확인
2226 if (s.substr (start, str.size ()) == str)
2327 {
28+ // 그리고 그 뒷 부분도 재귀를 통해 확인
2429 if (solve (start + str.size (), wordDict))
2530 {
31+ // 뒷 부분까지 통과했으면 true
2632 memo[start] = true ;
2733 return true ;
2834 }
2935 }
3036 }
3137
38+ // 통과 못했으면 false
3239 memo[start] = false ;
3340 return false ;
3441 }
3542
3643private:
3744 string s;
38- unordered_map<int , bool > memo;
45+ unordered_map<int , bool > memo; // 메모이제이션
3946};
You can’t perform that action at this time.
0 commit comments