【发布时间】:2020-08-12 18:07:20
【问题描述】:
谁能帮助我在我的代码中哪里出错?因为我无法通过所有的测试用例。
我必须优化这个程序,以便它可以在更短的时间内执行,你能帮忙吗??
这个程序应该用 0 初始化一个大小为 n(函数参数)的数组(让我们调用数组 arr),然后会在表单中查询
a b k
1 2 3
4 5 6
然后在数组 arr 中,我们在限制 a 和 b 之间添加 k,然后返回 arr 的最大值
example Sample Input
5 3
1 2 100
2 5 100
3 4 100
Sample Output
200
Explanation
After the first update list will be 100 100 0 0 0.
After the second update list will be 100 200 100 100 100.
After the third update list will be 100 200 200 200 100.
The returned answer will be 200.
这是我的代码
var sum = 0;
var greatestInteger = 0;
for(int i = 0; i< arr.Length; i++)
{
arr[i] = 0;
}
for(int k = 0; k <= queries.GetUpperBound(0); k++)
{
arr[queries[k][0]- 1] += queries[k][2];
arr[queries[k][1]] -= queries[k][2];
}
greatestInteger = arr[0];
for(int i = 0; i < arr.Length; i++)
{
sum += arr[i];
if(sum > greatestInteger)
{
greatestInteger = sum;
}
}
return greatestInteger;
【问题讨论】:
-
你想做什么?
-
hackerrank.com/challenges/crush/… - 点击此链接查看问题@johnnyMopp
-
不,你可以说出你想要做什么,以及你认为发生了什么。您是否收到任何反馈(例如您使用的算法对于大型数据集来说太慢了)?如果是这样,也告诉我们
标签: c# arrays data-structures