oelsayed
BAN USERfunction tree_equals($root1, $root2){
if(($root1 && !$root2) || ($root2 && !$root1)){
return FALSE;
}
if($root1->data != $root2->data){
return FALSE;
}
if((!$root1 && !$root2){
return TRUE;
}
return tree_equals($root1->left, $root2->left) && tree_equals($root1->right, $root2->right);
}
function binary_wildcard($s){
$count = 0;
for($i=0; $i<strlen($s); $i++){
if($s[$i] == "?"){
$count++;
}
}
$combs = pow(2, $count);
for($i=0; $i<$combs; $i++){
$b = sprintf("%0" . $count . 'b', $i);
$j=0;
$k=0;
$tmp = $s;
for($j=0; $j<strlen($tmp); $j++){
if($tmp[$j] == "?"){
$tmp[$j] = $b[$k];
$k++;
}
}
echo $tmp . '<br/>';
}
}
function abbreviations($s){
$middle = substr($s, 1, strlen($s) - 2);
for($i=1; $i<strlen($s)-2; $i++){
echo $s[0] . substr($middle, 0, $i) . (strlen($s)-2 - $i) . $s[strlen($s)-1] . '<br/>';
echo $s[0] . (strlen($s)-2 - $i) . substr($middle, strlen($s)-2 - $i, $i) . $s[strlen($s)-1] . '<br/>';
}
echo $s[0] . ( strlen($s) - 2) . $s[strlen($s)-1];
}
function longest_common_prefix($strings){
$str1_words = str_word_count($strings[0], 1);
for($i=0; $i<count($str1_words); $i++){
for($j=1; $j<count($strings); $j++){
$str_words = str_word_count($strings[$j], 1);
if(count($str_words) === $i || $str_words[$i] !== $str1_words[$i]){
return implode(" ", array_slice($str1_words, 0, $i));
}
}
}
return FALSE;
}
$strings = array("i love all dogs", "i love cats");
echo longest_common_prefix($strings);
function increasing_sum($list){
$longest_path = 0;
$longest_path_sum = 0;
$curr_path = 1;
$curr_path_sum = $list[0];
$breakdown_idx = 0;
for($i=1; $i<count($list); $i++){
if(($list[$i] < $list[$breakdown_idx] && $i < $breakdown_idx) || ($list[$i] > $list[$i-1] && $i > $breakdown_idx) || $i == $breakdown_idx){
$curr_path++;
$curr_path_sum += $list[$i];
}else if($i > $breakdown_idx){
if($curr_path > $longest_path){
$longest_path = $curr_path;
$longest_path_sum = $curr_path_sum;
}
$breakdown_idx = $i;
if($list[0] < $list[$breakdown_idx]){
$curr_path = 1;
$curr_path_sum = $list[0];
}else{
$curr_path = 0;
$curr_path_sum = 0;
}
$i=1;
}
}
if($curr_path > $longest_path){
$longest_path = $curr_path;
$longest_path_sum = $curr_path_sum;
}
return $longest_path;
}
function path_sum($node, $path, $level, $target){
if($node === NULL){
return;
}
$path[$level] = $node->data;
$sum = 0;
for($i=$level; $i>=0; $i--){
$sum += $path[$i];
if($sum === $target){
for($j=$i; $j<=$level; $j++){
echo $path[$j] . ' - ';
}
echo '<br/>';
}
}
/*$sum += $node->data;
if($sum === $target){
print_r('<pre>');
print_r($path);
}*/
path_sum($node->left, $path, $level + 1, $target);
path_sum($node->right, $path, $level + 1, $target);
}
1- Loop on employees vector in all the employees classes and build a hash table, the key will be the employee from the vector and the value will be the ID of the employee class
- oelsayed October 10, 20152- Take one of the two employees that we want to get the common manager and create another hash by tracing up the managers using hash built in step 1.
3- Do the same for the second employee but instead of building another hash, just trace up the second employee parents and check one by one from the hash built in step 2 until you find the first common manager