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 1+ class Solution {
2+ public:
3+ int maxProfit (vector<int >& prices) {
4+ int res = 0 ;
5+ int minPrice = prices[0 ];
6+ int len = prices.size ();
7+ for (int i = 0 ; i < len; ++i)
8+ {
9+ res = max (res, prices[i] - minPrice); // 수익 최대화: 지금까지 가장 쌌던 날에 사서 오늘 팔기
10+ minPrice = min (minPrice, prices[i]); // '지금까지 가장 쌌던 날' 업데이트
11+ }
12+
13+ return res;
14+ }
15+ };
Original file line number Diff line number Diff line change 1+ class Codec {
2+ public:
3+
4+ // Encodes a list of strings to a single string.
5+ string encode (vector<string>& strs) {
6+ string str;
7+ for (auto & s : strs)
8+ {
9+ str += s;
10+ str.push_back (3 ); // 문제의 입력으로 오지 않는 값(ETX, End of Text)을 구분자로 사용
11+ }
12+
13+ return str;
14+ }
15+
16+ // Decodes a single string to a list of strings.
17+ vector<string> decode (string s) {
18+ vector<string> v;
19+ v.reserve (200 );
20+ string str_buf;
21+ istringstream iss (s);
22+ while (getline (iss, str_buf, (char )3 ))
23+ {
24+ v.push_back (str_buf);
25+ }
26+
27+ return v;
28+ }
29+ };
Original file line number Diff line number Diff line change 1+ class Solution {
2+ private:
3+ // 단어별로 알파벳 개수를 세는 구조체
4+ struct chnum
5+ {
6+ int cnt[26 ];
7+
8+ chnum ()
9+ {
10+ memset (cnt, 0 , sizeof (int ) * 26 );
11+ }
12+
13+ bool operator ==(const chnum& other) const
14+ {
15+ for (int i = 0 ; i < 26 ; ++i)
16+ {
17+ if (this ->cnt [i] != other.cnt [i]) return false ;
18+ }
19+ return true ;
20+ }
21+ };
22+
23+ struct chnumHash
24+ {
25+ size_t operator ()(const chnum& key) const
26+ {
27+ size_t seed = 5381 ;
28+ for (int i = 0 ; i < 26 ; ++i)
29+ {
30+ seed = ((seed << 5 ) + seed) + key.cnt [i];
31+ }
32+ return seed;
33+ }
34+ };
35+
36+ public:
37+ vector<vector<string>> groupAnagrams (vector<string>& strs) {
38+ unordered_map<chnum, vector<string>, chnumHash> ns; // 알파벳 개수를 넣으면 알파벳 구성을 갖는 문자열 배열이 나옴
39+ for (auto & str : strs)
40+ {
41+ chnum n;
42+ for (auto ch : str)
43+ {
44+ n.cnt [ch - ' a' ]++; // 알파벳 개수를 세고...
45+ }
46+
47+ ns[n].push_back (str); // 해시맵에 넣음
48+ }
49+
50+ vector<vector<string>> v;
51+ v.reserve (ns.size ());
52+
53+ for (auto & p : ns)
54+ {
55+ v.push_back (move (p.second ));
56+ }
57+
58+ return v;
59+ }
60+ };
Original file line number Diff line number Diff line change 1+ class Trie
2+ {
3+ public:
4+ Trie ()
5+ {
6+ mRoot = make_unique<Node>();
7+ }
8+
9+ void insert (string word)
10+ {
11+ Node* curNode = mRoot .get ();
12+ for (char c : word)
13+ {
14+ auto & child = curNode->children [c - ' a' ];
15+ if (child == nullptr )
16+ {
17+ child = make_unique<Node>();
18+ }
19+
20+ curNode = child.get ();
21+ }
22+
23+ curNode->isWord = true ;
24+ }
25+
26+ bool search (string word)
27+ {
28+ Node* curNode = mRoot .get ();
29+ for (char c : word)
30+ {
31+ auto & child = curNode->children [c - ' a' ];
32+ if (child == nullptr )
33+ {
34+ return false ;
35+ }
36+
37+ curNode = child.get ();
38+ }
39+
40+ return curNode->isWord ;
41+ }
42+
43+ bool startsWith (string prefix)
44+ {
45+ Node* curNode = mRoot .get ();
46+ for (char c : prefix)
47+ {
48+ auto & child = curNode->children [c - ' a' ];
49+ if (child == nullptr )
50+ {
51+ return false ;
52+ }
53+
54+ curNode = child.get ();
55+ }
56+
57+ return true ;
58+ }
59+
60+ private:
61+ struct Node
62+ {
63+ bool isWord = false ; // 이 노드가 단어의 끝인지 저장
64+ unique_ptr<Node> children[26 ]; // 자식 노드 저장
65+ };
66+
67+ unique_ptr<Node> mRoot ;
68+ };
Original file line number Diff line number Diff line change 1+ class Solution {
2+ public:
3+ bool wordBreak (string s, vector<string>& wordDict) {
4+ this ->s = s;
5+ return solve (0 , wordDict);
6+ }
7+
8+ // substr 시작 인덱스와 wordDict를 입력으로 받음
9+ bool solve (int start, vector<string>& wordDict)
10+ {
11+ // 이미 계산한 적 있으면 메모이제이션으로 해결
12+ if (memo.contains (start))
13+ {
14+ return memo[start];
15+ }
16+
17+ // 베이스컨디션 -> 끝까지 도달했으면 true
18+ if (start == s.size ())
19+ {
20+ return true ;
21+ }
22+
23+ for (auto & str : wordDict)
24+ {
25+ // wordDict에 있는 단어들로 시작하는지 확인
26+ if (s.substr (start, str.size ()) == str)
27+ {
28+ // 그리고 그 뒷 부분도 재귀를 통해 확인
29+ if (solve (start + str.size (), wordDict))
30+ {
31+ // 뒷 부분까지 통과했으면 true
32+ memo[start] = true ;
33+ return true ;
34+ }
35+ }
36+ }
37+
38+ // 통과 못했으면 false
39+ memo[start] = false ;
40+ return false ;
41+ }
42+
43+ private:
44+ string s;
45+ unordered_map<int , bool > memo; // 메모이제이션
46+ };
You can’t perform that action at this time.
0 commit comments