aks
BAN USER{
Take for temp pointers corresponding to r,g,y,b. Set them to the first occurrence of node of that type. one more pointer to track cur_pos.
Pseudo-code:
1) Start condition: cur_pos = head
2) Traverse list to set pointers r,g,y,b to first occurance of node of those types.
3) move 4 nodes corresponding to r,g,y,b to cur_pos.
r->g->y->b->cur_pos->.....
4) repeat 2,3 from starting from cur_pos.
}
{
void paths(struct Node *root)
{
struct Node *current = root;
struct Stack *s = NULL;
bool done = 0;
while (!done)
{
if(current != NULL)
{
push(&s, current);
current = current->left;
}
else
{
if (!isEmpty(s))
{
current = pop(&s);
printStack(s); >>>> Prints current stack.
current = current->right;
}
else
done = 1;
}
}
}
}
{
Sequence where Difference between largest and smallest element is 1 is same as Sequence where all middle elements are either equal to smallest or equal to largest.
-
Solution:
cur_low, cur_high, cur_count init to -1
Use hash to store -> index of element in arr[], count.
While inserting x, look for y such that y - x = 1 && y.index > x.index && x.count + y.count > cur_count
-> cur_count = x.count.y.count
-> cur_low = x.index
-> cur_high = y.index
}
void reverse(char sub_str[], int begin, int end) {
char temp;
while (begin < end) {
temp = sub_str[begin];
sub_str[begin] = sub_str[end];
sub_str[end] = temp;
begin++;
end--;
}
}
void reverseWords(char str[], int N) {
int begin = 0, end = 0;
while(end < N) {
while (end != " " && end != '\0') {
end++;
}
reverse(str, begin, end); // This simply reverses a string
end++;
begin = end;
}
}
---
O(N) + O(N/2)
I think instead of having 2 priority queues, you can just use combination of direction and vector<floor> where floor could be integer/boolean type.
- aks October 14, 2014if direction is up, you serve floors in increasing order otherwise decreasing.
Someone requests elevator on floor X, set value at index X in vector to ON
Also, when elevator stops at some floor and people getting in to the elevator request for target floor. People (already)inside an elevator can request floor at any time.
(Also with each floor, we can link # of ppl requested for particular floor at any given time)
As you serve floors, reset it in vector to OFF. If people inside an elevator are linked to a floor, then clear that list now.
I look at it as a producer/consumer problem. So as others mentioned, synchronization is required.