java中LeftToPCorner练习的Codibility maxpath

java中LeftToPCorner练习的Codibility maxpath,java,Java,我正在努力解决这个问题 以下是练习的内容: You are given a matrix A consisting of N rows and M columns, where each cell contains a digit. Your task is to find a continuous sequence of neighbouring cells, starting in the top-left corner and ending in the bottom-right corn

我正在努力解决这个问题

以下是练习的内容:

You are given a matrix A consisting of N rows and M columns, where each cell contains a digit. Your task is to find a continuous sequence of neighbouring cells, starting in the top-left corner and ending in the bottom-right corner (going only down and right), that creates the biggest possible integer by concatenation of digits on the path. By neighbouring cells we mean cells that have exactly one common side.

    Write a function:

    class Solution { public String solution(int[][] A); }

    that, given matrix A consisting of N rows and M columns, returns a string which represents the sequence of cells that we should pick to obtain the biggest possible integer.

    For example, given the following matrix A:

    [9 9 7] [9 7 2] [6 9 5] [9 1 2]

    the function should return "997952", because you can obtain such a sequence by choosing a path as shown below:

    [9 9 *] [* 7 *] [* 9 5] [* * 2]

    Write an efficient algorithm for the following assumptions:

            N and M are integers within the range [1..1,000];
            each element of matrix A is an integer within the range [1..9].
我无法达到100%,因为矩阵值都相同的情况下我失败了

我试着按照练习中的要求从左到右、从下阅读矩阵,但我想我误解了这个问题

这是我的密码:

static String sol(int[][] A) {

    String st = "";

    int v = A.length - 1;
    int h = A[0].length - 1;
    if (h == 0) {
        for (int i = 0; i <= v; i++) {
            st = st.concat(String.valueOf(A[i][0]));
        }
    } else if (v == 0) {
        for (int i = 0; i <= h; i++) {
            st = st.concat(String.valueOf(A[0][i]));
        }

    } else {

        st = st.concat(String.valueOf(A[0][0]));

        int m = 0; //vertical
        int n = 0; // horizontal
        while(m<v && n<h) {
            if(A[m+1][n]>=A[m][n+1]){
                st = st.concat(String.valueOf(A[m+1][n]));
                m++;
            }else {
                st = st.concat(String.valueOf(A[m][n+1]));
                n++;
            }

        }

        st = st.concat(String.valueOf(A[v][h]));
    }


    return st;
}
静态字符串sol(int[]A){
字符串st=“”;
int v=A.长度-1;
int h=A[0]。长度为-1;
如果(h==0){

对于(inti=0;i这是我对问题的解决方案,虽然它会在某些情况下失败。我需要获得更多的测试案例来改进算法

console.log(解决方案[9,2,9,3,5],
[ 5, 3, 9, 5, 1 ],
[ 5, 6, 4, 9, 1 ],
[ 4, 3, 8, 4, 5 ],
[ 1, 4, 3, 5, 3 ]]));
log(解决方案)[[3,6,3,8,5,3,9,8,8,6,2,4,3,8,1],
[ 5, 6, 8, 3, 3, 7, 5, 4, 4, 3, 2, 6, 9, 7, 6 ],
[ 9, 2, 9, 3, 5, 9, 4, 5, 2, 9, 9, 2, 2, 5, 5 ],
[ 5, 3, 9, 5, 1, 7, 1, 2, 1, 6, 8, 6, 3, 8, 8 ],
[ 5, 6, 4, 9, 1, 9, 7, 8, 8, 2, 8, 6, 2, 8, 4 ],
[ 4, 3, 8, 4, 5, 5, 4, 6, 9, 1, 6, 3, 6, 6, 1 ],
[ 1, 4, 3, 5, 3, 8, 6, 7, 9, 5, 5, 2, 8, 1, 4 ],
[ 1, 7, 9, 4, 9, 4, 6, 9, 2, 1, 2, 1, 4, 2, 1 ],
[ 7, 9, 7, 9, 1, 6, 4, 3, 8, 3, 9, 4, 5, 7, 8 ],
[ 7, 1, 2, 6, 3, 9, 8, 8, 4, 8, 6, 8, 3, 5, 4 ]]));
log(解决方案[9,9,9,9,9,9,9,9,9,9],
[ 9, 9, 9, 9, 9, 9, 9, 9, 9, 9 ],
[ 9, 9, 9, 9, 9, 9, 9, 9, 9, 9 ],
[ 9, 9, 9, 9, 9, 9, 9, 9, 9, 9 ],
[ 9, 9, 9, 9, 9, 9, 9, 9, 9, 9 ],
[ 9, 9, 9, 9, 9, 9, 9, 9, 9, 9 ],
[ 9, 9, 9, 9, 9, 9, 9, 9, 9, 9 ],
[ 9, 9, 9, 9, 9, 9, 9, 9, 9, 9 ],
[ 9, 9, 9, 9, 9, 9, 9, 9, 9, 9 ],
[ 9, 9, 9, 9, 9, 9, 9, 9, 9, 9 ]]))
函数解(A){
var-nextindex=0;
var solution=A[0][0].toString();

对于(i=0;i这是我对该问题的解决方案,但它未通过上次速度测试(我得到77%)。不确定我是否可以对其进行更优化

public static String solution(int[][] A) {
    // write your code in Java SE 8

    int al = A.length;
    int all = A[0].length;

    BigInteger[][] res = new BigInteger[al+1][];
    for(int i=0; i<al+1; i++){
        res[i] = new BigInteger[all+1];
        for(int j=0; j<all+1; j++){
            res[i][j] = BigInteger.valueOf(0);
        }
    }

    for(int i=1; i<al+1; i++){
        for(int j=1; j<all+1; j++){
            res[i][j] = res[i-1][j]
                    .max(res[i][j-1])
                    .multiply(BigInteger.valueOf(10))
                    .add(BigInteger.valueOf(A[i-1][j-1]));
        }
    }

    return res[al][all].toString();

}
公共静态字符串解决方案(int[]A){
//用JavaSE8编写代码
int al=A.长度;
int all=A[0]。长度;
BigInteger[]res=新的BigInteger[al+1][];

对于(int i=0;i我尝试从
@Bojan Vukasovic
优化解决方案。我猜由于两个主要原因,它无法达到100%:

  • BigInteger
    的二维数组分配了太多内存(我有一个OOM用于大小为1000×1000的大输入数组)。为了避免这种二维
    res
    数组可以替换为大小为
    m
    的两个一维数组(行数)。解决方案如下所示:

     static String solution(int[][] A) {
    
      final int m = A.length;
      final int n = A[0].length;
    
      MutableInteger[] result = new MutableInteger[m];
      result[0] = new MutableInteger(A[0][0], m + n + 1);
    
      // initial raw from up to down
      for (int i = 1; i < m; i++) {
          result[i] = new MutableInteger(result[i - 1]).append(A[i][0]);
      }
    
      for (int j = 1; j < n; j++) {
          // top row we only can reach from left
          result[0].append(A[0][j]);
          // moving down
          for (int i = 1; i < m; i++) {
              MutableInteger previous = result[i - 1];
              MutableInteger current = result[i];
              // only replace if previous is bigger
              if (previous.compareTo(current) > 0) {
                  result[i].copy(previous);
              }
              result[i].append(A[i][j]);
          }
      }
    
      return result[m - 1].toString();
    }
    

该类非常简单,有两个用于优化的变量。
sum
(即使在整数溢出后,两个相等数组也具有相同的
sum
值)和
maxIndex
(用于从数字数组的开头跳过比较).

给我们提供一个到Codality的链接是有问题的。它不会落在挑战的实际文本上,而是开始某种测试。此外,您确实需要格式化错误文本。这确实不可读。问题是“在java中”并被标记为
java
。以不同的语言发布不同的错误尝试不会回答问题。您至少可以解释您的尝试是如何工作的,它与问题代码的不同之处,以及如何解决问题代码中的错误(如果有).我投了否决票,因为算法不正确。正如你自己所说,它通过了一些测试用例。如果从你不正确的算法中可以获得一些见解,用文字描述这种见解会更有用。我没有说它通过了一些测试用例,我说它“会”由于问题中的信息,我只能提出有限的测试用例,您是否运行了代码片段?它解决了问题中关于相同数量的测试用例的问题。
private static class MutableInteger {
  int[] digits;
  int position;
  int sum;
  int capacity;
  int maxIndex;

  private MutableInteger(int digit, int capacity) {
      this.digits = new int[capacity];
      digits[0] = digit;
      sum = digit;
      this.capacity = capacity;
      position++;
  }

  private MutableInteger(MutableInteger value) {
      digits = value.digits.clone();
      position = value.position;
      capacity = value.capacity;
      sum = value.sum;
      maxIndex = value.maxIndex;
  }

  private MutableInteger append(int value) {
      digits[position] = value;
      position++;
      sum = Math.abs(sum * 10 + value); // here integer overflow to compare exact digits efficiently
      return this;
  }

  private void copy(MutableInteger value) {
      digits = value.digits.clone();
      position = value.position;
      capacity = value.capacity;
      sum = value.sum;
  }

  private int compareTo(MutableInteger other) {
      // optimization for long arrays comparison
      if (this.sum != other.sum) {
          int start = Math.min(this.maxIndex, other.maxIndex);
          for (int i = start; i < this.position; i++) {
              int left = this.digits[i];
              int right = other.digits[i];
              if (left != right) {
                  other.maxIndex = i; // don't change this.maxIndex, it will be used in next iteration
                  return Integer.compare(left, right);
              }
          }
      }
      return 0;
  }

  @Override
  public String toString() {
      StringBuilder out = new StringBuilder(position);
      for (int i = 0; i < position; i++) {
          out.append(digits[i]);
      }
      return out.toString();
  }
 }