📜  最小化相邻行交换的计数,以将给定的矩阵转换为下三角矩阵

📅  最后修改于: 2021-04-24 21:26:49             🧑  作者: Mango

给定大小为N×N的矩阵mat [] [] ,任务是最小化相邻行交换的计数,以将给定矩阵转换为较低的三角矩阵。如果无法将给定的矩阵转换为较低的三角矩阵,则打印-1。
注意:下三角矩阵在主对角线上的所有索引处都包含0。

例子:

方法:可以使用贪婪技术解决此问题。首先找到每行中存在的零计数并将其存储在整数数组中的想法。然后,根据以降序为0的数目,计算对数组进行排序所需的最小相邻交换数。请按照以下步骤解决问题:

  1. 初始化一个数组,例如cntZero [],以存储每行中存在的0的计数。
  2. 对于i行,从第(i + 1)索引遍历cntZero []数组并找到第一个索引,说第一个,其中ctnZero [First]> =(N – i -1)
  3. 将所有相邻元素从cntZero []数组的i索引交换到第一个索引,并增加计数。
  4. 最后,返回将给定矩阵转换为下三角矩阵所需的相邻交换计数。

下面是上述方法的实现:

C++
// C++ program to implement
// the above approach
 
#include 
using namespace std;
 
// Function to count the minimum
// number of  adjacent swaps
int minAdjSwaps(vector >& mat)
{
    // Stores the size of
    // the given matrix
    int N = mat.size();
 
    // Stores the count of zero
    // at the end of each row
    vector cntZero(N, 0);
 
    // Traverse the given matrix
    for (int i = 0; i < N; i++) {
 
        // Count of 0s at the end
        // of the ith row
        for (int j = N - 1;
             j >= 0 && mat[i][j] == 0;
             j--) {
            cntZero[i]++;
        }
    }
 
    // Stores the count of swaps
    int cntSwaps = 0;
 
    // Traverse the cntZero array
    for (int i = 0; i < N; i++) {
 
        // If count of zero in the
        // i-th row < (N - i - 1)
        if (cntZero[i]
            < (N - i - 1)) {
 
            // Stores the index of the row
            // where count of zero > (N-i-1)
            int First = i;
            while (First < N
                   && cntZero[First]
                          < (N - i - 1)) {
                First++;
            }
 
            // If no row found that
            // satisfy the condition
            if (First == N) {
                return -1;
            }
 
            // Swap the adjacent row
            while (First > i) {
                swap(cntZero[First],
                     cntZero[First - 1]);
                First--;
                cntSwaps++;
            }
        }
    }
    return cntSwaps;
}
 
// Driver Code
int main()
{
    vector > mat
        = { { 0, 0, 2 },
            { 3, 1, 0 },
            { 4, 0, 0 } };
    cout << minAdjSwaps(mat);
}


Java
// Java program to implement
// the above approach
import java.util.*;
 
class GFG{
 
// Function to count the minimum
// number of  adjacent swaps
static int minAdjSwaps(int [][]mat)
{
     
    // Stores the size of
    // the given matrix
    int N = mat.length;
 
    // Stores the count of zero
    // at the end of each row
    int []cntZero = new int[N];
 
    // Traverse the given matrix
    for(int i = 0; i < N; i++)
    {
         
        // Count of 0s at the end
        // of the ith row
        for(int j = N - 1;
                j >= 0 && mat[i][j] == 0;
                j--)
        {
            cntZero[i]++;
        }
    }
 
    // Stores the count of swaps
    int cntSwaps = 0;
 
    // Traverse the cntZero array
    for(int i = 0; i < N; i++)
    {
         
        // If count of zero in the
        // i-th row < (N - i - 1)
        if (cntZero[i] < (N - i - 1))
        {
             
            // Stores the index of the row
            // where count of zero > (N-i-1)
            int First = i;
            while (First < N && cntZero[First] <
                  (N - i - 1))
            {
                First++;
            }
 
            // If no row found that
            // satisfy the condition
            if (First == N)
            {
                return -1;
            }
 
            // Swap the adjacent row
            while (First > i)
            {
                cntZero = swap(cntZero, First,
                               First - 1);
                First--;
                cntSwaps++;
            }
        }
    }
    return cntSwaps;
}
 
static int[] swap(int []arr, int i, int j)
{
    int temp = arr[i];
    arr[i] = arr[j];
    arr[j] = temp;
    return arr;
}
 
// Driver Code
public static void main(String[] args)
{
    int [][]mat = { { 0, 0, 2 },
                    { 3, 1, 0 },
                    { 4, 0, 0 } };
                     
    System.out.print(minAdjSwaps(mat));
}
}
 
// This code is contributed by Amit Katiyar


Python3
# Python3 program to implement
# the above approach
 
# Function to count the minimum
# number of  adjacent swaps
def minAdjSwaps(mat):
     
    # Stores the size of
    # the given matrix
    N = len(mat)
 
    # Stores the count of zero
    # at the end of each row
    cntZero = [0] * (N)
 
    # Traverse the given matrix
    for i in range(N):
 
        # Count of 0s at the end
        # of the ith row
        for j in range(N - 1, -1, -1):
            if mat[i][j] != 0:
                break
             
            cntZero[i] += 1
 
    # Stores the count of swaps
    cntSwaps = 0
 
    # Traverse the cntZero array
    for i in range(N):
 
        # If count of zero in the
        # i-th row < (N - i - 1)
        if (cntZero[i] < (N - i - 1)):
 
            # Stores the index of the row
            # where count of zero > (N-i-1)
            First = i
             
            while (First < N and
           cntZero[First] < (N - i - 1)):
                First += 1
 
            # If no row found that
            # satisfy the condition
            if (First == N):
                return -1
 
            # Swap the adjacent row
            while (First > i):
                cntZero[First] = cntZero[First - 1]
                cntZero[First - 1] = cntZero[First]
                 
                First -= 1
                cntSwaps += 1
                 
    return cntSwaps
 
# Driver Code
if __name__ == '__main__':
     
    mat = [ [ 0, 0, 2 ],
            [ 3, 1, 0 ],
            [ 4, 0, 0 ] ]
             
    print(minAdjSwaps(mat))
 
# This code is contributed by mohit kumar 29


C#
// C# program to implement
// the above approach
using System;
class GFG{
 
// Function to count the minimum
// number of  adjacent swaps
static int minAdjSwaps(int [,]mat)
{
  // Stores the size of
  // the given matrix
  int N = mat.GetLength(0);
 
  // Stores the count of zero
  // at the end of each row
  int []cntZero = new int[N];
 
  // Traverse the given matrix
  for(int i = 0; i < N; i++)
  {
    // Count of 0s at the end
    // of the ith row
    for(int j = N - 1;
            j >= 0 && mat[i, j] == 0;
            j--)
    {
      cntZero[i]++;
    }
  }
 
  // Stores the count of swaps
  int cntSwaps = 0;
 
  // Traverse the cntZero array
  for(int i = 0; i < N; i++)
  {
    // If count of zero in the
    // i-th row < (N - i - 1)
    if (cntZero[i] < (N - i - 1))
    {
      // Stores the index of the row
      // where count of zero > (N-i-1)
      int First = i;
       
      while (First < N &&
             cntZero[First] < (N - i - 1))
      {
        First++;
      }
 
      // If no row found that
      // satisfy the condition
      if (First == N)
      {
        return -1;
      }
 
      // Swap the adjacent row
      while (First > i)
      {
        cntZero = swap(cntZero,
                       First, First - 1);
        First--;
        cntSwaps++;
      }
    }
  }
  return cntSwaps;
}
 
static int[] swap(int []arr,
                  int i, int j)
{
  int temp = arr[i];
  arr[i] = arr[j];
  arr[j] = temp;
  return arr;
}
 
// Driver Code
public static void Main(String[] args)
{
  int [,]mat = {{0, 0, 2},
                {3, 1, 0},
                {4, 0, 0}};
  Console.Write(minAdjSwaps(mat));
}
}
 
// This code is contributed by 29AjayKumar


输出
3

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