Wednesday, May 18, 2016

Given arrival and departure times, find the maximum overlap 
Int maxOverlap(List<Tuple<int,int>> timings, int n) 
{ 
Timings.sortByArrivals(); 
Var overlap = List<Tuple<int,int>> (); 
Int max = 0; 
Int current = 0; 
for (int I = 0; I < n; i++) 
{ 
   Current = overlaps[i].first; 
   Overlap.add(timings[i]); 
   For (int I =0; I < overlap.Count; i++) 
   { 
         if (overlaps[i].second < current) 
            overlaps.RemoveAt[i];  
   } 
   If (overlaps.count> max) 
        Max = overlaps.count; 
} 
Return max; 
} 
#Find max area under the histogram 
Int MaxAreaOfARectangle(List<int>histogram) 
{ 
Int max = 0; 
For(int I = 0; I < histogram.count; i++){ 
Int area = GetArea(histogram,i); 
If (area > max) 
    max = area; 
} 
Return max; 
} 
Int GetArea(List<int>histogram, int center) 
{ 
Int area = histogram[center]*1; 
For (int I = center-1; 
         i>=0 && histogram[i] > histogram[center]; i++){ 
         area += histogram[center]*I; 
} 
For (int I = center+1; 
         I<histogram[count] && histogram[i] > histogram[center]; i++){ 
         area += histogram[center]*I; 
} 
return area; 
} 


#codingexercise
Given a positive integer N, count all possible distinct binary strings of length N such that there are no consecutive 1’s
void GetString(int N, ref stringbuilder candidate)
{
if (N==0) { print candidate; return;}
if (candidate.Count>0 && candidate[candidate.Count-1] == "1") {
candidate.add("0");
GetString(N-1, ref candidate);
candidate.RemoveLast();
return;
}
candidate.Add("0")
GetString(N-1, ref candidate);
candidate.RemoveLast();
candidate.Add("1")
GetString(N-1, ref candidate);
candidate.RemoveLast();
}

int GetNonRepeatedElement(List<int> nums)
{
assert(nums.Count > 0);
int x  = nums[0];
for (int i = 1; i  < nums.Count; i++)
      x^= nums[i];
return x;
}

for finding two elements
List<int> GetTwoElements(List<int> nums)
{
assert(nums.Count > 0);
int x = nums[0];
for (int i =1; i < nums.Count; i++)
{
x^= nums[i];
}
int div = x &^(x-1);
int a;
int b;
for (int i = 0; i < nums.Count; i++)
{
if (nums[i] ^ div)
    a ^= nums[i];
else
    b ^= nums[i];
}
var items = new List<int>;
items.add(a);
items.add(b);
return items;
}

No comments:

Post a Comment