Friday, February 28, 2014

use 3 color to paint a cube (都要用上)求多少种涂法儿)

Suppose you have three colors in which to paint the face of a cube, 
either red, blue, or yellow. How many different color patterns are 
there if each face of the cube must be painted red, blue, or yellow 
(assuming that two patterns are the same if the two cubes painted 
with them can be made to look the same by simply rotating one or the 
other. E.g. there is only one pattern of say, five red faces and one 
yellow).
 
分情况呗。    首先, 至少要先上r,b,y 3个颜色。
那么还剩3种颜色要取:   
1) 全r, 全b, 全y
2) 两个相同,一个不同   6 种情况  (3*2)  先选2 个(r) 。再选另一个 (b)
3)  再一次 r,b,y 
solution  =  3 * ( 3个r )  +  6 * ( 2个 ,1个b) + (r,g,b)

然后看其permutation , 首先,先固定一个面。
3个r的情况 = 先固定 b,再找 y,  再4个r = 1(把b放在bottom)*2(b在上面或者某个侧面) *1 (全填即可)=2
 
r,b,y +  2个r,+ b = 1 (先固定y) *【1 b 一上一侧) + 1(bb在侧面相邻) + 1( bb不相邻)】* 1(全填r)= 3
 
再一次 r,b,y = step 1 * 先固定一个r在最底下 = 1 
               step 2:  固定另一个r   
                       case 1: 在上面  1种。  其余4个面,有2种涂法
                       case 2; 在侧边, 1种  ,其余4个面, step 1 : 先选一个在上边, 2种选择,例如选择b
                                                         step 2:  确定第二个r的位置。2种选择。
                                                         step 3; fill 2 y 
solution = 3*2 + 6 * 3 + 1*{ 2 + 2*2  }= 30 种?????????


这里有个链接 , 或许可以看


This is a problem requiring the use of Burnside's lemma. You consider 
the 24 symmetries of a cube and sum all those symmetries that keep 
colours fixed. The number of non-equivalent configurations is then the 
total sum divided by the order of the group (24 in this case).

We first find the cycle index of the group of FACE permutations 
induced by the rotational symmetries of the cube.

Looking down on the cube, label the top face 1, the bottom face 2 and 
the side faces 3, 4, 5, 6 (clockwise).

You should hold a cube and follow the way the cycle index is 
calculated as described below. The notation (1)(23)(456) = 
(x1)(x2)(x3) means that we have a permutation of 3 disjoint cycles 
in which face 1 remains fixed, face 2 moves to face 3 and 3 moves to 
face 2, face 4 moves to 5, 5 moves to 6, and 6 moves to 4. (This is 
not a possible permutation for our cube, it is just to illustrate the 
notation.) We now calculate the cycle index.

(1) e = (1)(2)(3)(4)(5)(6);  index = (x1)^6
(2) 3 permutations like (1)(2)(35)(46); index 3(x1)^2.(x2)^2
(3) 3 permutations like (1)(2)(3456); index 3(x1)^2.(x4)
(4) 3 further as above but counterclockwise; index 3(x1)^2.(x4) 
(5) 6 permutations like (15)(23)(46); index 6(x2)^3
(6) 4 permutations like (154)(236); net index 4(x3)^2
(7) 4 further as above but counterclockwise; net index 4(x3)^2

Then the cycle index is

P[x1,x2,...x6] =(1/24)[x1^6 + 3x1^2.x2^2 + 6x2^3 + 6x1^2.x4 + 8x3^2]

and the pattern inventory for these configurations is given by the 
generating function:  

(I shall use r = red, b = blue, y = yellow as the three colours.)

f(r,b,y) = (1/24)[(r+b+y)^6 + 3(r+b+y)^2.(r^2+b^2+y^2)^2 
           + 6(r^2+b^2+y^2)^3 + 6(r+b+y)^2.(r^4+b^4+y^4) 
           + 8(r^3+b^3+y^3)^2]

and putting r = 1, b = 1, y = 1 this gives

  = (1/24)[3^6 + 3(3^2)(3^2) + 6(3^3) + 6(3^2)(3) + 8(3^2)]

  = (1/24)[729 + 243 + 162 + 162 + 72]

  = 1368/24

  = 57   

So there are 57 non-equivalent configurations.

n个数, 其中一个数出现次数大于1/6 n 次, 其余都小于这个次数。 求这个数

就是每次保证,扔掉6个不同的数。

如何找比一个int大的下一个palindrome。

CODE, 24 点

. BST,排序双链表互相转换

最长上升子序列

实现 itoa ???????????????

Merge N sorted array ??????????

Make a heap from the first element in each array.
Pop the head element from the heap, insert it into the result array, and then take the next element from the array the head of the heap came from, and insert that into the heap.
Repeat until you consume all of the arrays.

Thursday, February 27, 2014

c++中,int, long的大小问题

                     int (in bytes)            long(in bytes)
16位机:          2                                   4

32位机:          4                                  4
64位机:          4                                  8 (windows系统还是4)   


Wednesday, February 19, 2014

Lowest Common Ancestor of a Binary Tree with parent pointer

Given a binary tree, find the lowest common ancestor of two given nodes in the tree. Each node contains a parent pointer which links to its parent.

Note:
This is Part II of Lowest Common Ancestor of a Binary Tree. If you need to find the lowest common ancestor without parent pointers, please read Lowest Common Ancestor of a Binary Tree Part I.
        _______3______
       /              \
    ___5__          ___1__
   /      \        /      \
   6      _2       0       8
         /  \
         7   4
 
 
 
 An easy solution:   use HashSet

The best solution:

A little creativity is needed here. Since we have the parent pointer, we could easily get the distance (height) of both nodes from the root. Once we knew both heights, we could subtract from one another and get the height’s difference (dh). If you observe carefully from the previous solution, the node which is closer to the root is always dh steps ahead of the deeper node. We could eliminate the need of marking visited nodes altogether. Why?

The reason is simple, if we advance the deeper node dh steps above, both nodes would be at the same depth. Then, we advance both nodes one level at a time. They would then eventually intersect at one node, which is the LCA of both nodes. If not, one of the node would eventually reach NULL (root’s parent), which we conclude that both nodes are not in the same tree. However, that part of code shouldn’t be reached, since the problem statement assumed that both nodes are in the same tree.

int getHeight(Node *p) {
  int height = 0;
  while (p) {
    height++;
    p = p->parent;
  }
  return height;
}
 
// As root->parent is NULL, we don't need to pass root in.
Node *LCA(Node *p, Node *q) {
  int h1 = getHeight(p);
  int h2 = getHeight(q);
  // swap both nodes in case p is deeper than q.
  if (h1 > h2) {
    swap(h1, h2);
    swap(p, q);
  }
  // invariant: h1 <= h2.
  int dh = h2 - h1;
  for (int h = 0; h < dh; h++)
    q = q->parent;
  while (p && q) {
    if (p == q) return p;
    p = p->parent;
    q = q->parent;
  }
  return NULL;  // p and q are not in the same tree
}

Lowest Common Ancestor of a Binary Tree w/t parent pointer

Given a binary tree, find the lowest common ancestor of two given nodes in the tree.
        _______3______
       /              \
    ___5__          ___1__
   /      \        /      \
   6      _2       0       8
         /  \
         7   4
If you are not so sure about the definition of lowest common ancestor (LCA), please refer to my previous post: Lowest Common Ancestor of a Binary Search Tree (BST) or the definition of LCA here. Using the tree above as an example, the LCA of nodes 5 and 1 is 3. Please note that LCA for nodes 5 and 4 is 5.


 A Top-Down Approach (Worst case O(n2) ):
// Return #nodes that matches P or Q in the subtree.
int countMatchesPQ(Node *root, Node *p, Node *q) {
  if (!root) return 0;
  int matches = countMatchesPQ(root->left, p, q) + countMatchesPQ(root->right, p, q);
  if (root == p || root == q)
    return 1 + matches;
  else
    return matches;
}
 
Node *LCA(Node *root, Node *p, Node *q) {
  if (!root || !p || !q) return NULL;
  if (root == p || root == q) return root;
  int totalMatches = countMatchesPQ(root->left, p, q);
  if (totalMatches == 1)
    return root;
  else if (totalMatches == 2)
    return LCA(root->left, p, q);
  else /* totalMatches == 0 */
    return LCA(root->right, p, q);
}

A Bottom-up Approach (Worst case O(n) ):

Node *LCA(Node *root, Node *p, Node *q) {
  if (!root) return NULL;
  if (root == p || root == q) return root;
  Node *L = LCA(root->left, p, q);
  Node *R = LCA(root->right, p, q);
  if (L && R) return root;  // if p and q are on both sides
  return L ? L : R;  // either one of p,q is on one side OR p,q is not in L&R subtrees
}





Monday, February 17, 2014

traval maximum unvisited cities each day.

Q:
 Input (arr, k )
Output:a list of ending cities till traversal.

背景描述:

Given n cities, indexed from 0 to n-1, we have an array arr, of size n, where A[i] =j, means that there is a direct road between city i and j. there are n-1 roads, all cities are connected.
 for example :  we have 7 cities
                           3                   5
                           |                   | 
                           |                   |
                           |                   |
0 ------------------1 -------------- 2 ----------------4 ----------6
array[ 0 ]  = 1
array[ 1]  = 2
array[ 2 ]  =4
array[ 3 ]  = 1
array[ 4 ]  =6
array[ 5 ]  =2
array[ 6 ]  = 4    

Starting from city  k , each day we visit maximum number of unvisited cities, , but we traval back on the same day.

Print the last city that you visited each day, (ending city) , untill all cities are marked visited.
For example:   k = 2, 
one day 1 , you can visit 3 unvisited cities,   2-1-0(ending city is 0),  or 2-1-3,  or 2 - 4- 6.
so, our final output could be :
0   6     5 3
also, it could be   0 6 3 5,   or  6 0 3 5   or  6 0 5 3,  or 3 6 1 5  or 3 6 5 1

O(n) space &  time

A:





Sunday, February 16, 2014

一维数组,向左/右 循环移动k位

Q:

一维数组,向左/右  循环移动k位


A:
   先全部reverse, 再[0,k-1] 和 [k, end] 分别reverse

代码
 #!/usr/bin/python
  2 def reverse(arr,low,high):
  3     while(low<high):
  4         arr[low],arr[high] = arr[high],arr[low]
  5         low+=1
  6         high-=1
  7 
  8 lst = [n for n in range(0,14)]
  9 print(lst)
 10     
 11 k = int(input("enter right shift number k = "))%len(lst)
 12 reverse(lst,0,len(lst)-1)
 13 reverse(lst,0,k-1)
 14 reverse(lst,k,len(lst)-1)
 15 
 16 print(lst)