【问题标题】:Array Manipulation | Hackerrank runtime error issue数组操作 | Hackerrank 运行时错误问题
【发布时间】:2018-09-16 19:15:59
【问题描述】:

我正在从hackerrank 做这个数组操作问题,它告诉我运行时错误。从我的角度来看,时间复杂度是 O(n)。但它仍然遇到了这个问题。谁能帮帮我?

下面是link

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

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

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

添加索引和包含之间的值:

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

【问题讨论】:

  • m 在定义之前使用
  • 抱歉忘记提到 m 是一个全局变量。 @yar
  • @Robin 然后提供minimal reproducible example完成 - 可验证

标签: python arrays python-3.x


【解决方案1】:

如果你只是在正确的索引处添加/减去,你可以简化这个问题 - 所以你只有 2 个操作而不是 2000 个操作

1 2000 3 

您的查询列表如下:

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

通过整个列表一次,您将获得最大值,然后将所有数字相加 + 在您通过时记住最大值。

这简化了巨大列表的整个问题,这些列表会破坏你的记忆/计算时间。

而不是这样做(对于 200k 长列表)

 1 2000 3  # 2000 times change value
11 2000 3  # 2000 times change value
21 2000 3  # 2000 times change value 
31 2000 3  # 2000 times change value
41 2000 3  # 2000 times change value

你做了 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 == 位置的 dict 中(如果多次出现,请确保更新键的值而不是替换它:

3 7 3
3 9 3
3 5 3

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

弄清楚这些想法是让一些hackerranks 变得有趣的原因(但很多只是不好/不清楚)。

这是代码,玩得开心-复制和粘贴解决方案并不是什么大事-这就是为什么我一开始没有编写代码...

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)

【讨论】:

  • 我实际上是按照这个想法写了上面的代码。我通过了一些测试代码,而不是大量的数据输入。我实际上需要一些代码帮助@Patrick Artner
  • @robin 不知道我怎么能给出更好的描述该怎么做......你去,复制并粘贴它 - 它应该可以工作。
【解决方案2】:

导入 java.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 = new 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);
}

}

【讨论】:

  • 不要只转储代码作为答案;添加解释。除此之外,请进行适当的格式化。
猜你喜欢
  • 2019-10-08
  • 1970-01-01
  • 2021-04-23
  • 2020-07-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多