Python 数组操作| Hackerrank运行时错误问题

Python 数组操作| Hackerrank运行时错误问题,python,arrays,python-3.x,Python,Arrays,Python 3.x,我正在从hackerrank处理这个数组操作问题,它告诉我运行时错误。在我看来,时间复杂度是O(n)。但它仍然有这个问题。有人能帮我吗 下表为: 从一个1索引的零数组和一个操作列表开始,对于每个操作,在两个给定的索引(包括)之间向每个数组元素添加一个值。执行所有操作后,返回数组中的最大值 例如,零数组的长度。您的查询列表如下: a b k 1 5 3 4 8 7 6 9 1 a b k 1 5 3 4 8 7 6 9 1 # demonstation purposes, do not cr

我正在从hackerrank处理这个数组操作问题,它告诉我运行时错误。在我看来,时间复杂度是O(n)。但它仍然有这个问题。有人能帮我吗

下表为:

从一个1索引的零数组和一个操作列表开始,对于每个操作,在两个给定的索引(包括)之间向每个数组元素添加一个值。执行所有操作后,返回数组中的最大值

例如,零数组的长度。您的查询列表如下:

a b k
1 5 3
4 8 7
6 9 1
a b k
1 5 3
4 8 7
6 9 1


# demonstation purposes, do not create list with 200k zero entries - use a dict. see below.
[+3, 0, 0,  0, -3,  0, 0,  0, 0, 0]     # 2 index ops by 1 5 3
[+3, 0, 0, +7, -3,  0, 0, -7, 0, 0]      # 2 index ops by 4 8 7
[+3, 0, 0, +7, -3, +1, 0, -7,-1, 0]     # 2 index ops by 6 9 1

# sums:
  3  3  3  10   7   8  8   1  0  0  # for loop through list, adding all, remembering max
在索引和(含)之间添加的值:

index -> 1 2 3  4  5 6 7 8 9 10
        [0,0,0, 0, 0,0,0,0,0, 0]
        [3,3,3, 3, 3,0,0,0,0, 0]
        [3,3,3,10,10,7,7,7,0, 0]
        [3,3,3,10,10,8,8,8,1, 0]
最大值是在执行所有操作之后

我在这里附上了我的代码:

def arrayManipulation(n,queries):
    increment=[]
    value=[]
    for i in range(n):
        increment+=[0]
        value+=[0]
    for j in range(m):
        left=queries[j][0]
        right=queries[j][1]
        k=queries[j][2]
        increment[left-1]+=k
        if right>=n:
            continue
        else:
            increment[right]-=k
    value[0]=increment[0]
    for i in range(1,n):
        value[i]=value[i-1]+increment[i]
    maximum=max(value)
    return maximum

如果您只需在正确的索引处进行加法/减法运算,就可以简化此问题-因此,您只需要2次运算,而不是2000次运算

1 2000 3 
您的查询列表如下:

a b k
1 5 3
4 8 7
6 9 1
a b k
1 5 3
4 8 7
6 9 1


# demonstation purposes, do not create list with 200k zero entries - use a dict. see below.
[+3, 0, 0,  0, -3,  0, 0,  0, 0, 0]     # 2 index ops by 1 5 3
[+3, 0, 0, +7, -3,  0, 0, -7, 0, 0]      # 2 index ops by 4 8 7
[+3, 0, 0, +7, -3, +1, 0, -7,-1, 0]     # 2 index ops by 6 9 1

# sums:
  3  3  3  10   7   8  8   1  0  0  # for loop through list, adding all, remembering max
通过一次遍历整个列表,只需将所有数字相加,并在遍历时记住最大值,即可获得最大值

这简化了整个问题,因为庞大的列表会破坏您的内存/计算时间

而不是做(对于一个20万长的列表)

您可以更改10个值:

# demonstation purposes, do not create list with 200k zero entries - use a dict. see below.
[ ..., +3, ..., +3, ..., +3, ..., +3, ..., +3, ............, 
       -3, ..., -3, ..., -3, ..., -3, ..., -3, ...] 

{1:3, 11:3, 21:3, 31:3, 41:3, 2001:-3, 2011:-3, 2021:-3, 2031:-3, 2041:-3}
按键排序并添加得到的值时:

 3, 6, 9, 12, 15, 18, 15, 13, 12, 9, 6, 3 
您应该能够将这些内容插入到key==position的dict中(如果发生多次,请确保更新键的值,而不是替换它:

3 7 3
3 9 3
3 5 3

{3:9,7:-3, 9:-3, 5:-3} # 3 updated several times 

弄清楚这些想法是让一些黑客变得有趣的原因(尽管很多想法都不好/不清楚)

这是代码,祝你玩得开心-复制粘贴解决方案并不是什么成就-这就是为什么我没有首先编写代码的原因

maxElem, inputs = [int(n) for n in input().split(" ")]
d = {}
for _ in range(inputs):
    x, y, incr = [int(n) for n in input().split(" ")]
    d.setdefault(x,0)
    d[x]=d[x]+incr 
    if(y <= maxElem+2):
        d.setdefault(y+1,0)
        d[y+1]=d[y+1]-incr

maxx = 0
x= 0 

for i in sorted(d): 
    x+=d[i]
    if(maxx<x): 
        maxx=x

print(maxx)
maxElem,input=[int(n)表示input()中的n。split(“”)
d={}
对于范围内的(输入):
x、 y,incr=[int(n)表示输入()中的n。拆分(“”)
d、 设置默认值(x,0)
d[x]=d[x]+incr
如果(yimportjava.io.*

导入java.math.*

导入java.security。; 导入java.text

导入java.util.*

导入java.util.concurrent.*

导入java.util.regex.*

公共类解决方案{

// Complete the arrayManipulation function below.
static void findMax(int n, int a[],int b[], int k[], int m)
{ int[]arr=新的int[n]

// Start performing m operations
for(int i = 0; i < m; i++)
{
    // Store lower and upper index i.e. range
    int lowerbound = a[i];
    int upperbound = b[i];

    // Add 'k[i]' value at this operation to
    // whole range
    for(int j = lowerbound; j < upperbound; j++)
       arr[j]=arr[j]+k[i];
        
}

// Find maximum value after all 
// operations and return
int res = Integer.MIN_VALUE;
for(int i = 0; i < n; i++)
    res = Math.max(res, arr[i]);


    System.out.println(res);
}



public static void main(String[] args)  {
    Scanner scan = new Scanner(System.in);
    int n=scan.nextInt();
    int m=scan.nextInt();
    int a[]=new int[3];
    int b[]=new int[3];
    int k[]=new int[3];

    for(int i=0;i<a.length;i++)
    {
        a[i]=scan.nextInt();
        b[i]=scan.nextInt();
        k[i]=scan.nextInt();
    }
    findMax(n,a,b,k,m);
}
//开始执行m操作
for(int i=0;i对于(int i=0;在定义之前使用im很抱歉忘记了m是一个全局变量@yar@Robin然后提供一个:complete-verifiable我实际上遵循了这个想法,我写了上面的代码。我通过了一些测试代码,没有大量的数据输入。我需要一些代码方面的帮助@PatrickArtner@robin我不知道我怎么会有giv更好地描述该做什么…好了,复制并粘贴它-它应该可以工作。不要只是转储代码作为答案;添加解释。除此之外,做正确的格式设置。