📜  最大化矩阵的第K个列的总和

📅  最后修改于: 2021-04-23 18:35:05             🧑  作者: Mango

给定两个整数NK,该任务是最大化K的总和在[1,N 2]的范围内包括元件的N * N行方向排序的矩阵的第n列。

例子:

方法:这里的想法是先用范围[1,N *(K – 1)]中的值填充小于矩阵的K列的索引,然后填充大于或等于列的所有元素如下图所示k列由值从区间[+ 1,N 2 N *(1 K)]。

请按照以下步骤解决问题:

  1. [1,N *(K – 1)]范围内的值填充小于K的矩阵的所有列
  2. 然后,用[N *(K – 1)+ 1,N * N]范围内的值填充大于或等于K的矩阵列。
  3. 最后,打印矩阵。

下面是上述方法的实现:

C++
// C++ program to implement
// the above approach
 
#include 
using namespace std;
 
// Function to maximize the Kth column sum
int** findMatrix(int N, int K)
{
 
    // Store all the elements of the
    // resultant matrix of size N*N
    int** mat = (int**)malloc(
        N * sizeof(int*));
 
    for (int i = 0; i < N; ++i) {
        mat[i] = (int*)malloc(
            N * sizeof(int));
    }
 
    // Store value of each
    // elements of the matrix
    int element = 1;
 
    // Fill all the columns < K
    for (int i = 0; i < N; ++i) {
 
        for (int j = 0; j < K - 1; ++j) {
            mat[i][j] = element++;
        }
    }
 
    // Fill all the columns >= K
    for (int i = 0; i < N; ++i) {
 
        for (int j = K - 1; j < N; ++j) {
            mat[i][j] = element++;
        }
    }
 
    return mat;
}
 
// Function to print the matrix
void printMatrix(int** mat, int N)
{
 
    for (int i = 0; i < N; ++i) {
        for (int j = 0; j < N; ++j) {
            cout << mat[i][j] << " ";
        }
        cout << endl;
    }
}
 
// Driver Code
int main()
{
 
    int N = 3, K = 2;
    int** mat = findMatrix(N, K);
 
    printMatrix(mat, N);
}


Java
// Java program to implement
// the above approach
class GFG{
 
// Function to maximize the Kth column sum
static int [][]findMatrix(int N, int K)
{
 
    // Store all the elements of the
    // resultant matrix of size N*N
    int [][]mat = new int[N][N];
 
    // Store value of each
    // elements of the matrix
    int element = 1;
 
    // Fill all the columns < K
    for(int i = 0; i < N; ++i)
    {
        for(int j = 0; j < K - 1; ++j)
        {
            mat[i][j] = element++;
        }
    }
 
    // Fill all the columns >= K
    for(int i = 0; i < N; ++i)
    {
        for(int j = K - 1; j < N; ++j)
        {
            mat[i][j] = element++;
        }
    }
    return mat;
}
 
// Function to print the matrix
static void printMatrix(int [][]mat, int N)
{
    for(int i = 0; i < N; ++i)
    {
        for(int j = 0; j < N; ++j)
        {
            System.out.print(mat[i][j] + " ");
        }
        System.out.println();
    }
}
 
// Driver Code
public static void main(String[] args)
{
    int N = 3, K = 2;
    int [][]mat = findMatrix(N, K);
 
    printMatrix(mat, N);
}
}
 
// This code is contributed by Amit Katiyar


Python3
# Python3 program to implement
# the above approach
 
# Function to maximize the Kth
# column sum
def findMatrix(N, K):
     
    # Store all the elements of the
    # resultant matrix of size N*N
    mat = [[0 for i in range(N)]
              for j in range(N)];
 
    # Store value of each
    # elements of the matrix
    element = 0;
 
    # Fill all the columns < K
    for i in range(0, N):
        for j in range(0, K - 1):
            element += 1;
            mat[i][j] = element;
 
    # Fill all the columns >= K
    for i in range(0, N):
        for j in range(K - 1, N):
            element += 1;
            mat[i][j] = element;
 
    return mat;
 
# Function to prthe matrix
def printMatrix(mat, N):
     
    for i in range(0, N):
        for j in range(0, N):
            print(mat[i][j], end = " ");
 
        print();
 
# Driver Code
if __name__ == '__main__':
     
    N = 3; K = 2;
    mat = findMatrix(N, K);
 
    printMatrix(mat, N);
 
# This code is contributed by Amit Katiyar


C#
// C# program to implement
// the above approach
using System;
 
class GFG{
 
// Function to maximize the Kth column sum
static int [,]findMatrix(int N, int K)
{
 
    // Store all the elements of the
    // resultant matrix of size N*N
    int [,]mat = new int[N, N];
 
    // Store value of each
    // elements of the matrix
    int element = 1;
 
    // Fill all the columns < K
    for(int i = 0; i < N; ++i)
    {
        for(int j = 0; j < K - 1; ++j)
        {
            mat[i, j] = element++;
        }
    }
 
    // Fill all the columns >= K
    for(int i = 0; i < N; ++i)
    {
        for(int j = K - 1; j < N; ++j)
        {
            mat[i, j] = element++;
        }
    }
    return mat;
}
 
// Function to print the matrix
static void printMatrix(int [,]mat, int N)
{
    for(int i = 0; i < N; ++i)
    {
        for(int j = 0; j < N; ++j)
        {
            Console.Write(mat[i, j] + " ");
        }
        Console.WriteLine();
    }
}
 
// Driver Code
public static void Main(String[] args)
{
    int N = 3, K = 2;
    int [,]mat = findMatrix(N, K);
 
    printMatrix(mat, N);
}
}
 
// This code is contributed by Amit Katiyar


Javascript


输出:
1 4 5 
2 6 7 
3 8 9

时间复杂度: O(N 2 )
辅助空间: O(N 2 )