HackerRank:翻转矩阵

Posted

技术标签:

【中文标题】HackerRank:翻转矩阵【英文标题】:HackerRank: Flipping the Matrix 【发布时间】:2021-10-24 03:29:52 【问题描述】:

Problem: https://www.hackerrank.com/challenges/flipping-the-matrix/problem

Sample Solution: https://www.hackerrank.com/rest/contests/master/challenges/flipping-the-matrix/hackers/aymanwizard/download_solution?primary=true

我一直难以理解这个问题的解决方案以及如何处理算法。

【问题讨论】:

我在 Hackerrank 中既看不到问题陈述,也看不到它的解决方案。请在您的问题中描述它们。 【参考方案1】:

添加到 Adeniyi 的评论中,可以进一步简化。不需要我可以看到将矩阵的副本创建到数组中。

为了解释,通过将矩阵除以 2 来确定左上象限的大小。

Illustration of possible matrix values

请注意上图中的每个单元格,对于左上象限中的每个单元格,可以将 4 个值之一翻转到该单元格中,1) 当前值,2) 右上象限中的对应值,3)左下象限的对应值,或 4) 右下象限的值(需要行翻转和列翻转,但仍可以移动到左上象限)。

考虑到这一点,循环遍历左上象限中的每个单元格,确定可以翻转到该单元格中的 4 个可能值。在这 4 个值中,只需获取最大值并添加到运行总数中即可。

这里是更新的代码:

    int sum = 0;
    
    //Divide size by 2 to get quadrant size
    int quadSize = matrix.size()/2;


    //Now for each cell in the upper quadrant, get the max value that could be flipped into that cell
    //
    //Iterate all rows in quadrant
    for(int r = 0; r < quadSize; r++)
    
      //Iterate all columns in quadrant
      for(int c = 0; c < quadSize; c++)
      
          //Grab the 4 possible values that could wind up in this location of the upper quadrant
            int p1 = matrix.get(r).get((2*quadSize) - c - 1);
            int p2 = matrix.get(r).get(c);
            int p3 = matrix.get((2*quadSize) - r - 1).get(c);
            int p4 = matrix.get((2*quadSize) - r - 1).get((2*quadSize) - c - 1);

            //Get the highest value possible in this cell
            sum += Math.max(p1, Math.max(p2, Math.max(p3, p4)));
      
    

    return sum;

【讨论】:

感谢@GaryButler,您的解释帮助很大。【参考方案2】:

试试这个

public static int flippingMatrix(List<List<Integer>> matrix) 
// Write your code here
    int sum = 0;
    int n = matrix.size()/2;
    int[][] _matrix = new int[matrix.size()][matrix.size()];
    for(int i = 0; i < _matrix.length; i++)
    
            for(int j = 0; j < _matrix.length; j++)
            
                _matrix[i][j] = matrix.get(i).get(j);
            
    
    
    for(int i = 0; i < n; i++)
        
          for(int j = 0; j < n; j++)
          
              int num1 = _matrix[i][(2*n) - j - 1];
              int num2 = _matrix[i][j];
              int num3 = _matrix[(2*n) - i - 1][j];
              int num4 = _matrix[(2*n) - i - 1][(2*n) - j - 1];

              sum += Math.max(num1, Math.max(num2, Math.max(num3, num4)));
          
        
        return sum;

【讨论】:

您的答案可以通过额外的支持信息得到改进。请edit 添加更多详细信息,例如引用或文档,以便其他人可以确认您的答案是正确的。你可以找到更多关于如何写好答案的信息in the help center。 这与问题中链接的解决方案基本相同。我认为重点是解释它是如何工作的。特别是要证明左上象限中的每个数字都可以独立于其他数字(在四种可能性中)选择。【参考方案3】:

在 Java 中:

public static int flippingMatrix(List<List<Integer>> matrix) 
    
  int n = matrix.size() / 2;
  int sum = 0;
    
  for (int i = 0; i < n; i++) 
    for (int j = 0; j < n; j++) 
      sum += Math.max(Math.max(matrix.get(i).get(j), 
                               matrix.get(i).get(2*n - j - 1)),
                      Math.max(matrix.get(2*n - i - 1).get(j), 
                            matrix.get(2*n - i - 1).get(2*n - j - 1)));
     
  

  return sum;

 

【讨论】:

以上是关于HackerRank:翻转矩阵的主要内容,如果未能解决你的问题,请参考以下文章

hackerrank答案

不可分子集 - Hackerrank

堆栈和优化——来自hackerrank的例子

Hackerrank:Sherlock 和 Anagrams [关闭]

如何清除 Hackerrank 动态数组运行时错误?

HackerRank 天气观测站 5