-
-
Notifications
You must be signed in to change notification settings - Fork 297
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
24 changed files
with
1,122 additions
and
17 deletions.
There are no files selected for viewing
This file contains 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,5 +1,18 @@ | ||
__________________________________________________________________________________________________ | ||
|
||
class Solution { | ||
public: | ||
int calculateTime(string keyboard, string word) { | ||
vector<int> pos(26); | ||
for(int i=0;i<26;i++) pos[keyboard[i]-'a']=i; | ||
int cur=0,res=0; | ||
for(char c:word){ | ||
int dif=abs(cur-pos[c-'a']); | ||
res+=dif; | ||
cur=pos[c-'a']; | ||
} | ||
return res; | ||
} | ||
}; | ||
__________________________________________________________________________________________________ | ||
|
||
__________________________________________________________________________________________________ |
This file contains 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
This file contains 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,5 +1,18 @@ | ||
__________________________________________________________________________________________________ | ||
|
||
class Solution { | ||
public: | ||
int connectSticks(vector<int>& sticks) { | ||
int res=0; | ||
priority_queue<int, vector<int>, greater<int> > pq(sticks.begin(),sticks.end()); | ||
while(pq.size()>1u){ | ||
int x=pq.top();pq.pop(); | ||
int y=pq.top();pq.pop(); | ||
res+=x+y; | ||
pq.emplace(x+y); | ||
} | ||
return res; | ||
} | ||
}; | ||
__________________________________________________________________________________________________ | ||
|
||
__________________________________________________________________________________________________ |
This file contains 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
This file contains 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,5 +1,90 @@ | ||
__________________________________________________________________________________________________ | ||
sample 24 ms submission | ||
class Solution { | ||
public: | ||
|
||
vector<string> invalidTransactions(vector<string>& A) { | ||
int n = A.size(); | ||
map < string, vector < pair < int, pair < string, int > > > > M; | ||
for(int i = 0;i < n;++i) { | ||
string x; | ||
vector < string > info; | ||
for(int j = 0;j <= A[i].size();++j) { | ||
if(A[i][j] == ',' or j == A[i].size()) { | ||
info.push_back(x); | ||
x.clear(); | ||
} else { | ||
x += A[i][j]; | ||
} | ||
} | ||
int t = stoi(info[1]); | ||
int amount = stoi(info[2]); | ||
|
||
M[info[0]].push_back({t, {info[3], amount}}); | ||
} | ||
|
||
vector < string > ans; | ||
for(auto it = M.begin();it != M.end();++it) { | ||
string name = it->first; | ||
auto &adj = it -> second; | ||
// sort(adj.begin(), adj.end()); | ||
for(int j = 0;j < adj.size();++j) { | ||
if(adj[j].second.second > 1000) { | ||
ans.push_back(name + "," + to_string(adj[j].first) + "," + to_string(adj[j].second.second) + "," + adj[j].second.first); | ||
continue; | ||
} | ||
for(int k = 0;k < adj.size();++k) { | ||
if(abs(adj[j].first - adj[k].first) < 61 and adj[j].second.first != adj[k].second.first) { | ||
ans.push_back(name + "," + to_string(adj[j].first) + "," + to_string(adj[j].second.second) + "," + adj[j].second.first); | ||
break; | ||
} | ||
} | ||
} | ||
} | ||
|
||
return ans; | ||
} | ||
}; | ||
__________________________________________________________________________________________________ | ||
sample 32 ms submission | ||
struct Transaction { | ||
string name, city; | ||
int time, amount; | ||
string raw; | ||
Transaction() {} | ||
Transaction(string&_raw) { | ||
raw = _raw; | ||
istringstream iss(raw); | ||
string buf; | ||
getline(iss, name, ','); | ||
getline(iss, buf, ','); | ||
time = stoi(buf); | ||
getline(iss, buf, ','); | ||
amount = stoi(buf); | ||
getline(iss, city, ','); | ||
//cout << name << time << amount << city; | ||
} | ||
} ; | ||
|
||
class Solution { | ||
public: | ||
vector<string> invalidTransactions(vector<string>& transactions) { | ||
vector<Transaction> tr; | ||
vector<string> invalid_tr; | ||
for(auto transaction: transactions) { | ||
tr.push_back(Transaction(transaction)); | ||
} | ||
for(int i=0; i<tr.size(); ++i) { | ||
bool invalid = false; | ||
if(tr[i].amount >= 1000) invalid = true; | ||
for(int j=0; !invalid && j<tr.size(); ++j) { | ||
if(i != j && tr[i].name == tr[j].name && tr[i].city != tr[j].city && abs(tr[j].time - tr[i].time) <= 60) { | ||
invalid = true; | ||
} | ||
} | ||
if(invalid) invalid_tr.push_back(tr[i].raw); | ||
} | ||
return invalid_tr; | ||
} | ||
}; | ||
__________________________________________________________________________________________________ |
This file contains 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,5 +1,96 @@ | ||
__________________________________________________________________________________________________ | ||
|
||
sample 20 ms submission | ||
class Solution { | ||
public: | ||
|
||
vector < int > getFreq(vector < string > &A) { | ||
int n = A.size(); | ||
|
||
vector < int > freq; | ||
for(int i = 0;i < n;++i) { | ||
char mi = 'z' + 3; | ||
for(int j = 0;j < A[i].size();++j) { | ||
mi = min(mi, A[i][j]); | ||
} | ||
|
||
int f = 0; | ||
for(int j = 0;j < A[i].size();++j) { | ||
if(A[i][j] == mi) { | ||
++f; | ||
} | ||
} | ||
freq.push_back(f); | ||
} | ||
return freq; | ||
} | ||
|
||
vector<int> numSmallerByLeadingCount(vector<string>& Q, vector<string>& A) { | ||
vector < int > freqA = getFreq(A); | ||
vector < int > freqQ = getFreq(Q); | ||
|
||
vector < int > ans; | ||
sort(freqA.begin(), freqA.end()); | ||
|
||
for(int i = 0;i < freqQ.size();++i) { | ||
int x = freqQ[i]; | ||
int lo = 0, hi = freqA.size(); | ||
while(lo + 1 < hi) { | ||
int mid = lo + (hi - lo)/2; | ||
if(freqA[mid] > x) { | ||
hi = mid; | ||
} else { | ||
lo = mid; | ||
} | ||
} | ||
while(hi > 0 and freqA[hi - 1] > x) { | ||
--hi; | ||
} | ||
ans.push_back(A.size() - hi); | ||
|
||
} | ||
return ans; | ||
} | ||
}; | ||
__________________________________________________________________________________________________ | ||
|
||
sample 24 ms submission | ||
class Solution { | ||
public: | ||
vector<int> numSmallerByLeadingCount(vector<string>& queries, vector<string>& words) { | ||
|
||
vector<int>b(11, 0); | ||
|
||
for(int i=0;i<words.size();i++){ | ||
string s=words[i]; | ||
vector<int>f(26,0); | ||
for(int j=0;j<s.size();j++) | ||
f[s[j]-'a']++; | ||
for(int j=0;j<26;j++) | ||
if(f[j]>0){ | ||
b[f[j]]++; | ||
break; | ||
} | ||
} | ||
|
||
vector<int>ans(queries.size(),0); | ||
|
||
for(int i=0;i<queries.size();i++){ | ||
string s=queries[i]; | ||
vector<int>f(26,0); | ||
for(int j=0;j<s.size();j++) | ||
f[s[j]-'a']++; | ||
for(int j=0;j<26;j++) | ||
if(f[j]>0){ | ||
int val =f[j]; | ||
int sum=0; | ||
for(int k=val+1;k<11;k++) | ||
sum+=b[k]; | ||
ans[i]=sum; | ||
break; | ||
} | ||
|
||
} | ||
|
||
return ans; | ||
} | ||
}; | ||
__________________________________________________________________________________________________ |
Oops, something went wrong.