dificulty解决o一个code(LOGN) [英] dificulty solving a code in O(logn)

查看:320
本文介绍了dificulty解决o一个code(LOGN)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写了一个函数,获取作为输入的唯一整数列表,以便(从小到大)。即时通讯应该找到与列表中的相匹配的索引的值的索引。例如如果L [2] == 2的输出是真实的。 所以在我做了,在复杂度为O(LOGN)我现在想找到多少指标表现得像是在给定列表具有相同复杂度为O(LOGN)研究。 即时上传我的第一个code,做我需要帮助的第一部分和第二code:

 高清steady_state(L):

    低= 0
    上部的len =(L)-1
    而较低的< =上:
        middle_i =(上+下)// 2
        如果L [middle_i] == middle_i:
            返回middle_i
        elif的L [middle_i]≥middle_i:
            上= middle_i-1
        其他:
            低= middle_i +1
    返回None


高清cnt_steady_states(L):
    低= 0
    上部的len =(L)-1
    A = B = steady_state(L)的
    如果steady_state(L)==无:
        返回0
    其他:
        CNT = 1
        而真正的:

            如果L [上] ==上且A =上:
                CNT + =上-A
                上=一
            如果L [下] ==低,B> =低:
                CNT + = B-低
                低= B
 

解决方案

这是不可能的,你给还没有限制。理论上可以达到最佳的复杂性的 0 的( N 的)。

0 的()假设最坏的情况下(只是一个定义,你可能会下降的那部分)。在最坏的情况下,您的总是的必须看的每个的项目,以检查其是否等于它的索引。

如果您有更多的限制(如数字都是整数,没有可能出现一次以上,即没有连续两个数是相等的)的情况下更改。也许是这样的话?

编辑:

听,其实我认为限制(即只一次出现整数),我现在提出这种方法后:你可以安全地假设你只能有恰好一个连续范围,其中所有的匹配项的位置。一,即你只需要找到一个下限和上限。然后通缉的结果将是在该范围的大小。

每个绑定可以安全地使用二进制搜索,这对每个人都有被发现的 0 的(记录的 N 的)。

 高清BINSEARCH(场,低=真,A = 0,B =无):
  如果b为无:
    B = LEN(场)
  而一个+ 1&其中; B:
    C =(A + B)/ 2
    如果下:
      如果现场[C]< C:
        一个= C
      其他:
        B = C
    其他:#寻找上限
      如果现场[C]> C:
        B = C
      其他:
        一个= C
  返回b如果其他低一个

高清indexMatchCount(场):
  上部= BINSEARCH(场,低级=假)
  低= BINSEARCH(场,B =上+ 1)
  返回上 - 下+ 1
 

用于测试此我:

 字段=列表({范围内random.randint(-10,30),我(30)})
field.sort()
上部= BINSEARCH(场,低级=假)
低= BINSEARCH(场,B =上+ 1)
对于I,F在历数(场):
  打印低级LT = I< =上,我==楼I,F
 

I wrote a function that gets as an input a list of unique ints in order,(from small to big). Im supposed to find in the list an index that matches the value in the index. for example if L[2]==2 the output is true. so after i did that in complexity O(logn) i now want to find how many indexes behave like that in the given list with the same complexity O(logn). im uploading my first code that does the first part and the second code which i need help with:

def steady_state(L):

    lower= 0
    upper= len(L) -1
    while lower<=upper:
        middle_i= (upper+ lower)//2
        if L[middle_i]== middle_i:
            return middle_i
        elif L[middle_i]>middle_i:
            upper= middle_i-1
        else:
            lower= middle_i +1
    return None


def cnt_steady_states(L):
    lower= 0
    upper= len(L) -1
    a=b=steady_state(L)
    if steady_state(L)== None:
        return 0
    else:
        cnt=1
        while True:

            if L[upper] == upper and a<=upper:
                cnt+= upper-a
                upper= a
            if L[lower]== lower and b>=lower:
                cnt+= b- lower
                lower = b

解决方案

It's not possible with the restrictions you've given yet. The best complexity you can theoretically achieve is On).

O() assumes the worst case (just a definition, you could drop that part). And in the worst case you will always have to look at each item in order to check it for being equal to its index.

The case changes if you have more restrictions (e. g. the numbers are all ints and none may appear more than once, i. e. no two consecutive numbers are equal). Maybe this is the case?

EDIT:

After hearing that in fact my assumed restrictions apply (i. e. only once-appearing ints) I now propose this approach: You can safely assume that you can have only exactly one continuous range where all your matching entries are located. I. e. you only need to find a lower bound and upper bound. The wanted result will then be the size of that range.

Each bound can safely be found using a binary search, of which each has O(log n).

def binsearch(field, lower=True, a=0, b=None):
  if b is None:
    b = len(field)
  while a + 1 < b:
    c = (a + b) / 2
    if lower:
      if field[c] < c:
        a = c
      else:
        b = c
    else:  # search for upper bound
      if field[c] > c:
        b = c
      else:
        a = c
  return b if lower else a

def indexMatchCount(field):
  upper = binsearch(field, lower=False)
  lower = binsearch(field, b=upper+1)
  return upper - lower + 1

This I used for testing:

field = list({ random.randint(-10, 30) for i in range(30) })
field.sort()
upper = binsearch(field, lower=False)
lower = binsearch(field, b=upper+1)
for i, f in enumerate(field):
  print lower <= i <= upper, i == f, i, f

这篇关于dificulty解决o一个code(LOGN)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆