【问题标题】:How to solve two dimensional growth grid problem?如何解决二维增长网格问题?
【发布时间】:2022-05-07 20:22:00
【问题描述】:

我正在为求职面试做评估。我必须在一小时内解决的 3 个问题之一是在网格中找到最大值,您可以在其中遍历它并根据给定的坐标将元素加 1。我在第二个问题上花了一点时间,最后只用了大约 20 分钟。我没有及时完成它,所以它困扰着我。

我只是想确保对我记忆中的问题的解决方案进行了优化。

输入是一个包含两个 int 值和网格维度的 String 数组。

为了说明,如果给定的坐标是 (3,2) (2,2) (1,3) 那么

[1][1][0]     
[1][1][0]     
[1][1][0]     

[1][1][0]     
[2][2][0]     
[2][2][0]     

[1][1][0]
[2][2][0]
[3][3][1]

等等……

我相信所需的结果是不在 (1,1) 中的最大值以及它在网格中存在的次数。

这是我想出的解决方案。有什么办法可以优化吗?

public static List<Integer> twoDimensions(String[] coordinates, int n) {

    List<Integer> maxAndCount = new ArrayList<Integer>();
    int[][] grid = new int[n][n];
    int arrLength = coordinates.length;
    int max = Integer.MIN_VALUE;
    int count = 1;

    for (int i = 0; i < arrLength; i++) {

        String[] coors = coordinates[i].split(" ");
        int row = Integer.parseInt(coors[0]);
        int column = Integer.parseInt(coors[1]);

        for (int j = 0; j < row; j++) {
            for (int k = 0; k < column; k++) {

                grid[j][k] += 1;
                System.out.println("grid (" + j + "," + k + "): " + grid[j][k]);

                if (!(j == 0 & k == 0) && grid[j][k] > max) {

                    max = grid[j][k];
                    count = 1;

                } else if (grid[j][k] == max) {

                    count++;

                }
            }
        }
    }

    maxAndCount.add(max);
    maxAndCount.add(count);

    return maxAndCount;
}

public static void main(String[] args) {

    String[] coors = { "1 3", "2 4", "4 1", "3 2" };
    System.out.println("The Max and count Are:" + twoDimensions(coors, 4).toString());
}

【问题讨论】:

  • 我假设在你的图中,(1,1) 从左下角开始?我这样说是因为在编程中 (1,1) 通常从左上角开始。我看到你已经做了行主顺序(坐标中的第一个数字是指垂直距离)
  • 另外我认为如果要添加的矩形总是从 (1,1) 开始,那么在删除 (1,1) 之后,最大值将总是在 (1,2) 或 (2,1) ,所以找到最大值只是比较这两个值,返回较大的值。也许还有其他你没有提到的条件?

标签: java optimization multidimensional-array


【解决方案1】:

锻炼的其他解决方案是。 (erikdhv@gmail.com)

public static long countMax(List<String> upRight) {
    // Write your code here
    int xl = 1;
    int yl = 1;

    xl = Integer.parseInt(upRight.get(1).split(" ")[0]);
    yl = Integer.parseInt(upRight.get(1).split(" ")[1]);

    for (int i=0; i<upRight.size(); i++){
        if (xl > Integer.parseInt(upRight.get((int) i).split(" ")[0]) ) xl = Integer.parseInt(upRight.get((int) i).split(" ")[0]);
        if (yl > Integer.parseInt(upRight.get((int) i).split(" ")[1])) yl = Integer.parseInt(upRight.get((int) i).split(" ")[1]);
    }

    return (yl * xl);
}

【讨论】:

    【解决方案2】:

    这在我能想到的几个测试用例中失败了,XL 的值应该在索引 0 而不是 1 上

    【讨论】:

      【解决方案3】:

      erik 的答案在少数测试用例中失败,只需将 xl 和 yl 的 int 更改为 long 和繁荣......所有测试用例都通过了。

      【讨论】:

        【解决方案4】:
        public static long twoDimensions(List<String> list, int n) {
            long res = 0;
            int rowCount =0,colCount=0;
            for(int i=0;i<n;i++)
            {
                int row = list.get(i).charAt(0) - 48;
                int col = list.get(i).charAt(2) - 48;
                if(row > rowCount)
                    rowCount = row;
                if(col >colCount)
                    colCount = col;
            }
            int tempArr[][] = new int[rowCount+1][colCount+1];
            for(int i = 0;i<n;i++)
            {
                int row = list.get(i).charAt(0) - 48;
                int col = list.get(i).charAt(2) - 48;
                for(int j=row;j>=1;j--)
                {
                    int m = j;
                for(int k=1;k<=col;k++)
                {
                    tempArr[m][k]= tempArr[m][k]+1;
                    if(tempArr[m][k]>res)
                        res = tempArr[m][k];
                }
                }
                System.out.println("for row: "+row+" col: "+col);
                for(int j = 0;j<=rowCount;j++)
                {
                    for(int k=0;k<=colCount;k++)
                    {
                        System.out.print(""+tempArr[j][k]);
                    }
                    System.out.println("");
                }
                System.out.println("");
            }
            return res;
            }
        

        【讨论】:

        • 您的答案可以通过额外的支持信息得到改进。请edit 添加更多详细信息,例如引用或文档,以便其他人可以确认您的答案是正确的。你可以找到更多关于如何写好答案的信息in the help center
        猜你喜欢
        • 2015-07-21
        • 1970-01-01
        • 2019-02-23
        • 1970-01-01
        • 1970-01-01
        • 2023-02-18
        • 2023-04-03
        • 2020-12-08
        • 1970-01-01
        相关资源
        最近更新 更多