zoukankan      html  css  js  c++  java
  • English interview

    Background introduction

    Here are some problems related to Big-O notation.

    From now on, I will try to write blogs in English for my English interview.

    But below the English descriptions there will be some translations in Chinese, so you guys who don't know English well can still read my article.

    中文:

    这是一些关于算法分析的题目。

    我最近要开始尝试用英语写博客了,但是我会附上中文翻译在下面。

    Upper bound or Lower bound?

    Question:

    Suppose your friend discovers a new algorithm and in his excitement tells you that his algorithm has a lower bound of O(n2). Can you explain why your friend's statement makes no sense?

    Answer:

    Let me explain the definition of the two bounds.

    The lower bound is the best an algorithm can perform. Oppositely,the upper bound is the worst an algorithm can perform.

    And Big O notation cares about the upper bound but is silent about the lower bound.

    We can not say in the best case the running time complexity is quadratic or better,may be in linear or constant.

    Hence,It makes no sense and it's not logical.

    中文解读:

    大O表示法关注的是一个最差的情况,但是下界意思是最佳情况,所以下界是O(n^2)这种表述的意思是 在最佳情况下,算法的时间复杂度最差是n^2,这种说法是不合逻辑的。


    Formal Definition of Big O

    Question:

    Does O(2^2n) equal O(2^n) ?

    Answer:

    First of all, O(g(n)) means a set of functions.

    if O(2^2n) equals O(2^n) , it means that two statements are ture at the same time.

    • we can find any functions which belongs to O(2^2n) must belongs to O(2^n)
    • we can find any functions which belongs to O(2^n) must belongs to O(2^2n)

    Let's test the f(n) = 2^2n

    if 2^2n = O(2^n) , so the inequality 0 <= 2^2n <= c2^n holds when c is positive and n is greater than n0

    let's divide both sides by 2^n, and we can see

    0 <= 2^n <= c ,when c is fix, we can find a n' which makes the inequality false.

    So O(2^2n) is not equal to O(2^n)

    中文解读:

    首先翻译一下题目,能想得到若题目条件成立,则必须同时满足两个条件:

    • 任何属于2^2n函数集合的函数也都属于2 ^ n集合
    • 任何属于2^n函数集合的函数也都属于2 ^ 2n集合

    这两个集合就一模一样了。

    用反证法证明,若2^2n属于O(2 ^n),最后推出矛盾结果,所以并不相等。


    M % N means?

    Determine the time complexity for the following snippet of code

        void complexMethod(int n, int m) {
    
           for (int j = 0; j < n; j++) {   
              for (int i = 0; i < m % n; i++) {
                  System.out.println("")
              } 
           }
        }
    

    Answer:

    m%n is the key to solve the problem.

    if m equals n, m%n equals 0 and the inner loop will not run. The complexity is O(n)

    if m is less than n ,m%n equals m and the complexity is O(m*n).

    The last case is when m is greater than n, m%n equals value ranging from 1 to n-1, So the complexity will in the worse case be O(n^2)

    Hence, the complexity would be O(n^2)

    中文解读:

    内层循环的余数需要分为三种情况讨论,最差的情况是O(n^2),所以整个算法的时间复杂度是O(n ^2)

  • 相关阅读:
    New Skateboard
    1127
    一张图看懂开源许可协议,开源许可证GPL、BSD、MIT、Mozilla、Apache和LGPL的区别
    vim 快捷键绑定
    使用git 上传项目到gitee/github
    Linux进程/线程调度策略与 进程优先级
    【框架】共享内存组设计思路与实现(更新中)
    linux下六大IPC机制【转】
    详解Linux内核红黑树算法的实现
    Linux 内核里的数据结构:红黑树(rb-tree)
  • 原文地址:https://www.cnblogs.com/ging/p/14858939.html
Copyright © 2011-2022 走看看