Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# C语言中的HackerRank数组操作解决方案#_C#_Arrays_Data Structures - Fatal编程技术网

C# C语言中的HackerRank数组操作解决方案#

C# C语言中的HackerRank数组操作解决方案#,c#,arrays,data-structures,C#,Arrays,Data Structures,有人能帮我找出代码中的错误吗?因为我无法通过所有的测试用例 我必须优化这个程序,以便它可以在更短的时间内执行,你能帮忙吗 这个程序应该用0(让我们调用数组arr)初始化一个大小为n(函数参数)的数组,然后就会有形式的查询 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 Aft

有人能帮我找出代码中的错误吗?因为我无法通过所有的测试用例

我必须优化这个程序,以便它可以在更短的时间内执行,你能帮忙吗

这个程序应该用0(让我们调用数组arr)初始化一个大小为n(函数参数)的数组,然后就会有形式的查询

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;
var总和=0;
var greatestenteger=0;
对于(int i=0;i
以下是我的解决方案:

private long FindGreatestNumber(int arraySize, int[,] queries)
        {
            long max = 0;
            var array = new long[arraySize];
            for (int i = 0; i < queries.GetLength(0); i++)
            {
                int start = queries[i, 0] - 1;
                int end = queries[i, 1] - 1;
                for (int j = start; j <= end; j++)
                {
                    array[j] += queries[i, queries.GetLength(1) - 1];
                    if (array[j] > max)
                        max = array[j];
                }
            }
            return max;
        }
private long FindGreatestNumber(int-arraySize,int[,]查询)
{
长最大值=0;
var数组=新长[arraySize];
for(inti=0;i

我相信如果你使用整数数组,它会溢出。

我真的不相信“我要做这个网络挑战来证明我是一个多么棒的程序员。嗯,我不能完成这个挑战。我要请别人帮我做”的概念,因为这完全是错误的,从剽窃到在面试中歪曲自己,再到因为不能胜任被聘用的工作而被解雇,可能会带来可怕的后果(如果你正在编写如飞机控制软件的话)

..但我准备在您现有的解决方案中填写一些我认为可以合理超越所讨论的代码的注释,我希望它们将帮助您找出它可能无法满足挑战的原因

    //skip the part where I declare the array and the number of queries according to the first line of the input 

    //declare some variables far away from where they will be used, just for extra fun 
    var sum = 0;
    var greatestInteger = 0;

    //init arr to all zeroes, in case c# forgot to take its default action of doing that
    for(int i = 0; i< arr.Length; i++)
    {
        arr[i] = 0;
    }

    //queries is just the lines of manipulation instructions; the first line of input
    //was magically dispensed with somewhere else 
    //use a variable called k as an indexer, even though the challenge uses a variable called k to mean something else 
    //just to add in some extra confusion factor for anyone looking at this code 
    for(int k = 0; k <= queries.GetUpperBound(0); k++)
    {
        //the challenge wants every array element between two inclusive 1-based indexes to be incremented
        //and c# uses 0base indexes so I'll sub one off the start index to keep it in line
        //increment the arr start index a by k (the challenge's k, not my variable k)
        arr[queries[k][0]- 1] += queries[k][2];

        //skip the part where I'm supposed to increment everything between a and b by k

        //drop that 0base/1base adjustment logic we did earlier and instead of incrementing, 
        //decrement the arr that is one after the end index by k instead
        arr[queries[k][1]] -= queries[k][2];  
    }

    //forgot to keep a track of the biggest int while we were making
    //the manipulations so we'll do another scan of the array to
    //find it now. Init greatestinteger to be the first element of the array so we could save a loop cycle by starting he loop from 1
    greatestInteger = arr[0];

    //loop over the array, starting from 0
    for(int i = 0; i < arr.Length; i++)
    {
        //accumulate the current array element onto a running total of all the array elements
        sum += arr[i];

        //if the running total is bigger than the biggest integer so far
        if(sum > greatestInteger)
        {
            //then set the biggest bit to be equal to the running total
            greatestInteger = sum;
        }
    }
    //return the total sum of all the array elements, essentially
    return greatestInteger;
//跳过根据输入的第一行声明数组和查询数的部分
//声明一些远离使用它们的地方的变量,只是为了额外的乐趣
var总和=0;
var greatestenteger=0;
//init arr为全零,以防c#忘记执行默认操作
对于(int i=0;i
最后,我做了与你坐下来应对这一挑战时应该做的完全相反的事情。你应该先在注释中写出算法,然后在下面写上c。您将得到实现该算法的注释良好的代码。相反,您编写的代码没有实现算法,因为您试图将其全部保存在头脑中并写出,您生成的代码不正确,不符合规范-明确的遗漏,如在一行上分解一个,然后在下一行上忘记它,这向我表明您没有按照一个高级别的计划工作,这将使您走上正轨。我反向评论说,我已经将您编写的错误代码转换为注释,希望能够以一种明显不同于挑战的方式

我建议从头开始,先在注释中编写算法,然后再编写C


所有专业开发人员首先用高级语言和抽象编写算法和复杂设计;这是一个好的软件工程师的标志,不是一个坏的

上面的代码很好,但是缺少很多方面,我不知道他们是否测试过它。以下是c#中的正确代码:

静态长数组操作(int n,int[][]查询){
long[]计算=新的long[n];
for(int i=0;i
您想做什么?-点击这个链接,回答问题@Johnnymopnope,你可以告诉我你想做什么,以及你的想法。您是否得到任何反馈(比如您使用的算法对于大型数据集来说太慢)?如果是这样的话,告诉我们,如果你想做这个挑战,也许考虑自己在哈克雷克自己去做,而不是“为某人做家庭作业”。并非每个人都处于同一水平。实际上,你可以通过分析自己无法解决的问题的解决方案来学习。我时不时地做leetcode,这对我来说已经足够了。我将永远提倡教一个人如何钓鱼比给一个人一条鱼更有价值。我一点也不在乎“这里有很多帖子”都是鱼的捐赠,它们寻求虚假的互联网点数作为回报;我非常关心通过积极参与解决方案过程而不是作为答录机来提高软件工程的质量(我保留对那些只发布代码回答而没有解释或试图回答问题的人喋喋不休的权利)