Saturday, May 28, 2016

#coding exercise
Write box stacking algorithm
int GetStackedBoxesCount(List<Tuple<int,int,int>> boxes)
{
int n = boxes.Length;
int best = new List<int>(n);
for (int i =1; i < n; i++)
for (int j = 0; j < i j++)
{
    if (box[i].first * box[i].second < box[j].first * box[j].second && best[j] + 1 > best[i])
          best[i] = best[j] + 1;
}
return best.max();
}

Minimum number of steps between start and end positions for a knight on a chessboard
void GetMinMoves(Point start, Point end, int count, ref int min)
{
 if ( start == end) { if (count < min), {min = count;} }
 var x = new List<int>() { 2, 1, -1, -2, -2, -1,  1,  2};
 var y = new List<int>() { 1, 2,  2,  1, -1, -2, -2, -1};
for (int k =0 ; k < 8; k ++)
{
   start.x += x[k];
   start.y += y[k];
   if (isValid(start))
       GetMinMoves(start, end, count + 1, ref min);
   start.x -= x[k];
   start.y -= y[k];
}
}

int GetWordSnake( char[,] board, Point start, int count, string candidate, Dictionary<string, string> d)
{
  for (int i =0; i< 4; i++)
  {
        start = update(start, i);
        candidate.Add(board[start.x, start.y]);
        id (d.contains(candidate)) print(candidate);
        if (d.containsprefix(candidate) && count < board.rows * board.cols)
               GetWordSnake(board, start, count+1, candidate, d);
        start = revert(start, i)
  }
}

Perform Topological Sort
Topological DFS(int[,] G, List<int> color, List<int> parent, ref List<int> result)
for (int i =0; i < G.rows; i++)
{
    color[i] = white;
    parent[i] = -1;
}
time = 0;
for (int j =0 ; j < G.rows; j++)
{
      if (color[j] == white)
         DFS-Visit(G, j, ref color, ref parent, ref time, def result)
}
}

DFS-Visit(int[,] G, int u, ref List<int> color, ref List<int> parent, ref int time,ref List<int> result)
{
time = time + 1;
//u.start = time;
color[i] = gray;
for (int i = 0; i < G.cols; i++)
   var v =  G[u, i];
       if v > 0 &&color[v] == white
           parent[v] = u;
           DFS-Visit(G, v, color, parent, time, result)
color[u] = black;
time = time + 1;
result.InsertAt(0, u);
//u.f = time
}


No comments:

Post a Comment