【问题标题】:HackerRank Array Manipulation Solution in C#C# 中的 HackerRank 数组操作解决方案
【发布时间】: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


【解决方案1】:

上面的代码都很好,但是缺少很多方面我不知道他们有没有测试过。这是 c# 中的正确代码:

static long arrayManipulation(int n, int[][] queries) {
    long[] computation = new long[n];

    for (int i = 0; i < queries.Length; i++) {
        int a = queries[i][0] - 1;
        int b = queries[i][1] - 1;
        int k = queries[i][2];

        computation[a] += k;
        if (b + 1 < n ) {
            computation[b + 1] -= k;
        }
    }

    long max = 0; long sum = 0;
    for (int i = 0; i < n; i++) {
        sum += computation[i];
        max = Math.Max(max, sum);
    }

    return max;
}

【讨论】:

    【解决方案2】:

    这是我的解决方案:

    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;
            }
    

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

    【讨论】:

    • 如果你想做这个挑战,也许可以考虑在hackerrank上自己做,而不是“为他们做作业”
    • @CaiusJard 你可以为这里的大量帖子争辩说。不是每个人都处于同一水平。实际上,您可以通过分析自己无法弄清楚的问题的解决方案来学习。我时不时地做 leetcode,这对我来说已经足够了。
    • 我将永远提倡,教一个人如何钓鱼比给一个人一条鱼更有价值。我不在乎“这里的大量帖子”是鱼的捐赠,以寻求虚假的互联网积分作为回报;我非常关心通过积极参与解决方案过程而不是成为答录机来提高软件工程的质量(并且我确实保留对那些发布代码仅以零解释或尝试进行教育的回答的人进行抨击的权利)
    • 我不认为这个问题是无用的(尽管许多其他人都这么认为),因为我发布了一个答案,但我认为为了自己的利益而接受个人挑战然后来的想法出于各种原因,要求 SO 社区为您解决它是错误的,这就是为什么我没有竭尽全力给您答案,而是花了合理的时间提供尽可能多的指示来帮助您学习并让你自己解决这个挑战。这也是为什么我要向 insane_developer 指出,仅仅给你答案就会对每个人造成伤害
    • 如果我没有提到“黑客等级”这个词,我想这不会发生,每个人都会把它当作一个严肃的问题来弄清楚我实际遇到的问题。跨度>
    【解决方案3】:

    我不太相信“我要参加这个网络挑战赛来证明我是一个多么了不起的编码员。嗯。我无法完成挑战。我要问别人所以为我做”因为这是各种错误的,从抄袭到在面试中歪曲自己,再到因为你不能胜任被聘用的工作而被解雇,可能会带来可怕的后果(如果你正在写飞机控制软件)..

    ..但是我准备在您现有的解决方案中填写一些我认为可以合理地超越相关代码的 cmets,我希望它们能帮助您弄清楚它可能无法应对挑战

        //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;
    

    最终,我所做的与您应该在您坐下来应对这一挑战时所做的完全相反。你应该在 cmets first 中写出算法,然后在它下面写 c#。您最终会得到实现该算法的注释良好的代码。相反,您编写的代码没有实现算法,因为您试图将其全部记在脑海中并写出来,您生成了不正确的代码并且不符合规范 - 清楚诸如在一行中逐一考虑然后在下一行忘记它的遗漏向我表明你没有制定一个能让你保持正轨的高级计划。我已经反向评论说,我已将您编写的不正确的代码转换为评论,希望以某种方式使其与挑战不同的地方显而易见。

    我建议重新开始,先用 cmets 编写算法,然后再编写 C#。

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

    【讨论】:

      【解决方案4】:

      这是我的愚蠢解决方案

          public static long arrayManipulation(int n, List<List<int>> queries)
          {
              List<long> myList = new List<long>(new long[n]);
      
              foreach (var list in queries)
              {
                  for (int i = list[0] - 1; i <= list[1] - 1; ++i)
                  {
                      myList[i] += list[2];
                  }
              }
      
              return myList.Max();
          }
      

      【讨论】:

        【解决方案5】:
          public static long arrayManipulation(int n, List<List<int>> queries)
            {
                    long[]  outputArray= new long[n];
                        for (int i = 0; i < queries.Count(); i++) {
                           outputArray[queries[i][0]-1]=outputArray[queries[i][0]-1] +queries[i][2];
                           if(queries[i][1]<n)
                           {
                           outputArray[queries[i][1]]=outputArray[queries[i][1]]- queries[i][2];   
                           }
                        }
                        long temp=0,max=0;
                        for(int i=0;i<n;i++){
                            temp=temp+outputArray[i];
                            if(temp>max)
                                max=temp;
                        }
                         return max;
            }
        

        【讨论】:

          猜你喜欢
          • 2020-11-15
          • 2020-05-14
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多