Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/algorithm/10.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
Java 在删除数组中的行和列列表后查找最大间距_Java_Algorithm - Fatal编程技术网

Java 在删除数组中的行和列列表后查找最大间距

Java 在删除数组中的行和列列表后查找最大间距,java,algorithm,Java,Algorithm,我有一个大小为m行的数组,n列,每个单元格的大小被视为1x1 现在我要从这个数组中删除行和列的列表,接下来我想知道删除它们后可以形成的最大间隙 示例: Array of size 4 rows and 3 columns, now I am removing rows {1,2,3} and columns {1,2} This results is an array having biggest gap of 12 cells. Array of size 4 rows and 4 c

我有一个大小为m行的数组,n列,每个单元格的大小被视为1x1

现在我要从这个数组中删除行和列的列表,接下来我想知道删除它们后可以形成的最大间隙

示例:

Array of size 4 rows and 3 columns, now I am removing rows {1,2,3} and columns {1,2}

This results is an array having biggest gap of 12 cells.
  Array of size 4 rows and 4 columns, now I am removing rows {2} and columns {2}

This results is an array having biggest gap of 4 cells.
另一个例子:

Array of size 4 rows and 3 columns, now I am removing rows {1,2,3} and columns {1,2}

This results is an array having biggest gap of 12 cells.
  Array of size 4 rows and 4 columns, now I am removing rows {2} and columns {2}

This results is an array having biggest gap of 4 cells.
我提出了以下适用于此示例的代码:

static long process(int n, int m, int[] h, int[] v) {
    ArrayList<ArrayList<Long>> array = new ArrayList<ArrayList<Long>>();
    for (int r = 0; r <= n; r++) {
        ArrayList<Long> temp = new ArrayList<Long>();
        for (int c = 0; c <= m; c++) {
            temp.add((long) 1);
        }
        array.add(temp);
    }

    int[] x = h;
    int xnum = x.length;
    Arrays.sort(x);
    int[] y = v;
    int ynum = y.length;
    Arrays.sort(y);

    // removing bar i means that list at i-1 and at i
    for (int a = xnum - 1; a >= 0; a--) {
        int i = x[a];
        for (int cell = 0; cell < array.get(i).size(); cell++) {
            array.get(i).set(cell, array.get(i).get(cell) + array.get(i - 1).get(cell));
        }
        array.remove(i - 1);
    }

    ArrayList<ArrayList<Long>> newarray = new ArrayList<ArrayList<Long>>();

    for (int col = 0; col < array.get(0).size(); col++) {
        ArrayList<Long> temp = new ArrayList<Long>();
        for (int row = 0; row < array.size(); row++) {
            temp.add(array.get(row).get(col));
        }
        newarray.add(temp);
    }

    for (int b = ynum - 1; b >= 0; b--) {
        int i = y[b];
        for (int cell = 0; cell < newarray.get(i).size(); cell++) {
            newarray.get(i).set(cell, newarray.get(i).get(cell) + newarray.get(i - 1).get(cell));
        }
        newarray.remove(i - 1);
    }

    long max = 1;
    for (ArrayList<Long> arr : newarray) {
        for (long num : arr) {
            if (num > max)
                max = num;
        }
    }
    return max;
}
静态长进程(int n,int m,int[]h,int[]v){
ArrayList数组=新的ArrayList();
对于(int r=0;r=0;b--){
int i=y[b];
for(int cell=0;cell最大值)
max=num;
}
}
返回最大值;
}
因为行和列的大小是:

1 <= rows, columns <= 100000

1让我们从查看当前解决方案开始,通过使用简单数组而不是ArrayList,我们可以将此代码简化为:

static long process(int rows, int cols, int[] hor, int[] ver) {
    final long[][] a = new long[rows][cols];
    for (int i = 0; i < rows; i++) for (int j = 0; j < cols; j++) a[i][j] = 1;

    for (int h : hor) for (int j = 0; j < cols; j++) a[h - 1][j] = a[h][j] = a[h - 1][j] + a[h][j];
    for (int v : ver) for (int i = 0; i < rows; i++) a[i][v - 1] = a[i][v] = a[i][v - 1] + a[i][v];

    long max = 0;
    for (int i = 0; i < rows; i++) for (int j = 0; j < cols; j++) max = Math.max(max, a[i][j]);
    return max;
}
静态长进程(int行、int列、int[]hor、int[]ver){
最终长[][]a=新长[行][cols];
对于(inti=0;i
此代码的复杂性为O(N^2)

但是,我们应该理解,最大的差距将出现在删除最大数量的连续行和列的地方,因此我们可以将算法简化为:

static int maxConsecutive(int[] a) {
    Arrays.sort(a);

    int max = 0;
    int start = 0;
    for (int i = 0; i < a.length; i++)
        if (a[i] - a[start] == i - start) max = Math.max(max, i - start + 1);
        else start = i;

    return max;
}

static long process(int rows, int cols, int[] hor, int[] ver) {
    long maxH = maxConsecutive(hor);
    long maxV = maxConsecutive(ver);
    return (maxH + 1) * (maxV + 1);
}
static int maxconcertive(int[]a){
数组。排序(a);
int max=0;
int start=0;
for(int i=0;i

它的复杂性为O(logN)

让我们先看看您当前的解决方案,通过使用简单数组而不是ArrayList,我们可以将此代码简化为:

static long process(int rows, int cols, int[] hor, int[] ver) {
    final long[][] a = new long[rows][cols];
    for (int i = 0; i < rows; i++) for (int j = 0; j < cols; j++) a[i][j] = 1;

    for (int h : hor) for (int j = 0; j < cols; j++) a[h - 1][j] = a[h][j] = a[h - 1][j] + a[h][j];
    for (int v : ver) for (int i = 0; i < rows; i++) a[i][v - 1] = a[i][v] = a[i][v - 1] + a[i][v];

    long max = 0;
    for (int i = 0; i < rows; i++) for (int j = 0; j < cols; j++) max = Math.max(max, a[i][j]);
    return max;
}
静态长进程(int行、int列、int[]hor、int[]ver){
最终长[][]a=新长[行][cols];
对于(inti=0;i
此代码的复杂性为O(N^2)

但是,我们应该理解,最大的差距将出现在删除最大数量的连续行和列的地方,因此我们可以将算法简化为:

static int maxConsecutive(int[] a) {
    Arrays.sort(a);

    int max = 0;
    int start = 0;
    for (int i = 0; i < a.length; i++)
        if (a[i] - a[start] == i - start) max = Math.max(max, i - start + 1);
        else start = i;

    return max;
}

static long process(int rows, int cols, int[] hor, int[] ver) {
    long maxH = maxConsecutive(hor);
    long maxV = maxConsecutive(ver);
    return (maxH + 1) * (maxV + 1);
}
static int maxconcertive(int[]a){
数组。排序(a);
int max=0;
int start=0;
for(int i=0;i

它的复杂性为O(logN)

我不确定我是否理解示例,在3x2的数组中有6个单元格,如何获得12个单元格的间隔?工作代码更适合:@Iłya Bursov,抱歉,我现在更新了它,这个概念就像监狱里的栏杆,移除栏杆以获得最大的间隔。@学习者我仍然不明白你问题中的间隔是什么,你能画一些图片吗?@IłyaBursov,我现在添加了图片或第一个示例,你能检查一下吗?我不确定我是否理解示例,在3x2数组中,你有6个单元格,如何获得12个单元格的间隔?工作代码更适合:@Iłya Bursov,抱歉,我现在更新了它,这个概念就像监狱里的栅栏和移除栅栏以获得最大的间隙。@学习者我仍然不明白你的问题中的间隙是什么,你能画一些图片吗?@IłyaBursov,我现在添加了图像或第一个示例,你能检查数组吗。排序(a)是O(nLog(n)),所以我就是复杂度数组。排序(a)是O(nLog(n)),所以我就是复杂度