English 中文(简体)
特定结构的最大要素
原标题:max element in specific structure

我有一系列长篇,从中得出以下顺序:

b[0] = 0
b[1] = b[0] + a[0]
b[2] = b[1] + a[0]
b[3] = b[2] + a[1]
b[4] = b[3] + a[0]
b[5] = b[4] + a[1]
b[6] = b[5] + a[2]
b[7] = b[6] + a[0]
b[8] = b[7] + a[1]
b[9] = b[8] + a[2]
b[10] = b[9] + a[3]
#etc.

a 可能含有不积极值。 我需要找到最大部分。 我只收到O(n^2)的解决办法。 是否有更快的办法?

def find_max(a):
  b = [0]
  i = 0
  count = 0
  while i < len(a):
    j = 0
    while j <= i:
      b.append(b[count] + a[j])
      count += 1
      j += 1
    i += 1
  return max(b)
问题回答

仍然精细,但似乎较快五倍,我发现它更清楚/更明确了<>?

from itertools import chain, accumulate

def find_max_2(a):
    def prefixes():
        prefix = []
        for x in a:
            prefix.append(x)
            yield prefix
    increments = chain.from_iterable(prefixes())
    return max(accumulate(increments, initial=0))

独角兽器似乎仍比你更快四倍:

from itertools import chain, islice, repeat, accumulate

def find_max_fun(a):
    return max(accumulate(chain(*map(islice, repeat(a), range(1, len(a) + 1))), initial=0))

Or with a :

def Shlemiel(xs):
    path = []
    for x in xs:
        path.append(x)
        yield from path

def find_max_fun(a):
    return max(accumulate(Shlemiel (a), initial=0))

*** 虽然这或许只是因为你使用了相当不成熟的代码。 我可以这样写:

def find_max(a):
    b = [0]
    for i in range(1, len(a) + 1):
        for x in a[:i]:
            b.append(b[-1] + x)
    return max(b)

这里是O(n)解决办法:

def find_max(a):
  b = [0]
  i = 1
  max_a = 0

  while i <= len(a):
    for j in range(0, max_a + 1):
      b.append(b[i-1] + a[j])
      i += 1
      if i >= len(a):
        break
    max_a += 1
  return max(b)

测试:

print(find_max([1, 2, 3, -4, -2, 1])) # 7

由于产出减少,你可以看到进展:

def find_max(a, debug=False):
  b = [0]
  if debug:
    print (f b[0] = {b[0]} )
  i = 1
  max_a = 0

  while i <= len(a):
    for j in range(0, max_a + 1):
      b.append(b[i-1] + a[j])
      if debug:
        print (f b[{i}] = b[{i-1}] + a[{j}] )
      i += 1
      if i >= len(a):
        break
    max_a += 1
  return max(b)

<代码>(定义_max([1、2、3、4、2、1]、debug=True])

b[0] = 0
b[1] = b[0] + a[0]
b[2] = b[1] + a[0]
b[3] = b[2] + a[1]
b[4] = b[3] + a[0]
b[5] = b[4] + a[1]
7




相关问题
How to add/merge several Big O s into one

If I have an algorithm which is comprised of (let s say) three sub-algorithms, all with different O() characteristics, e.g.: algorithm A: O(n) algorithm B: O(log(n)) algorithm C: O(n log(n)) How do ...

Grokking Timsort

There s a (relatively) new sort on the block called Timsort. It s been used as Python s list.sort, and is now going to be the new Array.sort in Java 7. There s some documentation and a tiny Wikipedia ...

Manually implementing high performance algorithms in .NET

As a learning experience I recently tried implementing Quicksort with 3 way partitioning in C#. Apart from needing to add an extra range check on the left/right variables before the recursive call, ...

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

Enumerating All Minimal Directed Cycles Of A Directed Graph

I have a directed graph and my problem is to enumerate all the minimal (cycles that cannot be constructed as the union of other cycles) directed cycles of this graph. This is different from what the ...

Quick padding of a string in Delphi

I was trying to speed up a certain routine in an application, and my profiler, AQTime, identified one method in particular as a bottleneck. The method has been with us for years, and is part of a "...

热门标签