puncaz
BAN USERGeek
c# Implementation
bool Match(string string, string pattern)
{
if((pattern==null) && (string==null) )
return true;
if(pattern == "*" || pattern==string)
return true;
if(pattern.ElementAt(0)=="." || pattern.ElementAt(0)==string.ElementAt(0))
return Match(string.substring(1), pattern.substring(1));
if(pattern.ElementAt(0)=="*")
{
return Match(string, pattern.substring(1)) || Match(string.substring(1), pattern) ;
}
}
This logic does not care about the original order. But it finds out the total repoting (not direct) for each employee.
It uses two iterations #1 find direct reports #2 performs the summation
Dictionary<string, int> fnGetReporting(Dictionary<string, string> list)
{
Dictionary<string, int> result=new Dictionary<string, int>();
foreach(var i in list)
{
if(result.contains(i.value))
result.update(i.value, result.get(i.value)+(i.value==i.key?0:1))
else
result.add(i.value,(i.value==i.key?0:1))
}
foreach(var i in list)
{
if(result.contains(i.key) && i.key!=i.value)
{
result.update(i.value, result.get(i.value)+result.get(i.key))
}
else
{
result.add(i.key,0);
}
}
return result;
}
fnDefrag(string file)
{
using (var stream = File.Open(file, FileMode.Open))
{
byte[] barry=new byte[1];
int current=0;
int pivot=0;
while(current<(stream.length-1))
{
stream.position=current;
if((current+1)%5==0)
{
current++;
}
if(current!=pivot)
{
stream.read(barry,current,1);
stream.write(barry,pivot,1);
}
pivot++;
current++;
}
stream.SetLength=pivot;
}
}
Double linked list is the easy to implement solution for this problem.
Although balanced BST can also be used to solve this problem but at Left & Right height of the tree will grow by n/2 where n is the total number of nodes in the tree.
So BST to push and pop will not give best of BST, ultimately it will look like a double linked list
let's say
node
{
next
last
payload
count
}
initial ,
next =nil
last
=nil
payload
=nil
count=0
Using Double LL
Insert the node.
Calculate the mid point (n/2+1).
Place the start pointer at the mid point.
count++
PUSH
To push new nodes traverse backward from the start pointer till end
insert new node.
recalculate the mid-point.
Update start pointer & count++.
POP
Traverse backward from start pointer till you find the last node.
pop the node
Recalulate the mid point ,
Set start pointer,
count--
GetMiddle
return start->payload :)
As mentioned, 0-value is known but information regarding association of 0-value with pots is lost. In that scenarios, how can you rearrange pots (sort) where you do not know the 0-value of pot.
- puncaz May 01, 2015