Skip to content

Commit

Permalink
contest151
Browse files Browse the repository at this point in the history
contest151
  • Loading branch information
strengthen committed Aug 25, 2019
1 parent fee4436 commit 0a2e570
Show file tree
Hide file tree
Showing 24 changed files with 1,122 additions and 17 deletions.
15 changes: 14 additions & 1 deletion C++/1165.cpp
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;
}
};
__________________________________________________________________________________________________

__________________________________________________________________________________________________
43 changes: 43 additions & 0 deletions C++/1166.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,47 @@
__________________________________________________________________________________________________
class FileSystem {
vector<string> split(string& s,char c){
int n=s.size();
vector<string> res;
for(int i=0;i<n;i++){
if(s[i]==c) continue;
string t;
while(i<n&&s[i]!=c) t.push_back(s[i++]);
res.push_back(t);
}
return res;
}
public:
struct Node{
map<string, Node*> nxt;
int value;
Node(){}
Node(int value):value(value){}
};
Node* root;
FileSystem() {
root=new Node();
}

bool create(string path, int value) {
auto vs=split(path,'/');
Node* pos=root;
for(int i=0;i+1<(int)vs.size();i++)
if(pos) pos=pos->nxt[vs[i]];
if(!pos) return false;
pos->nxt[vs.back()]=new Node(value);
return true;
}

int get(string path) {
auto vs=split(path,'/');
Node* pos=root;
for(int i=0;i<(int)vs.size();i++)
if(pos) pos=pos->nxt[vs[i]];
if(!pos) return -1;
return pos->value;
}
};

__________________________________________________________________________________________________

Expand Down
15 changes: 14 additions & 1 deletion C++/1167.cpp
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;
}
};
__________________________________________________________________________________________________

__________________________________________________________________________________________________
71 changes: 71 additions & 0 deletions C++/1168.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,75 @@
__________________________________________________________________________________________________
class Solution {
template<typename T>
struct Kruskal{

struct edge{
int from,to;
T cost;
int used;
edge(int from,int to,T cost):
from(from),to(to),cost(cost),used(0){}
bool operator<(const edge& e) const{
return cost<e.cost;
}
};

int n;
vector<int> r,p;
vector<edge> es;

Kruskal(){}
Kruskal(int n):n(n),r(n,1),p(n){
iota(p.begin(),p.end(),0);
}

int find(int x){
return (x==p[x]?x:p[x]=find(p[x]));
}

bool same(int x,int y){
return find(x)==find(y);
}

void unite(int x,int y){
x=find(x);y=find(y);
if(x==y) return;
if(r[x]<r[y]) swap(x,y);
r[x]+=r[y];
p[y]=x;
}

void add_edge(int u,int v,T c){
es.emplace_back(u,v,c);
}

T build(){
sort(es.begin(),es.end());
T res=0;
for(auto &e:es){
if(!same(e.from,e.to)){
res+=e.cost;
unite(e.from,e.to);
e.used=1;
}
}
return res;
}
};
public:
int minCostToSupplyWater(int n, vector<int>& wells, vector<vector<int>>& pipes) {
Kruskal<int> G(n+1);
for(int i=0;i<n;i++){
G.add_edge(n,i,wells[i]);
}
for(auto es:pipes){
int u=es[0],v=es[1],w=es[2];
u--;v--;
G.add_edge(u,v,w);
}
return G.build();
}
};

__________________________________________________________________________________________________

Expand Down
85 changes: 85 additions & 0 deletions C++/1169.cpp
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;
}
};
__________________________________________________________________________________________________
95 changes: 93 additions & 2 deletions C++/1170.cpp
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;
}
};
__________________________________________________________________________________________________
Loading

0 comments on commit 0a2e570

Please sign in to comment.