简体   繁体   English

如何操作Java中的数组?

[英]How to manipulate the array in Java?

Starting with a 1-indexed array of zeros and a list of operations, for each operation, I have to add value to each of the array elements between two given indices, inclusive.从一个 1 索引的零数组和一个操作列表开始,对于每个操作,我必须为两个给定索引之间的每个数组元素添加值,包括在内。 Once all operations have been performed, I have to return the maximum value in the array.执行完所有操作后,我必须返回数组中的最大值。 But the wrong answer is coming every time for most test cases and for some test cases time limit is exceeding.但是对于大多数测试用例来说,每次都会出现错误的答案,并且对于某些测试用例来说,时间限制已经超过了。 Kindly help me to solve this problem.请帮我解决这个问题。

I am using arrayManipulation function to get the number of array elements and queries array.我正在使用 arrayManipulation function 来获取数组元素的数量和查询数组。 And update function is used to update(add the element) the array.更新 function 用于更新(添加元素)数组。

import java.util.*;
import java.util.Arrays;
public class sample
{
public static void main(String args[])
{
    int maximum=0;
    Scanner sc=new Scanner(System.in);
    int n=sc.nextInt();
    int m=sc.nextInt();
    int queries[][]=new int[m][3];
    for(int x=0;x<m;x++)
    {
        for(int y=0;y<3;y++)
        {
            queries[x][y]=sc.nextInt();
        }
    }
    maximum=arrayManipulation(n,queries);
    System.out.println(maximum);

}
public static int arrayManipulation(int num,int qry[][])
{
    int a=0,b=0,k=0;
    int max=Integer.MIN_VALUE;
    int arr[]=new int[num];
    int arr2[]=new int[num];
    for(int i=0;i<num;i++)
    {
         arr[i]=0;
    }
    for(int j=0;j<qry.length;j++)
    {
        for(int kl=0;kl<qry[0].length;kl++)
        {
            a=qry[j][kl];
            b=qry[j][kl+1];
            k=qry[j][kl+2];
            break;
        }
       arr2=update(a,b,k,arr);
       int lengtharr2=arr2.length;
       max=Math.max(max,arr2[lengtharr2-1]);
    }
    return max;
}

public static int[] update(int a1,int b1, int k1,int array[])
{

    for(int i=a1;i<b1;i++)
    {
        array[i]+=k1;
    }
    Arrays.sort(array);
    return array;
}
}

Input: 10 means the number of array elements and 3 is no.输入:10 表示数组元素个数,3 表示没有。 of queries which consist of a,b,k values which mean like this: the left index, right index, and summand由 a、b、k 值组成的查询,其含义如下:左索引、右索引和 summand

10 3 10 3

1 5 3 1 5 3

4 8 7 4 8 7

6 9 1 6 9 1

My output:我的 output:

11 11

expected output:预期 output:

10 10

In this for loop:在这个 for 循环中:

for(int kl=0;k<qry[0].length;k++)
{
    a=qry[j][kl];
    b=qry[j][kl+1];
    k=qry[j][kl+2];
    break;
}

Why are you using two different variables ( k and kl ) in your loop condition?为什么在循环条件中使用两个不同的变量( kkl )?

Regardless, you should use a variable other thank k for the loop, as you are updating the value of k and then executing k++ at each iteration of your loop, which is very likely to mess up your output.无论如何,您应该为循环使用其他变量,谢谢k ,因为您正在更新k的值,然后在循环的每次迭代中执行k++ ,这很可能会弄乱您的 output。

Consider something like:考虑类似的事情:

for(int l=0;l<qry[0].length;l++)
{
    a=qry[j][kl];
    b=qry[j][kl+1];
    k=qry[j][kl+2];
    break;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM