lxfuhuo
BAN USER 11of 11 votes
AnswersGiven a string which only contains lowercase. You need delete the repeated letters only leave one, and try to make the lexicographical order of new string is smallest.
 lxfuhuo in 
i.e:
bcabc
You need delete 1 'b' and 1 'c', so you delete the first 'b' and first 'c', the new string will be abc which is smallest.
ps: If you try to use greedy algorithm to solve this problem, you must sure that you could pass this case:
cbacdcbc. answer is acdb not adcb
I can't solve this problem well and the interviewer said that you can scan the string twice. First scan is do some preprocess, and the second is to get the answer, but I really can't come up this idea. Report Duplicate  Flag  PURGE
Google Software Developer Algorithm  0of 0 votes
AnswersYou should transform an structure of multiple tree from machine A to machine B. It is a serialization and deserialization problem, but i failed to solve it well.
You could assume the struct is like this:struct Node{ string val; vector<Node*> sons; }
and in machine A, you will given the point to root Node, and in machine B,you should return a pointer to root Node.
 lxfuhuo in China Report Duplicate  Flag  PURGE
Google Software Engineer Algorithm  0of 0 votes
AnswersThe latest reality show has hit the TV: “Cat vs. Dog”. In this show, a bunch of cats and dogs compete for the very prestigious Best Pet Ever title. In each episode, the cats and dogs get to show themselves off, after which the viewers vote on which pets should stay and which should be forced to leave the show.
 lxfuhuo in United States
Each viewer gets to cast a vote on two things: one pet which should be kept on the show, and one pet which should be thrown out. Also, based on the universal fact that everyone is either a cat lover (i.e. a dog hater) or a dog lover (i.e. a cat hater), it has been decided that each vote must name exactly one cat and exactly one dog.
Ingenious as they are, the producers have decided to use an advancement procedure which guarantees that as many viewers as possible will continue watching the show: the pets that get to stay will be chosen so as to maximize the number of viewers who get both their opinions satisfied. Write a program to calculate this maximum number of viewers.
Input
On the first line one positive number: the number of testcases, at most 100. After that per testcase:
One line with three integers c, d, v (1 ≤ c, d ≤ 100 and 0 ≤ v ≤ 500): the number of cats, dogs, and voters.
v lines with two pet identifiers each. The first is the pet that this voter wants to keep, the second is the pet that this voter wants to throw out. A pet identifier starts with one of the characters ‘C’ or ‘D’, indicating whether the pet is a cat or dog, respectively. The remaining part of the identifier is an integer giving the number of the pet (between 1 and c for cats, and between 1 and d for dogs). So for instance, “D42” indicates dog number 42.
Output
Per testcase:
One line with the maximum possible number of satisfied voters for the show.
Sample Input 1
2
1 1 2
C1 D1
D1 C1
1 2 4
C1 D1
C1 D1
C1 D2
D2 C1
Sample Output 1
1
3 Report Duplicate  Flag  PURGE
Google Software Engineer Intern Algorithm  5of 5 votes
AnswersThere are n bombs in a big circle,and each bomb has a value and a 'effect range'.If you detonated a bomb,you will get this bomb's value,but a bomb can have effect on the neighbors which the distance(difference between index) between them is smaller than the 'effect range'.It's say that the neighbor bomb will be destoryed and you could not get their values.
 lxfuhuo in CHINA
You will given each bomb's value and the 'effect range',and you need calculate the max value you can get.
eg. n=3 index 0's bomb' v is 2,range is 0(will not effect others).and v[1]=1,r[1]=1,v[2]=3,r[2]=1;
this case's max value is 5.(detonate the 0's and than the 2's).
HELP ME.
ps: It's a interval DP. Report Duplicate  Flag  PURGE
Google Intern Algorithm  21of 37 votes
AnswersGive you an array which has n integers,it has both positive and negative integers.Now you need sort this array in a special way.After that,the negative integers should in the front,and the positive integers should in the back.Also the relative position should not be changed.
 lxfuhuo in CHINA
eg. 1 1 3 2 2 ans: 1 2 1 3 2.
o(n)time complexity and o(1) space complexity is perfect. Report Duplicate  Flag  PURGE
Google Intern Algorithm
string max_substr(string str){
vector<pair<int,int>> vec;
char maxx = 'a';
for (int i = 0; i < str.size(); ++i){
maxx = max(maxx, str[i]);
}
vector<pair<int,int>> candidates;
for (int i = 0; i < str.size(); ++i){
if (str[i] == maxx){
candidates.push_back(make_pair(i, i));
}
}
while (candidates.size() > 1){
char maxx = 'a';
for (int i = 0; i < candidates.size(); ++i){
if (candidates[i].second + 1 < str.size()){
maxx = max(maxx, str[candidates[i].second + 1]);
}
}
vector<pair<int, int>> newcan;
for (int i = 0; i < candidates.size(); ++i){
if (candidates[i].second + 1 < str.size() && str[candidates[i].second + 1] == maxx){
newcan.push_back(make_pair(candidates[i].first, candidates[i].second + 1));
}
}
candidates = newcan;
}
return str.substr(candidates[0].first);
}

lxfuhuo
January 08, 2018 Suppose all digit mean the times of following string in bracket. "abc2a" is invalid.
string inner_expand(string str, int& site){
string ret = "";
while (site < str.size()){
if (str[site] >= '0' && str[site] <='9'){
int times = 0;
while (str[site] != '['){
times = times * 10 + str[site++]  '0';
}
++site;
string sub_ret = inner_expand(str, site);
for(int i = 0; i < times; ++i){
ret += sub_ret;
}
}
else if (str[site] == ']'){
++site;
return ret;
}
else{
ret += str[site++];
}
}
return ret;
}
string expand(string str){
int site = 0;
return inner_expand(str, site);
}

lxfuhuo
January 08, 2018 int inner_min_swap(vector<int> &vec){
int l = 0;
int sum = 0;
for (int i = 0; i < vec.size(); ++i){
if (vec[i] == 0){
sum += i  l;
++l;
}
}
return sum;
}
int min_swap(vector<int> vec){
int ans = inner_Min_swap(vec);
reverse(vec.begin(), vec.end());
ans = min(inner_min_swap(vec));
return ans;
}

lxfuhuo
January 08, 2018 map<long, int> search(TreeNode *root){
if (root == NULL){
return map<long,int>();
}
if (root>left == NULL && root>right == NULL){
map<long, int> mp;
mp[root>val] = 1;
return mp;
}
map<long, int> l = search(root>left);
map<long, int> r = search(root>right);
map<long, int> ret;
if (l.size() != 0){
for (auto it = l.begin(); it != l.end(); ++it){
ret[it>first + root>val] += it>second;
}
}
if (r.size() != 0){
for (auto it = r.begin(); it != r.end(); ++it){
ret[it>first + root>val] += it>second;
}
}
return ret;
}
int pathSum(TreeNode *root, int target){
map<long, int> mp = search(root);
return mp[target];
}

lxfuhuo
January 07, 2018 Sort every time point including departure time and arrival time.
Then scan all the time point. Firstly, drop off crews for arrival flights. Then let some crews go to departure flights. If no enough crews, then return false result.
struct Flight{
string start;
string destination;
int start_time;
int arrive_time;
Flight(string s, string d, int st, int ar){
start = s;
destination = d;
start_time = st;
arrive_time = ar;
}
};
bool check(vector<Flight> flights, vector<string> crews){
vector<string> arrive[25];
vector<string> departs[25];
map<string, int> crew_in_place;
for (int i = 0; i < crews.size(); ++i){
crew_in_place[crews[i]]++;
}
for (int i = 0; i < flights.size(); ++i){
arrive[flights[i].arrive_time].push_back(flights[i].destination);
departs[flights[i].start_time].push_back(flights[i].start);
}
for (int i = 0; i < 25; ++i){
for (int j = 0; j < arrive[i].size(); ++j){
crew_in_place[arrive[i][j]]++;
}
for (int j = 0; j < departs[i].size(); ++j){
crew_in_place[departs[i][j]];
if (crew_in_place[departs[i][j]] < 0){
return false;
}
}
}
return true;
}

lxfuhuo
January 07, 2018 1. set current first on as the start car of the cluster.
2.find following car whose speed is smaller than start car, so all cars before this one are belong to previous cluster.
3.Repeat step #1 and #2 till the end.
I don't quite understand what followup is talking.
Easy DP could be O(n*m*(n+m)).
Greedy algorithm is iteratively choosing minimum from rest values, then check if it is valid in previous selected path by comparing its coordinate with previous coordinate and next coordinate in path. O(n*m*log(n+m))
struct Node{
int val;
int x;
int y;
};
int cmp(Node &a, Node &b){
return a.val < b.val;
}
vector<int> get_ans(vector<vector<int>> &grid){
int n = grid.size();
int m = grid[0].size();
vector<Node> vec(n * m  2);
int site = 0;
for (int i = 0; i < n; ++i){
for (int j = 0; j < m; ++j){
if ((i == 0 && j == 0)  (i == n1 && j == m1)){
continue;
}
vec[site].val = grid[i][j];
vec[site].x = i;
vec[site].y = j;
++site;
}
}
sort(vec.begin(), vec.end(), cmp);
map<pair<int,int>, int> mp;
mp[make_pair(0,0)] = grid[0][0];
mp[make_pair(n1,m1)] = grid[n1][m1];
for (int i = 0; i < vec.size(); ++i){
pair<int, int> p = make_pair(vec[i].x, vec[i].y);
cout << p.first << ", " << p.second << endl;
auto it = mp.upper_bound(p);
if (it != mp.end() && (vec[i].x > it>first.first  vec[i].y > it>first.second)){
continue;
}
it;
if (it != mp.end() && (vec[i].x < it>first.first  vec[i].y < it>first.second)){
continue;
}
cout << "pass" << endl;
mp[p] = vec[i].val;
}
vector<int> ans;
for (auto it = mp.begin(); it != mp.end(); ++it){
ans.push_back(it>second);
}
sort(ans.begin(), ans.end());
return ans;
}

lxfuhuo
December 31, 2017 0
01
0110
01101001
.......
You could find that prefix is same as previous line, and postfix is the reverse of number of prefix. So jth is the only variable you need to care.
jth is 0 indexed.
int get_ans(int k, int jth){
int str[4] = {0,1,1,0};
if (jth < 4){
return str[jth];
}
bool reverse = 0;
while (jth >= 4){
jth = jth  pow(2,(int)log(jth));
reverse ^= 1;
}
if (reverse){
return str[jth] ^1;
}
return str[jth];
}

lxfuhuo
December 31, 2017 change the solution a little.
#include <iostream>
#include <vector>
#include <queue>
#include <sstream>
using namespace std;
struct tree_node {
string val;
vector<tree_node *> children;
tree_node(string v) : val(v) { }
};
string serialize(tree_node *root) {
if (root == NULL)
return "";
queue<tree_node*> queue;
ostringstream oss;
queue.push(root);
oss << root>val.size();
oss << " " << root>val;
while (!queue.empty()) {
tree_node *curr = queue.front();
queue.pop();
oss << " " << curr>children.size();
for (vector<tree_node*>::const_iterator it = curr>children.begin();
it != curr>children.end();
it++) {
oss << " " << (*it)>val.size() << " " << (*it)>val;
queue.push(*it);
}
}
return oss.str();
}
bool read(istringstream& iss, char buf[], int len){
int totallen = 0;
int restlen = len;
while (1){
int sz = iss.readsome(buf, restlen);
totallen += sz;
if (totallen >= len){
break;
}
if (sz == 0){
return false;
}
}
buf[len] = '\0';
return true;
}
tree_node *deserialize(const string &tree_str) {
if (tree_str == "")
return NULL;
queue<tree_node*> queue;
istringstream iss(tree_str);
int length;
iss >> length;
char buf[1000];
read(iss, buf, length + 1);
tree_node *root = new tree_node(buf + 1);
queue.push(root);
while (!queue.empty()) {
tree_node *curr = queue.front();
queue.pop();
int count;
iss >> count;
for (int i = 0; i < count; i++) {
iss >> length;
read(iss, buf, length +1);
tree_node *new_node = new tree_node(buf + 1);
curr>children.push_back(new_node);
queue.push(new_node);
}
}
return root;
}
void traverse(tree_node* root){
if (root == NULL)
return;
cout << root>val.size() << ":" << root>val << endl;
for (int i = 0; i < root>children.size(); ++i){
traverse(root>children[i]);
}
}
int main(void) {
tree_node* root = new tree_node("ro ot1");
tree_node* r1 = new tree_node("r\n1");
tree_node* r2 = new tree_node("r 2");
tree_node* r3 = new tree_node("r 3");
tree_node* r21 = new tree_node("r21");
root>children.push_back(r1);
root>children.push_back(r2);
root>children.push_back(r3);
r3>children.push_back(r21);
string ret = serialize(root);
//cout << ret.size() << " " << ret << endl;
tree_node* newroot = deserialize(ret);
traverse(newroot);
return 0;
}

lxfuhuo
July 31, 2015 Open Chat in New Window
Let F(N,L,K) = N*(N1)...(NK) * [(NK)*(NK)*....(NL)].(length is L)
 lxfuhuo January 08, 2018The answer should be :
F(N,L,K)  C(N,1)F(N1,L,K) + C(N,2)F(N2,L,K)F(N,3)F(N3,L,K) .....where C(n,i) means combination number of selecting i items from n items.