Algorithm 美化矩阵
我最近在一次采访中收到了这个问题,但我不知道如何解决它,我想确保我从中吸取教训,并知道如何为未来解决它 问题: 让我们把方阵的美定义为最小正整数,不会出现在这个矩阵中。例如,如果平方矩阵2x2包含数字1、2、4和6,则该矩阵的美丽度将等于3 您将得到一个称为“数字”的正整数平方矩阵,以及一个整数“大小”,该整数将数字等分。长度。您的任务是按以下方式排列矩阵元素:Algorithm 美化矩阵,algorithm,sorting,matrix,matrix-multiplication,Algorithm,Sorting,Matrix,Matrix Multiplication,我最近在一次采访中收到了这个问题,但我不知道如何解决它,我想确保我从中吸取教训,并知道如何为未来解决它 问题: 让我们把方阵的美定义为最小正整数,不会出现在这个矩阵中。例如,如果平方矩阵2x2包含数字1、2、4和6,则该矩阵的美丽度将等于3 您将得到一个称为“数字”的正整数平方矩阵,以及一个整数“大小”,该整数将数字等分。长度。您的任务是按以下方式排列矩阵元素: 将矩阵拆分为不重叠的“大小x大小”子矩阵 将所有子矩阵按美丽的升序排列,然后将它们放回矩阵中 子矩阵的顺序应该是从左到右,从上到下
- 将矩阵拆分为不重叠的“大小x大小”子矩阵李>
- 将所有子矩阵按美丽的升序排列,然后将它们放回矩阵中
C
版本中,我使用一个类子矩阵来捆绑子矩阵的功能:
using System;
using System.Collections.Generic;
using System.Linq;
namespace akMatrixBeauty
{
class SubMatrix : IComparable<SubMatrix>
{
private int[,] matrix; // main matrix, avoids some copy operations
private int row0; // top row
private int col0; // left column
private int size; // sub-matrix dimension
private int beauty; // calculated beauty
private int order; // iteration index of sub_matrix
// construct submatrix
public SubMatrix(int[,] matrix, int row0, int col0, int size)
{
this.matrix = matrix;
this.row0 = row0;
this.col0 = col0;
this.size = size;
beauty = ComputeBeauty();
order = row0 * size + col0;
}
// indexer: return element of sub-matrix from the main matrix
public int this[int row, int col]
{
get { return matrix[row0 + row, col0 + col]; }
}
// compare against another sub-matrix for sorting
public int CompareTo(SubMatrix other)
{
int delta = beauty.CompareTo(other.beauty);
// resolve tie, if needed
return (delta != 0) ? delta : order.CompareTo(other.order);
}
// this comparator demonstrates some Linq wizardry
// might be useful for interviews ...
private int ComputeBeauty()
{
// iterate through sub-matrix to collect all elements row col
var elements = Enumerable.Range(0, size*size).Select(i => this[i/size, i %size]);
// return the smallest positive integer not contained
return Enumerable.Range(1, elements.Max()).Where(e => !elements.Contains(e)).Min();
}
}
}
爪哇:
int[][]美丽矩阵(int[][]m,int size){
Map Map=newtreemap();
对于(int i=0;iAm i)或这与矩阵无关?只需在数据上循环一次,以获得(submatrixIndex,submatrixBeauty)
对,然后进行常规(稳定)排序?您在尝试解决此问题时遇到了什么困难?
using System;
using System.Collections.Generic;
using System.Linq;
namespace akMatrixBeauty
{
class SubMatrix : IComparable<SubMatrix>
{
private int[,] matrix; // main matrix, avoids some copy operations
private int row0; // top row
private int col0; // left column
private int size; // sub-matrix dimension
private int beauty; // calculated beauty
private int order; // iteration index of sub_matrix
// construct submatrix
public SubMatrix(int[,] matrix, int row0, int col0, int size)
{
this.matrix = matrix;
this.row0 = row0;
this.col0 = col0;
this.size = size;
beauty = ComputeBeauty();
order = row0 * size + col0;
}
// indexer: return element of sub-matrix from the main matrix
public int this[int row, int col]
{
get { return matrix[row0 + row, col0 + col]; }
}
// compare against another sub-matrix for sorting
public int CompareTo(SubMatrix other)
{
int delta = beauty.CompareTo(other.beauty);
// resolve tie, if needed
return (delta != 0) ? delta : order.CompareTo(other.order);
}
// this comparator demonstrates some Linq wizardry
// might be useful for interviews ...
private int ComputeBeauty()
{
// iterate through sub-matrix to collect all elements row col
var elements = Enumerable.Range(0, size*size).Select(i => this[i/size, i %size]);
// return the smallest positive integer not contained
return Enumerable.Range(1, elements.Max()).Where(e => !elements.Contains(e)).Min();
}
}
}
using System;
using System.Collections.Generic;
namespace akMatrixBeauty
{
class Program
{
static void Main(string[] args)
{
// initialize
var matrix = new int[,]
{
{ 1, 2, 3, 1, 2, 3, 1, 2, 3},
{ 4, 5, 6, 4, 5, 6, 4, 5, 6},
{ 7, 8,10, 7, 9,10, 8, 9,10},
{ 1, 2, 3, 1, 2, 3, 1, 2, 3},
{ 4, 5, 7, 4, 6, 7, 5, 6, 7},
{ 8, 9,10, 8, 9,10, 8, 9,10},
{ 1, 2, 4, 1, 3, 4, 2, 3, 4},
{ 5, 6, 7, 5, 6, 7, 5, 6, 7},
{ 8, 9,10, 8, 9,10, 8, 9,10}
};
const int size = 3;
var subMatrices = new List<SubMatrix>();
int rows = matrix.GetLength(0);
int cols = matrix.GetLength(1);
for (int row = 0; row < rows; row += size)
for (int col = 0; col < cols; col += size)
{
var subMatrix = new SubMatrix(matrix, row, col, size);
subMatrices.Add(subMatrix);
}
ShowMatrix(subMatrices, size, rows, cols);
// sort in ascending order of beauty and original position
// cf. SubMatrix.CompareTo()
subMatrices.Sort();
ShowMatrix(subMatrices, size, cols, rows);
Out("");
}
private static void ShowMatrix(List<SubMatrix> subMatrices, int size, int rows, int cols)
{
Out("");
for (int row = 0; row < rows; row++)
{
string s = "";
for (int col = 0; col < cols; col++)
{
// combination of row and col determines the position of the sub-matrix
var subMatrix = subMatrices[(row / size) * (cols / size) + (col / size)];
s += $"{subMatrix[row % size, col % size], 3}";
if (((col + 1) % size == 0) && (col + 1 != cols))
{
s += " | "; // column separator
}
}
Out(s);
if (((row + 1) % size == 0) && (row + 1 != rows))
{
// row separator
s = " " + new string('-', 3 * (cols + cols / size - 1));
Out(s);
}
}
Out("");
}
private static void Out(string s)
{
Console.WriteLine(s);
}
}
}
int[][] beautifyMatrix(int[][] m, int size) {
Map<Integer, List<int[][]>> map = new TreeMap<>();
for (int i=0; i<m.length; i+=size) {
for (int j=0; j<m.length; j+=size) {
fillMap(m, i, j, map, size);
}
}
int [][] res = new int[m.length][m.length];
int i = 0;
int j = 0;
for (Map.Entry<Integer, List<int[][]>> entry : map.entrySet()) {
for (int[][] sub : entry.getValue()) {
for (int k=0; k<sub.length; k++) {
for (int l=0; l<sub.length; l++) {
res[k+i][j+l] = sub[k][l];
}
}
}
if (j < m.length) {
j += size;
}
else {
i += size;
j = 0;
}
}
return res;
}
private void fillMap(int [][] m, int i, int j, Map<Integer, List<int[][]>> map, int size) {
int [][] subM = new int[size][size];
int row = 0;
int col = 0;
for (int k=i; k<size; k++) {
for (int l=j; l<size; l++) {
subM[row][col++] = m[k][j];
}
row++;
}
int beauty = getBeauty(subM, size);
List<int[][]> subs = map.getOrDefault(beauty, new ArrayList<>());
subs.add(subM);
map.put(beauty, subs);
}
private int getBeauty(int[][] m, int size) {
int total = (size * size+1)/2;
for (int i=0; i<m.length; i++) {
for (int j=0; j<m.length; j++) {
total -= m[i][j];
}
}
return total;
}