`
sillycat
  • 浏览: 2542918 次
  • 性别: Icon_minigender_1
  • 来自: 成都
社区版块
存档分类
最新评论

Interview(1)Algorithm Book

 
阅读更多
Interview(1)Algorithm Book

Chapter 1 Algorithm
Basic Sorting
A[0.. n-1], 0<i<n, system should have A[i-1] <= A[i]
Solution:
5, 2, 7, 4, 6, 3, 1
Bubblesort(S[], n)
exchange the position of 2 items which are next to each other
2, 5, 4, 6, 3, 1, 7
2, 4, 5, 3, 1, 6, 7
2, 4, 5, 1, 3, 6, 7
2, 4, 1, 3, 5, 6, 7
2, 1, 3, 4, 5, 6, 7
1, 2, 3, 4, 5, 6, 7

Max N round will make it working.

Algorithm Performance
Time Performance
Execution Time - depend on inputs
n will be the inputs, Time will be T(n). So if we enlarge T, who the T(n) will increase?
2 * (n - 1) * (n -1)
T(n) < 2 * n * n
T(n) = O(n * n)

Space Complexity
Memory Space system uses.

Non-extremeElement(S[], n)
for example, x, y, z belongs to S, min(x, y, z), max(x, y, z), find the non mix, non max items.

System will only have 1 min, 1 max, so no matter how many items in S, we can pick first 3 items, system should be able to find the non-min non-max one.

O(3), bubble sort to 1, 2, 3. Then choose the middle one. O(3) + O(3) + O(1) = O(7) = O(1)

BaseConversion(n)
N - 10 will be convert to N - 3
for till n = 0 {
    print n mod 3;
    n = n /3
}
101(10) = 10202(3)
1 + log3n = O(2 * (1 + |log3n|)) = O(log3n) = O(log n)

Sum(A[], n)
s = 0, for every A[i], i = 0, 1, …, n-1, s = s + A[i]
O(1) + O(1) x n = O(n+1) = O(n)

PowerBruteForce
{
    power = 1;
    while (0 < r——){
        power = power * 2;
    }
    return power;
}

O(2-n)

Recursive
LinearSum(A, n)
{
    if(n=1){
         return A[0];
    }else{
        return LinearSum(A, n-1) + A[n -1]
    }
}

ReverseArray(A, lo, hi)
{
    if(lo < hi){
        Swap(A[lo], A[hi]);
        ReverseArray(A, lo+1, hi -1);
    }
}

power(2, r) = 2 * 2 * 2…R
recursive solution
power(2, r) = {
                        1                           //if r = 0
                        2 * power(2, r-1)
                      }

Enhancement

power(2, r) = {
                        1    // if r = 0
                        2 * power(2, (r-1)/2) ‘2    //if r > 0 and odd
                        power(2, r/2)’2               //if r > 0 and even
                    }

Power(r)
inputs: r >=0
outputs: 2 * 2 * 2 …r

{
    if(r = 0)  return 1;
    if(r is odd) return 2 * sqr(Power((r-1)/2));
    else          return sqr(Power(r/2));
}
O(logR)

Recursion Trace Performance
LinearSum(A, 5) -   A[0] + A[1] + A[2] + A[3] + A[4]
LinearSum(A, 4) -   A[0] + A[1] + A[2] + A[3]
LinearSum(A, 3) -   A[0] + A[1] + A[2]
LinearSum(A, 2) -   A[0] + A[1]
LinearSum(A, 1) -   A[0]

N * O(1) = O(n)

Binary Recursion
BinarySum(A, i, n)
inputs: A, i >= 0, n >0
outputs: sum of start position i, length n

{
    if(n <=1) return A[i];
    else       return BinarySum(A, i, [n/2]) + BinarySum(A, i+[n/2], [n/2]);
}

Fibonacci
Fib(n) = {
                0    // if n = 0
                1   // if n = 1
                Fib(n -1) + Fib(n-2) //if n>=2
            }

BinaryFib(k)
inputs: k >=0
outputs: Fib(k)
{
    if (k<=1) return k;
    else       return BinaryFib(k-1) + BinaryFib(k-2);
}

Performance O(2 * 2 * 2 ..n)

LinearFibonacci(k)
inputs: k >=0
outputs: Fibonacci (Fib(k), Fib(k-1))
{
    if( k < = 1){
        return (k, 0);
    }
    else {
        (i , j ) = LinearFibonacci(k-1);
        return (i+j, i);
    }
}

O(n)

Multiple Recursion


References:
https://codility.com/programmers/lessons/1-iterations/
https://www.codecademy.com/learn/all
https://leetcode.com/
https://github.com/kamyu104/LeetCode
https://github.com/leetcoders/LeetCode-Java
http://www.programcreek.com/2012/12/leetcode-solution-of-two-sum-in-java/
https://www.gitbook.com/book/lidinghao/leetcode-java/details
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics