121. [Apple] You are given a deck containing n cards. While holding the deck:
1. Take the top card off the deck and set it on the table
2. Take the next card off the top and put it on the bottom of the deck in your hand.
3. Continue steps 1 and 2 until all cards are on the table. This is around.
4. Pick up the deck from the table and repeat steps 1-3 until the deck is in the original order.
Write a program to determine how many rounds it will take to put a deck backinto the original order. This will involve creating a data structure to represent the order of the cards. This program should be written in Python. It should take a number of cards
in the deck as a command line argument and write the result to stdout.
略。。。
122. Suppose there is a binary tree having millions of nodes and by mistake one node has two indegree (i.e. There becomes a cycle/loop in that tree. Tou have to find that node which is having two indegree) Constraint is no extra memory can be used and tree representation is in Linked list format.
???可能吗?没有额外memory, 否则做个DFS/BFS即可
123. Print the nodes on the exterior of a binary tree in a anti-clockwise order, i.e., nodes on left edge, then leaf nodes, then nodes on right edge.
先按层遍历,将每一层的开始放到left edge, 结尾放到right edge. 再中序遍历打出所有的leaf node(这里取决于leaf node的定义), CODE取自:
http://www.leetcode.com/2010/10/print-edge-nodes-boundary-of-binary.html
124. 已知整数 m ,其二进制形式有 k 位为1,打印出 0≤x≤m 所有满足以下条件的 x
x^(m-x) = m,其中^是异或运算符。在 0≤m<2^n 范围内对每一个 m ,打印出所有的 x ,并求总复杂度。
TO LEARN, 像数学题
125. You can win three kinds of basketball points, 1 point, 2 points, and 3 points. Given a total score X, print out all the combination to compose X. (recursion/ Dp)
略。。。
126. 有n个job,分配给3个打印机,求所有任务完成的最短时间。例如:3,5,4每个打印机占1个job,最短时间是5. 15,7,8,10, 4, 15给1号打印机,7,8给2号打印机,10,4给3号打印机,最短时间是15.
http://en.wikipedia.org/wiki/Partition_problem
见133题
127. 设计一个算法,判断一个integer n是不是可以表示成k(k>=2)个连续正整数的和
假设所要分解的数为x ,分解成n个数,那么我们可以这样表示:
x=m+(m+1)+(m+2)+……….+(m+n-1)
其中m为分解成的连续整数中最小的那一个,并且我们知道m大于等于1的正整数。易知:
x=(2m+n-1)*n/2, 变换一下的m=(2*x/n-n+1)/2
由m的范围我们知道(2*x/n-n+1)/2>=1 以上就是x和n的关系。给定一个n看是否x能分解成n个连续整数的和可以判断是否存在m,也就是转换成(2*x/n-n+1)是否是偶数。
代码略
128. how to serialize and deserialize a n ary tree
见79题
129. 如何刷屏最快
G(n) = max{G(n-1)+1, g(n-k)*(k-2)}
130. Given a sorted array with duplicates and a number, find the range in the form of (startIndex, endIndex) of that number. For example, given 0 2 3 3 3 10 10 and 3, return (2,4). Given the same array and 6, return (-1,-1).
Binary Search的扩展,见142题
131. 有N个整数,M个bucket,每个bucket都可以装至少N个整数,一个bucket的value等于放入它的所有整数的和。现求解一种分配方法,使之 minimize(the max value of all buckets)
NP完全问题,见:
http://en.wikipedia.org/wiki/Job_shop_scheduling
132. Given pairs of connected items ((A, B), (B, C), (C, D)…), find the root
node of this tree.
找到入度为零的node即可
133. There’s a book and each sentence consists of several words. How to find the minimal set of words which can used by the reader to understand half of total number of the sentences. Each sentence is readable if the reader knows all the words.
TO LEARN, 有点类似于dancing links用到的满足问题
134. [facebook]求一段时间内出现最多的ip address(es)
只能是搞几张aggregate表,按秒,分钟,小时等做为粒度。。。
135. 两个文件里面存着行程安排,开始时间以及结束时间,设计算法找所有conflicts。
如果要找所有conflicts, 则复杂度为O(N^2), 略。。。
136. what’s the best data structure for storing and looking up a huge amount of url’s (presented in a prefix format) like these:
com.google.www -> value1
com.yahoo.finance -> value2
com.yahoo.finance/stocks -> value3
com.yahoo/finance -> value2
1.2.3.4/node/1