算法类面试题-8

来源:互联网 发布:创业软件经营状态 编辑:程序博客网 时间:2024/05/28 15:44

211. Rabin Karp Algorithm

    略。。。

 

212. Given a list of presentations with begin and end time that all need to use a conference room. We want to optimize the utilization of the room by allowing maximum hours of the conference room being used.

DP题,先把所有的presentation按endtime排序,然后设q(endtime)为在endtime之间conference room被使用的最长时间。然后依次用presentation与之间所有的q相比,得到新的q.总体复杂度O(N^2)

 

213. Given an array of int, each int appears exactly TWICE in the array. Find and return the int such that this pair of int has the max distance between each other in this array.
e.g. [2, 1, 1, 3, 2, 3]
2: d = 5-1 = 4;
1: d = 3-2 = 1;
3: d = 6-4 = 2;
return 2

用hashtable保存num->index,然后线性扫描一遍即可

214. 二进制加法
/**
* i.e.
*
* char a[] = “11″;
* char b[] =  “1″;
* char *c = bstradd(a, b); // c is a pointer to “100″
**/

215. 给你一列单词/字符串(内部字符范围:unicode),例如:banana, cat, dog, elephant, type, middle, lake, 让你把这些单词排列成任意相邻单词不能有任何相同字符的序列,如果确定无法满足这个要求,返回false.

考虑一个图,满足的首尾字母不同即有一条a->b的边,那该问题等价于找Hamilton圈,是一个NP完全问题

 

216. 判断(二叉)树是否镜像对称

    镜像对称的定义?

 

217. Given a binary tree, find 2 leaf nodes say X and Y such that F(X,Y) is maximum where F(X,Y) = sum of nodes in the path from root to X + sum of nodes in the path from root to Y – sum of nodes in the common path from root to first common ancestor of the Nodes X and Y

除了穷举,没啥好idea… 求所有的和O(N),求最近公共父结点可以优化到O(1)具体比较为O(N^2),总体复杂度为O(N^2).

 

218. x^n = y, x/n/y都是整数,n>1,y叫做一个啥数来着,姑且叫做Super Cool数吧,
比如,1^2 = 1×1=1, 1^3 = 1×1×1 = 1 …
2^2 = 2×2=4, 2^3 = 2×2×2 = 8 …
现在给你一个整数y,请返回最近的那个Super Cool数,写Code。

对每一个小于sqrt(y)的数,求对数后找一个最接近的即可。。

 

219. 题目是给定一个unsorted int array, 怎么找到第一个大于0,并且不在此array的integer。
比如[1,2,0] return 3, [3,4,-1,1] return 2. (time O(n), constant space?)

    将第i个数放到i位,再从头扫描

    220. 给定一个数字数组 (Let’s call it count-array) ,其中每个元素是从末端数小于原数组中该元素的个数。求原数组。原数组中元素是从1到n。
Example:
原数组  4,1, 3, 2
Count array  3, 0, 1, 0
求nlogn的算法。

    CODE,考虑通过merge sort的变形

 

221将整型变量 x 中数字左右翻转后存到另外一个整型变量 y中,例如 x = 12345 时,y为 54321,x = ‐123 时,y为‐321。其中 x 的个位不为 0。      void reverse (int x, int* y); 

    CODE

 

222. 对集合{1, 2, 3, …, n}中的数进行全排列,可以得到 n!个不同的排列方式。现在我们用字母序把它们列出来,并一一标上序号,如当 n=3 时:
0.123
1.132
2.213
3.231
4.312
5.321
现在,请书写一个函数 void print (int n, int k), (函数原型是用 C语言写的,你可以用你熟悉的语言)在已知 n和序号 k 的情况下,输出对应的排列,并简要阐述思路

    223. 一维数轴上有 n 条线段,它们的端点都是已知的。请设计一个算法,计算出这些线段的并集在数轴上所覆盖的长度,并分析时间复杂度。例如,线段 A 的坐标为[4, 8],线段 B 的坐标为[1, 5.1], 那么它们共同覆盖的长度为 7。 请尽量找出最优化的算法, 解释算法即可,不必写代码。

    TO LEARN, INTERVAL TREE

 

224. 3 sorted arrays, A, B, C, find indexes i, j, k, so that max(a-b, b-c, c-a) is minimized. (a = A[i], b = B[j], c = C[k]) Another version is to minimize max(|a-b|, |b-c|, |c-a|).

    IDEA:类似于三路归并,反复地将当前最小的元素往前移,并Update目标值即可。

 

225. 一条直线上有N个站台,已知任何两点间直达列车的票价,求出从起点到终点的票价最优的乘车方案。因为从A到B,再从B到C的价格可能比直接从A到C便宜

    DP。。。。

226. N个job,要求分配到M台机器上,每个机器可以被分配0-N个job,但有些job相互排斥不能被放到一起执行,给出所有可能的分配方案

要给出所有方案只能递归穷举吧

 

227. 给N个元素,第i个元素有一个大于0的score(i),要求随机选出k个,每个元素可以被选择任意多次,但保证被选择的概率要和score(i)成比例

    将0-1之间按scroe(i)的比例划分成区间,然后生成0-1之间的随机数,按其所在区间决定为哪个元素

 

228. N个矩形,所有矩形都有一条边在同一条直线上,他们相互可能有overlap,找出最后得到的这个不规则图形的所有边界点

TO LEARN,

也是INTERVAL TREE或者SEGMENT TREE?先取出所有的点,再把被任意矩形所包含的点去掉。

 

229. 给两颗树,如果节点深度相同且value相同,则这两个node是match的,两棵树上的节点如果相互match,则它们的父节点必须也要match。假设一棵树上所有node的value都不同,并且兄弟节点间不用考虑顺序,问给两棵树,如何求最大match的node数目。如果value有重复,并且要求兄弟节点match的顺序一致,问如何求最大match数。

    CODE

 

230. Write a program to find the largest possible rectangle of letters such that every row forms a word (reading left to right) and every column forms a word (reading top to bottom). Words should appear in this dictionary: WORD.LST (1.66MB). Heuristic solutions that may not always produce a provably optimal rectangle will be accepted: seek a reasonable tradeoff of efficiency and optimality. (Hint: Use a B-Tree)

http://www.itasoftware.com/careers/work-at-ita/hiring-puzzles.h

    TO LEARN, hard.

 

231. 直方图盛水

    CODE

 

232. We have been given a deck of cards and each combination has a rating eg 3 As is higher than 2 As 1K etc. Write an algorithm such that the probability of picking 3 or 5 or 7 cards from the deck results in high rating

    DP,写递推公式

 

233. 求一个unsorted数组中最长的等差数列(int 数组,可递增或者递减)
http://compgeom.cs.uiuc.edu/~jeffe/pubs/pdf/arith.pdf

    难题。。。略

 

Jump Game:
Given an array start from the first element and reach the last by jumping. The jump length can be at most the value at the current position in the array. Optimum result is when u reach the goal in minimum number of jumps.
For ex:
Given array A = {2,3,1,1,4}
possible ways to reach the end (index list)
i) 0,2,3,4 (jump 2 to index 2, then jump 1 to index 3 then 1 to index 4)
ii) 0,1,4 (jump 1 to index 1, then jump 3 to index 4)
Since second solution has only 2 jumps it is the optimum result.
要求:O(N) time, O(1) space.

类似于DP?反复update jump N+1步后,记录到达当前位置的最小步数


234.一个数组,不考虑重复数字,找出数组中有多少个cycle。cycle:每个element去找他对应的index,如果index在界内,就继续找这个index的值所对应的下一个index直到找到一个cycle,或者出界。
比方说 0,2,1,9,10
对于0, index 0 就是 val 0, 所以是一个cycle
对于2, index 2 是 1, 在界内,就找 index 1,which is 2,所以又是一个cycle
总共4个cycles: 0–>0, 2 -> 1, 9 -> 界外, 10->界外
先要求写了个可以有extra space的,然后要求no extra space

No extra space需要修改原数组吧。。。CODE

 

235. bool isOverlap(int a, int b, int c, int d)参数取值范围0~6代表星期几。能不能不用大于号和小于号

    用一个七个数的数组来标记就行了吧

 

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

    先走到角落,然后来回扫就行了

 

237. Edit Distance

    见185题

 

238. {1,5, -5, -8,2,  -1,15 } 要把负的扫到左边,正的扫到后边。不能改变顺序得到{-5 -8 -1 1 5 2 15}  这个题有time 低于 n^2 space=O(1)的解法吗

应该没有这样的解?

 

239.给一个通常的表达式,转成后缀表达式

    CODE

 

240. Given n arrays, find n number such that sum of their differences is minimum. For e.g. if there are three arrays

A = {4, 10, 15, 20}
B = {1, 13, 29}
C = {5, 14, 28}
find three numbers a, b, c such that |a-b| + |b-c| + |c-a| is minimum. Here the answer is a = 15, b = 13, and c = 14

    同224题

 

241. reverse double linked list.

    CODE

 

242. [Facebook] Given an array A of positive integers. Convert it to a sorted array with
minimum cost. The only valid operation are:
1) Decrement with cost = 1
2) Delete an element completely from the array with cost = value of element

DP,写递推公式,使[0,i]中的元素有序,update时,加入[0,i+1],要么decrement以前的,要么删掉i+1

 

243. 在一个平面上有n个点,设计算法看能不能找出四个点构成一个正方形,分析时间复杂度。

    TO LEARN

 

244. Algorithm to find the two numbers whose difference is minimum among the set of numbers.
For example the sequence is 5, 13, 7, 0, 10, 20, 1, 15, 4, 19
The algorithm should return min diff = 20-19 = 1.
Constraint – Time Complexity O(N) & Space is not a constraint [upto O(3N)]
Assumption – Sorting O(nlogn) & comparison of adjacent numbers is already known & is not an option. Try to keep it linear

    想不出来。。。

 

245. Given an array of integers (both positive and negative) divide the array
into two parts (sub-arrays) such that the difference between the sum of
elements in each array is minimum?

如果subarray连续的话很简单。。。

 

246. 给一个string, 比如“facebook”, 可以拆成“face”和“book”, 对任一string, 找出最长的可以拆分成其他单词的子串。

    先在string里找是单词的范围,得到一组range,然后将这些range按起点排序,再递归+回溯,找到最长的能正好拼接在一起的最长的范围

 

247. 有10个unsorted array, 分给10太不同的机器处理,这10台机器之间不能通信,但可以和总机通信,如何求总的median. 如何减少数据量的传输。

    TO LEARN, 设计题

 

248. You have an array like ar[]= {1,3,2,4,5,4,2}. You need to create another array ar_low[] such that ar_low[i] = number of elements lower than or equal to ar[i] in ar[i+1:n-1].
So the output of above should be {0,2,1,2,2,1,0} Time complexity : O(n) use of extra space allowed.

等价于前面一个求逆序对的题的吧,感觉不可能做到O(N),

 

249. 给一串数字(比如说1,4,10,22,30,表示4个区间:[1,4],(4,10],(10,22],(22,30])。现在给很多个数字,要设计一个快速算法,能用最快的速度告诉那些数字分别落在哪个bucket那里。比如说前面这个例子输入double数13,算法返回string: “(10,22]”;输入double[]序列13,8,25,返回string[] “(10,22]” “(4,10]” “(22,30]”

    TO LEARN, INTERVAL TREE?

 

250. Given Numerator and Denominator. After division you might get a recurring decimal points float as the answer. You need to identify the recurring part? For example 23.34563456 … return 3456

    CODE

 

251. A positive integer is said to be square free if it is divisible by no perfect square larger than 1. For example, the first few squarefree numbers are {1, 2, 3, 5, 6, 7, 10, 11, 13, 14, 15, 17, 19, …}. Find the nth smallest squarefree number. Note n can be very large such as 1M.

先找出sqrt(n)以内的所有质数。。再想想

 

252.判断一个string是否是某个pattern的周期循环

    TO LEARN,后缀树?

 

253. 在一个N Dimensional 的正方形里面,Assume the top right point is (n,n,…. n) and bottom left point is (0, 0, 0…. 0), given any point in the cube, find all the paths inside the cube to the (n, n,…n) around it, change its value to 1. Otherwise, mark its value to 0 (cannot recall exactly anymore). how to do it. Given a very big such matrix and n computer, how to do it efficiently. Assume each computer has very limited memory, how to do it.

    TO LEARN

 

254. 给个数组,没排序,已知数组中每个元素距离排序以后的位置最多是k,让你给这个数组排序

    TO LEARN, Shell排序?

 

255.两个线段数组,求common区间 A[1,5][10,15]B [3,12] return [3,5],[10,12]

TO LEARN, Interval tree?

 

256. minimum window cover

    TO LEARN

 

257. Given a sorted array of n integers, pick up k elements so that the minimal difference between consecutive elements is maximal (that is choose the elements to maximize the quantity min(a[i+1] – a[i]))

DP题,写递推公式

fr:http://www.mianwww.com/html/2011/10/11343.html

原创粉丝点击