【问题标题】:Optimise the code to find max consecutive points in a straight line(row,column or diagonal) in a matrix优化代码以在矩阵中的直线(行、列或对角线)中查找最大连续点
【发布时间】:2017-10-02 13:56:43
【问题描述】:

我需要找到矩阵中直线(行、列或对角线)中连续点的最大数量。

例如:- 如果是 4*4 矩阵 输入是 (1#1,2#2,3#3,2#1) 答案应该是 3,因为对角线上的最大连续点是 3。

我的代码已成功执行并获得预期结果。但代码复杂度非常高。

有人可以建议最好的方法复杂性。

下面是我的代码:

// Get the max number of Continuous points in a matrix row
private static int getMaxContinuousPointsInRow(boolean[][] matrix, int row){
    int maxCount = 0;
    int currCount = 0;
    int pos = 0;
    while(pos < matrix[row].length){
        currCount = 0;
    while(pos < matrix[row].length && !matrix[row][pos])
        pos++;
    if(pos >= matrix[row].length)
        break;
    while(pos < matrix[row].length && matrix[row][pos]){
        currCount++;
        pos++;
    }
    if(currCount > maxCount)
        maxCount = currCount;
    }
    return maxCount;
}

// Get the max number of Continuous points in a matrix row
private static int getMaxContinuousPointsInCol(boolean[][] matrix, int col) {
    int maxCount = 0;
    int currCount = 0;
    int pos = 0;

    while (pos < matrix.length) {
        currCount = 0;
    while (pos < matrix.length && !matrix[pos][col])
        pos++;
    if(pos >= matrix.length)
        break;
    while (pos < matrix.length && matrix[pos][col]) {
        currCount++;
        pos++;
    }
    if (currCount > maxCount)
        maxCount = currCount;
    }
    return maxCount;
}

// Get the max number of Continuous points in a matrix diagonal right starting from position (row,col)
private static int getMaxContinuousPointsInDiagonalRight(boolean[][] matrix, int row, int col) {
    int maxCount = 0;
    int currCount = 0;
    int i = row, j = col;
    while (i < matrix.length && j < matrix[row].length) {
        currCount = 0;
    while (i < matrix.length && j < matrix[row].length && !matrix[i][j]){
        i++;
        j++;
    }
    if(i >= matrix.length || j >= matrix[row].length)
        break;
    while (i < matrix.length && j < matrix[row].length && matrix[i][j]) {
        currCount++;
        i++;
        j++;
    }
    if (currCount > maxCount)
        maxCount = currCount;
    }
    return maxCount;
}

public static int function_called_by_main_method(int input1, int input2, String[] input3) {
    // create a boolean matrix of size  input1 x input2
    // M[i][j] = true if  input3 contains a point i#j, else M[i][j] = false
    boolean M[][] = new boolean[input1 + 1][input2 + 1];
    // initialize the matrix with all false values
    for(int i=0; i <= input1; i++){
        for(int j=0; j <= input2; j++){
            M[i][j] = false;
        }
    }
    // process each value in input3 and populate the matrix
    for (String s : input3) {
        // extract row, column value
        String[] data = s.split("#");
        int i = Integer.parseInt(data[0]);
        int j = Integer.parseInt(data[1]);
        M[i][j] = true;
    }
    // get max number of Continuous points among all matrix rows
    int max = 0;
    for(int row = 0; row <= input1; row++){
        int rowMax = getMaxContinuousPointsInRow(M, row);
        if(rowMax > max)
        max = rowMax;
    }
    // get max number of Continuous points among all matrix rows and columns
    for (int col = 0; col <= input2; col++) {
        int colMax = getMaxContinuousPointsInCol(M, col);
        if (colMax > max)
        max = colMax;
    }
    // get max number of Continuous points among all matrix rows, columns and right diagonals
    for(int col = input2 ; col >= 0; col--){
        int diagMax = getMaxContinuousPointsInDiagonalRight(M, 0, col);
        if(diagMax > max)
        max = diagMax;
    }
    for(int row = 1 ; row <= input1; row++){
        int diagMax = getMaxContinuousPointsInDiagonalRight(M, row, 0);
        if(diagMax > max)
            max = diagMax;
    }
    return max;
}

【问题讨论】:

  • 例如:- 如果是 4*4 矩阵输入是 (1#1,2#2,3#3,2#1) 答案应该是 3 作为最大值对角线上的连续点是 3。 我没有理解这个例子,因为我不知道如何解释它
  • @user7185318 i 和 j 是矩阵的行和列如果一个点落在第 1 行第 2 列,输入是 1#2
  • 连续是什么意思?请提供与 (1#1,2#2,3#3,2#1) 一起使用的 4x4 矩阵
  • 例如:- 1#1 , 1#2 在第 1#1 行中是连续的,1#3 不是 1#1,2#2 在对角线 1#1, 3#3 中是连续的不是 1#1,2#1 在 1#1 列中是连续的,3#1 不是
  • 什么是连续的:例如点 (0,0) (1,1) (2,2) 是连续的?是您正在寻找的值 sqrt(xx+yy) 吗?

标签: java algorithm matrix multidimensional-array time-complexity


【解决方案1】:

您可以尝试使用 java 8 流的并行性,采用愚蠢的蛮力方法。

class Coord {
    int i;
    int j;
}

List<Coord[]> allLineCoords = calculateLinesForMatrix(rows, columns);

Comparator<Coord[]> comparator = (coords1, coords2) ->
        length(matrix, coords1) - length(matrix, coords2);

Coord[] maxCoords = allLineCoords.parallelStream()
    .max(comparator);

// or for just the max length:

int maxLength = (int) allLineCoords.parallelStream()
    .mapToInt(coords -> length(matrix, coords))
    .max();

非常不满意的是缺少的情报。而且并行度仅与您计算机的内核数量有关。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-10-30
    • 2019-03-03
    • 2019-10-11
    • 1970-01-01
    相关资源
    最近更新 更多