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 );
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+ };
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 1+ class Solution {
2+ private:
3+ struct chnum
4+ {
5+ int cnt[26 ];
6+
7+ chnum ()
8+ {
9+ memset (cnt, 0 , sizeof (int ) * 26 );
10+ }
11+
12+ bool operator ==(const chnum& other) const
13+ {
14+ for (int i = 0 ; i < 26 ; ++i)
15+ {
16+ if (this ->cnt [i] != other.cnt [i]) return false ;
17+ }
18+ return true ;
19+ }
20+ };
21+
22+ struct chnumHash
23+ {
24+ size_t operator ()(const chnum& key) const
25+ {
26+ size_t seed = 5381 ;
27+ for (int i = 0 ; i < 26 ; ++i)
28+ {
29+ seed = ((seed << 5 ) + seed) + key.cnt [i];
30+ }
31+ return seed;
32+ }
33+ };
34+
35+ public:
36+ vector<vector<string>> groupAnagrams (vector<string>& strs) {
37+ unordered_map<chnum, vector<string>, chnumHash> ns;
38+ for (auto & str : strs)
39+ {
40+ chnum n;
41+ for (auto ch : str)
42+ {
43+ n.cnt [ch - ' a' ]++;
44+ }
45+
46+ ns[n].push_back (str);
47+ }
48+
49+ vector<vector<string>> v;
50+ v.reserve (ns.size ());
51+
52+ for (auto & p : ns)
53+ {
54+ v.push_back (move (p.second ));
55+ }
56+
57+ return v;
58+ }
59+ };
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+ bool solve (int start, vector<string>& wordDict)
9+ {
10+ if (memo.contains (start))
11+ {
12+ return memo[start];
13+ }
14+
15+ if (start == s.size ())
16+ {
17+ return true ;
18+ }
19+
20+ for (auto & str : wordDict)
21+ {
22+ if (s.substr (start, str.size ()) == str)
23+ {
24+ if (solve (start + str.size (), wordDict))
25+ {
26+ memo[start] = true ;
27+ return true ;
28+ }
29+ }
30+ }
31+
32+ memo[start] = false ;
33+ return false ;
34+ }
35+
36+ private:
37+ string s;
38+ unordered_map<int , bool > memo;
39+ };
You can’t perform that action at this time.
0 commit comments