tegers only, and what happens when it is mixed with positive and negative integers
线性扫描并计算一下即可。正负数是否有关系?
202. 一个arraylist,里面有很多string,它们按照未知的alphabetical顺序排列,要求输出所有可能的alphabetical order,(后来又问了如何判断是否有conflict)
例子:it is a good day today,
it is a good day
两两比较找第一个不同即可。
判断是否有冲突?整个关系形成一个图,这个图应该是无环的,做一次DFS即可。
203. 有一堆的工作,每个工作有开始和结束时间,假设表示为[2,3],[3,7]等等,现在要求在[1,n]的时间段内选出尽可能多的工作.
贪心,尽量选择结束时间最早的工作即可
204. 给一个数组 里边都是整数 求最大乘积的连续子数组
先按零把数组分成若干个子数组,再在子数组里找包含偶数个负数的最大的范围。
205. f(i,j)=2^i*5^j给出一个长度为N的(i,j)序列,使得f值递增,i>=0,j>=0
设f(i,j)是第k个数f_k, 则下一个数取使i+1或者j+1中间比较小的那个即可
206. f(N): return round(reduce(lambda x,y: int(x)+int(y), list(str(N))))/len(N) N为整数,f函数返回N各位数值的平均数,现在给出一个正整数范围[begin, end],要求得出该范围中符合f(N)>=7的数的集合,希望算法尽可能比end-begin+1次test快。
TO LEARN,
207. There is a straight road with ‘n’ number of milestones. You are given an array with the distance between all the pairs of milestones in some random order. Find the position of milestones.
Example:
Consider a road with 4 milestones(a,b,c,d) :
a <— 3Km —>b<— 5Km —>c<— 2Km —>d
Distance between a and b = 3
Distance between a and c = 8
Distance between a and d = 10
Distance between b and c = 5
Distance between b and d = 7
Distance between c and d = 2
All the above values are given in a random order say 7, 10, 5, 2, 8, 3.
The output must be 3,5,2 or 2,5,3
对这个数组里的每一个数,如果它不能表示为该数组里两个数之和,则是一个要求的两个milestone之间的距离
208. 找二叉树两个最大的相同子树
不是很理解题意。。。两两比较即可,复杂度O(N^2),如要加速,可以先计算每个结点的深度和子结点数,相同再进行比较。
209. Given an array of elements find the largest possible number that can be formed by using the elements of the array.
eg: 10 9
ans: 910
2 3 5 78
ans: 78532
100 9
ans: 9100
先按如下规定排序: a > b if ab > ba,
然后再从高到低拼接起来即可。
210. You have an array of 0s and 1s and you want to output all the intervals (i, j) where the number of 0s and numbers of 1s are equal. Example index = 0 1 2 3 4 5 6 7 8 array = 0 1 0 0 1 1 1 1 0 One interval is (0, 1) because there the number of 0 and 1 are equal. There are many other intervals, find all of them in linear time. How can this be done in O(n) find all intervals, not find the longest interval.
O(N)好像不可能做到,因为所有的intervals可能就是O(N^2)级别的