Twitter online test

avatar 108207
tyr034
6217
15
前几天接收到twitter的面试通知,非常意外,而且正是final的时候,没有办法,只好硬着头皮上了;在codility 上面做了两题,第一题
做出来了,第二题挂了,最终结果也挂了
第一题:
[align="left"]A positive integer N is given. The goal is to construct the shortest possible sequence of integers ending with N, using the following rules:[/align][align="left"]the first element of the sequence is 1; more specifically: A[0] = 1,[/align][align="left"]each of the following elements is generated by multiplying the previous element by 2 or increasing it by 1; more precisely: A = A[i−1] * 2 or A = A[i−1] + 1, for i ≥ 1.[/align][align="left"]For example, for N = 17, the shortest sequence is:[/align][align="left"] A[0] = 1[/align][align="left"] A[1] = 2[/align][align="left"] A[2] = 4[/align][align="left"] A[3] = 8[/align][align="left"] A[4] = 16[/align][align="left"] A[5] = 17[/align][align="left"]Write a function:[/align][align="left"]class Solution { public int solution(int N); }[/align][align="left"]that, given a positive integer N, returns the length of the shortest possible sequence of integers satisfying the above conditions and ending with N.[/align][align="left"]
[/align][align="left"]For example, given N = 17, the function should return 6, as explained above.[/align][align="left"]Assume that:[/align][align="left"]N is an integer within the range [1..2,147,483,647].[/align][align="left"]Complexity:[/align][align="left"]expected worst-case time complexity is O(log(N));[/align][align="left"]expected worst-case space complexity is O(1).[/align][align="left"]
[/align][align="left"]第二题:[/align][align="left"]是two dimesion array ,找equilibrium point. [/align][align="left"]稍后补充完整[/align]
  • 2
15条回复