Tuesday, June 12, 2012

How to clone a graph

A nice article on how to clone a graph: http://www.leetcode.com/2012/05/clone-graph-part-i.html

Sunday, June 3, 2012

Reverse a Linked List Recursively and Iterately


struct ListNode {
    ListNode* next;
    int       data;
};

 
ListNode* reverseListResursive(ListNode* head) {

 //Initial
 if(head == NULL) return head;
 if((head->next)== NULL) return head;

 ListNode *newHead = reverseListResursive(head->next);
 head->next->next = head;
 head -> next = NULL;

 return newHead;
}

 
ListNode* reverseListNonResursive(ListNode* head) {

 //Initial
 if(head == NULL) return head;
 //if((head->next)== NULL) return head;

 ListNode *newHead = head;
 ListNode *tempNext = NULL;

 while(head) {
  newHead = head;
  head = head->next;
  newHead->next = tempNext;
  tempNext = newHead;
 }

 return newHead;
}


Tuesday, December 13, 2011

Cloud computing Books

http://blog.sina.com.cn/s/blog_6edef4580100t0oy.html

Monday, December 12, 2011

The Boost C++ Libraries

 The Boost C++ Libraries
http://en.highscore.de/cpp/boost/

http://www.boost.org/

How to use Boost in Visual Studio 2010
 http://stackoverflow.com/questions/2629421/how-to-use-boost-in-visual-studio-2010



  1. Unzip the latest version of boost (1.48.0 12/11/2011) into a directory of your choice (e.g. d:\boost_1_48_0).
  2. Create a new empty project in Visual Studio 2010.
  3. Open the Property Manager and expand one of the configuration for the platform of your choice.
  4. Select & right click Microsoft.Cpp..user, and select Properties to open the Property Page for edit.
  5. Select VC++ Directories on the left.
  6. Edit the Include Directories section to include the path to your boost source files.
  7. Repeat steps 3 - 6 for different platform of your choice if needed.




Boost Image Processing (GIL) http://stlab.adobe.com/gil/presentation/index.htm Boost Graph Library http://www.boost.org/doc/libs/1_48_0/libs/graph/doc/index.html

Sunday, December 4, 2011

Big Data --Petabyte

http://en.wikipedia.org/wiki/Petabyte
giga tera peta

Saturday, November 19, 2011

Pigs Get Fat, Hogs Get Slaughtered!

Pigs get fat, Hogs get slaughtered!

I love this proverb, it tells me the most important business rule.



Saturday, November 12, 2011

how to fit data into two probability density functions?

tic-tac-toe OOD design

How to post source code in blogspot

http://vivianningyang.blogspot.com/2009/05/how-to-post-source-code-in-blogspotcom.html

Correlation does not imply causation

http://en.wikipedia.org/wiki/Correlation_does_not_imply_causation

Wednesday, November 9, 2011

smart_ptr vs weak_ptr

http://stackoverflow.com/questions/4984381/in-c-shared-ptr-and-weak-ptr-differences

Thursday, November 3, 2011

Suffix tree

http://en.wikipedia.org/wiki/Suffix_tree



http://mila.cs.technion.ac.il/~yona/suffix_tree/

Wednesday, November 2, 2011

Rectangle overlap

From Crack a google interview lecture notes.

Describe an algorithm that takes an unsorted array of axis‐aligned rectangles and
returns any pair of rectangles that overlaps, if there is such a pair. Axis‐aligned
means that all the rectangle sides are either parallel or perpendicular to the x‐ and
y‐axis. You can assume that each rectangle object has two variables in it: the x‐y
coordinates of the upper‐left corner and the bottom‐right corner.

Good Answer:
1. Create a sorted array of the x coordinates of the left and right edges of
the rectangles.
2. Then, use a "scanline" to move from left to right through the
rectangles.
3. Keep a binary search tree containing the y coordinates of the top and
bottom edges of the rectangles that overlap the scanline.
     For each element of the array, check whether it is a left or right edge.
      -If it is a right edge, remove the corresponding top and bottom edges from the BST.
      -If it is a left edge, search the BST for rectangles that overlap the current rectangle;
                                   if there is one, return the overlap.

Then, add the y coordinates of the top and bottom edges of the rectangle to the BST.
The search takes O(n log n) time, since it takes O(n log n) time to sort the rectangles
and each of the 2n iterations takes O(log n) time.

Linear programming: maximal sum of subsequence

maxim_subsequence( int num_arr[]; int size)
{
  int sum=0;
  int sum_max = 0;

  for(int i=0; i
 {
    sum +=num_arr[i];
    if(sum>sum_max)
      sum_max = sum;
   else if(sum<0) //sum will not contribute to new maximum subsequence.
         sum=0;
}
  return sum_max;
}

Array rearranging algorithm


/*Suppose we have an array a1, a2, ..., an, b1, b2, ..., bn. Implement an algorithm to change
this array to a1, b1, a2, b2, ..., an, bn.*/ a1, b1   ok
a1, a2, b1, b2-> a1, a2<->b1, b2-> a1, b1, a2, b2 ok
a1,a2,a3,a4,b1,b2,b3,b4 -> a1 a2 (b1 b2), (a3,a4), b3, b4-> it becomes 2 subproblems of the above

//size of arr_num must be even number
void rearrange(int arr_num[], int p, int q)
{

if ( p == q || g == p+1) return;
int r = (q+p)/2;

//Exchange: (p+r)/2........r<----> r+1......(r+q)/2
for(int i = (p+r)/2; i
{
int temp= arr_num[i];
arr_num[i]=arr_num[r+1+i];
arr_num[r-1+i] = temp;
};

rearrange(arr_num, p, r);
rearrange(arr_num, r+1, q);
return;

}

Tuesday, November 1, 2011

贪吃蛇走法


给一个吸地毯的irobot,和一个长方形的屋子,四面有墙,四个指令:
Bool moveForward()//向前走一格,走不了的话返回false
Void Rotate(int degree)//就是左拐右拐
Bool isClean()//当前单元格是否干净
Void clean()
把irobot 扔在屋子任意位置,写代码让irobot清理房间,每一格都要走过(单元格没有坐标)。

Stack and Queue

Can we implement a Queue using stack(s)?


Can we implement a minimum stack using stacks?


minimum stack has the following operation:
1. push, pop
2. get minimum.

What is a good OOD design answer?

1. Classes, subclasses.

2. relationship: is-a, has-a, 1->n, n->n

3. design pattern: singleton, observer, MVC.

4. flexible, extendable, reusable.

i.e. How to design a Parking lot?