forked from wisdompeak/LeetCode
-
Notifications
You must be signed in to change notification settings - Fork 1
[pull] master from wisdompeak:master #13
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 1 commit
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
21c5983
Update 642.Design-Search-Autocomplete-System.cpp
wisdompeak 8f5c9fb
Update Readme.md
wisdompeak 57e78dd
Create 2311.Longest-Binary-Subsequence-Less-Than-or-Equal-to-K_v1.cpp
wisdompeak 64d401c
Create 2311.Longest-Binary-Subsequence-Less-Than-or-Equal-to-K_v2.cpp
wisdompeak 16bd37e
Update 2311.Longest-Binary-Subsequence-Less-Than-or-Equal-to-K_v1.cpp
wisdompeak 99fca9d
Create Readme.md
wisdompeak 655136c
Update Readme.md
wisdompeak 36cd4a4
Create 2312.Selling-Pieces-of-Wood.cpp
wisdompeak 99bb6dd
Update Readme.md
wisdompeak 0cb6957
Create Readme.md
wisdompeak 54e342b
Update Readme.md
wisdompeak 94b6826
Create 2222.Number-of-Ways-to-Select-Buildings.cpp
wisdompeak fbf5e9a
Update Readme.md
wisdompeak 99304f3
Create Readme.md
wisdompeak 7d629b3
Update 2222.Number-of-Ways-to-Select-Buildings.cpp
wisdompeak f3ef226
Update Readme.md
wisdompeak File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next
Next commit
Update 642.Design-Search-Autocomplete-System.cpp
- Loading branch information
commit 21c5983222ffa2f7d922f23a6311a23c3039c6c2
There are no files selected for viewing
110 changes: 64 additions & 46 deletions
Design/642.Design-Search-Autocomplete-System/642.Design-Search-Autocomplete-System.cpp
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,70 +1,88 @@ | ||
class AutocompleteSystem { | ||
unordered_map<string,int>Map; | ||
string data; | ||
|
||
struct cmp | ||
class TrieNode | ||
{ | ||
public: | ||
TrieNode* next[128]; | ||
set<pair<int,string>>top; | ||
TrieNode() | ||
{ | ||
bool operator()(pair<string,int>a, pair<string,int>b) | ||
{ | ||
if (a.second==b.second) | ||
return a.first<b.first; | ||
else | ||
return a.second>b.second; | ||
} | ||
}; | ||
for (int i=0; i<128; i++) | ||
next[i] = NULL; | ||
} | ||
}; | ||
|
||
class AutocompleteSystem { | ||
TrieNode* root; | ||
string inputStr; | ||
TrieNode* cur; | ||
int flag = 1; | ||
public: | ||
AutocompleteSystem(vector<string> sentences, vector<int> times) | ||
AutocompleteSystem(vector<string>& sentences, vector<int>& times) | ||
{ | ||
root = new TrieNode(); | ||
cur = root; | ||
for (int i=0; i<sentences.size(); i++) | ||
Map[sentences[i]]=times[i]; | ||
data.clear(); | ||
add(root, sentences[i], 0, -times[i]); | ||
} | ||
|
||
void add(TrieNode* node, const string sentence, int i, int freq) | ||
{ | ||
if (i==sentence.size()) return; | ||
|
||
int k = sentence[i]; | ||
if (node->next[k] == NULL) | ||
node->next[k] = new TrieNode(); | ||
node = node->next[k]; | ||
|
||
int f = 0; | ||
for (auto iter = node->top.begin(); iter!=node->top.end(); iter=next(iter)) | ||
{ | ||
if (iter->second == sentence) | ||
f = iter->first; | ||
} | ||
if (f!=0) node->top.erase({f, sentence}); | ||
node->top.insert(make_pair(f+freq, sentence)); | ||
|
||
add(node, sentence, i+1, freq); | ||
} | ||
|
||
vector<string> input(char c) | ||
{ | ||
inputStr.push_back(c); | ||
|
||
if (c=='#') | ||
{ | ||
Map[data]++; | ||
data.clear(); | ||
inputStr.pop_back(); | ||
add(root, inputStr, 0, -1); | ||
inputStr = ""; | ||
cur = root; | ||
flag = 1; | ||
return {}; | ||
} | ||
|
||
data.push_back(c); | ||
priority_queue<pair<string,int>,vector<pair<string,int>>,cmp>pq; | ||
|
||
for (auto x:Map) | ||
else if (flag==0) | ||
{ | ||
string a=x.first; | ||
if (match(data,a)) | ||
{ | ||
pq.push({a,Map[a]}); | ||
if (pq.size()>3) pq.pop(); | ||
} | ||
return {}; | ||
} | ||
|
||
vector<string>results; | ||
while (!pq.empty()) | ||
else if (cur->next[c]==NULL) | ||
{ | ||
results.push_back(pq.top().first); | ||
pq.pop(); | ||
flag = 0; | ||
return {}; | ||
} | ||
reverse(results.begin(),results.end()); | ||
return results; | ||
} | ||
|
||
bool match(string a, string b) | ||
{ | ||
for (int i=0; i<a.size(); i++) | ||
|
||
cur = cur->next[c]; | ||
vector<string>rets; | ||
for (auto iter = cur->top.begin(); iter!=cur->top.end(); iter=next(iter)) | ||
{ | ||
if (i>=b.size() || a[i]!=b[i]) | ||
return false; | ||
rets.push_back(iter->second); | ||
if (rets.size()==3) break; | ||
} | ||
return true; | ||
return rets; | ||
|
||
} | ||
|
||
}; | ||
|
||
/** | ||
* Your AutocompleteSystem object will be instantiated and called as such: | ||
* AutocompleteSystem obj = new AutocompleteSystem(sentences, times); | ||
* vector<string> param_1 = obj.input(c); | ||
* AutocompleteSystem* obj = new AutocompleteSystem(sentences, times); | ||
* vector<string> param_1 = obj->input(c); | ||
*/ |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.