jackdanielsparrow
BAN USERint nthsmallest(vector<int> &v, int k){
int sz=v.size();
priority_queue<int,vector<int>,less<int>> pq;
int multiplier = 1;
while(pq.size()<k){
for(int i=0; i<sz && pq.size()<k; ++i){
pq.push(v[i]*multiplier);
}
multiplier++;
}
return pq.top();
}
#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
#include <set>
#include <math.h>
using namespace std;
bool testIntersect(string x,string y){
cout<<"Testing: "<<x<<" "<<y;
vector<char> res;
set<char> a(x.begin(),x.end());
set<char> b(y.begin(),y.end());
set_intersection(a.begin(),a.end(),b.begin(),b.end(),back_inserter(res));
cout<<" InterSect Length:"<<res.size()<<endl;
return res.size()>0;
}
int getMax(vector<string> words){
if(words.size()<=1)
return 0;
int maxLen=0;
for(int i=0;i<words.size()-1;++i){
for(int j=i+1;j<words.size();++j){
if (!testIntersect(words[i],words[j])){
maxLen = max(maxLen,(int)words[i].length()*(int)words[j].length());
cout<<"max:"<<maxLen<<endl;
}
}
}
return maxLen;
}
int main(){
vector<string> words{"ABCW","BAZ","FOO","DOE","BAR","XTFN","ABCDEF"};
cout<<getMax(words)<<endl;
}
C++ solution with single hash table
#include <iostream>
#include <string>
#include <sstream>
#include <unordered_map>
#include <vector>
#include <algorithm>
using namespace std;
int main()
{
vector<string> v{"11:01AM $10.01 100","11:03AM $11.01 200","11:04AM $12.81 150","11:06AM $10.01 210"};
v.push_back("11:07AM $10.01 180");
v.push_back("11:08AM $12.81 400");
v.push_back("11:09AM $11.01 200");
unordered_map<string,pair<int,double>> hash;
for(int i=0;i<(int)v.size();++i){
//input parsing
stringstream ss;
ss<<v[i];
string times;
double price;
int quantity;
ss>>times;
string dv_s;
ss>>dv_s;
price=stod(dv_s.substr(1));
ss>>quantity;
//actual logic: store total quantity and price in a map hashed by dollar value of the item
hash[dv_s].first+=quantity;
hash[dv_s].second=price*hash[dv_s].first;
double max_p=0;
int res_quantity;
string res_dv;
//search the map for item with maximum total price so far and print dollar value and quantity
for(auto it=hash.begin();it!=hash.end();++it){
if(it->second.second>max_p){
res_dv=it->first;
res_quantity=it->second.first;
max_p=it->second.second;
}
}
cout<<res_dv<<"("<<res_quantity<<")"<<endl;
//clear stream
ss.clear();
}
}
For any n>1.
bool isPowerOf_N(int num,int n){
if(num==1)
return true;
if(num<=0||num%n!=0)
return false;
return isPowerOf_N(num/n,n);
}
Repaliciaable183, Analyst at 247quickbookshelp
I am an agent contract clerk who is responsible for handling the recruitment process. I advertise the vacancies for agents ...
- jackdanielsparrow March 16, 2016