📜  使用给定的操作将矩阵压缩为单个数字

📅  最后修改于: 2021-05-14 08:37:23             🧑  作者: Mango

给定维度为M * N的矩阵mat [] [] ,任务是首先对其进行压缩以获得一个数组,然后使用以下操作再次对其进行压缩以获得一个整数:

  • 压缩矩阵时,其值的二进制表示形式将被压缩。
  • 因此,考虑每个比特,并且如果一个比特的位置具有S个置位比特和NS个非置位比特,则如果S> NS ,则将该比特设置为该位置,否则置为未置位。
  • 压缩每列以将矩阵转换为数组,然后将该数组进一步压缩为单个数字。

例子:

方法:想法是计算每个位置的设置位数。请按照以下步骤解决问题:

  • 遍历矩阵每一列的每个元素,并将每一列压缩为单个数字。
  • 计算每个位置的设置位数。
  • 设置位置位数超过未设置位数的位置。
  • 在决定是否为每个位置设置该位后,计算该值。
  • 获取数组后,请执行相同的步骤以获取所需的整数。

下面是上述方法的实现:

C++
// c++ Program for the above approach
#include
using namespace std;
 
  // Function to compress an
  // array to a single number
  vector append(vector arr, int x)
  {
     
    // create a new ArrayList
    arr.push_back(x);
    return arr;
  }
   
  int compress(vector arr)
  {
 
    // Stores the required integer
    int ans = 0;
    int getBit = 1;
 
    // Checking for each position
    for (int i = 0; i < 32; i++)
    {
      int S = 0;
      int NS = 0;
      for (int j = 0; j < arr.size(); j++)
      {
 
        // Count set and unset bit
        int temp = getBit & arr[j];
        if (temp == 1) {
          S += 1;
        }
        else {
          NS += 1;
        }
 
        // If count of set bits exceeds
        // count of unset bits
        if (S > NS) {
 
          // Add value of set bits to ans
          ans += pow(2, i);
        }
        getBit <<= 1;
      }
    }
 
    return ans;
  }
 
  // Function to compress
  // matrix to a single number
 int getResult(vector> mat)
  {
 
    // Stores compressed array
    vector compressedArr;
    int len = mat.size();
    int len2 = mat[0].size();
    for (int i = 0; i < len; i++)
    {
      vector col;
 
      for (int j = 0; j < len2; j++)
      {
        col = append(col, mat[j][i]);
      }
       
      // Compress all columns
      // to a single number
      compressedArr = append(compressedArr, compress(col));
    }
    return compress(compressedArr);
  }
 
  // Driver Code
  int main()
  {
    vector> mat{{3, 2, 4 },{5, 6, 1},{8, 1, 3}};
    cout<<(getResult(mat));
  }
 
// THIS CODE IS CONTRIBUTED BY SURENDRA_GANGWAR.


Java
// Java Program for the above approach
import java.io.*;
import java.lang.*;
import java.lang.Math;
import java.util.*;
class GFG {
 
  // Function to compress an
  // array to a single number
  static Integer[] append(Integer arr[], int x)
  {
    // create a new ArrayList
    List arrlist
      = new ArrayList(Arrays.asList(arr));
 
    // Add the new element
    arrlist.add(x);
 
    // Convert the Arraylist to array
    arr = arrlist.toArray(arr);
 
    // return the array
    return arr;
  }
  static int compress(Integer[] arr)
  {
 
    // Stores the required integer
    int ans = 0;
    int getBit = 1;
 
    // Checking for each position
    for (int i = 0; i < 32; i++) {
 
      int S = 0;
      int NS = 0;
 
      for (int j = 0; j < arr.length; j++) {
 
        // Count set and unset bit
        int and = getBit & arr[j];
        if (and == 1) {
          S += 1;
        }
        else {
          NS += 1;
        }
 
        // If count of set bits exceeds
        // count of unset bits
        if (S > NS) {
 
          // Add value of set bits to ans
          ans += Math.pow(2, i);
        }
        getBit <<= 1;
      }
    }
 
    return ans;
  }
 
  // Function to compress
  // matrix to a single number
  static int getResult(Integer[][] mat)
  {
 
    // Stores compressed array
    Integer[] compressedArr = {};
    int len = mat.length;
    int len2 = mat[0].length;
    for (int i = 0; i < len; i++)
    {
      Integer[] col = {};
 
      for (int j = 0; j < len2; j++)
      {
        col = append(col, mat[j][i]);
      }
       
      // Compress all columns
      // to a single number
      compressedArr
        = append(compressedArr, compress(col));
    }
    return compress(compressedArr);
  }
 
  // Driver Code
  public static void main(String[] args)
  {
    Integer[][] mat = { { 3, 2, 4 },
                       { 5, 6, 1 },
                       { 8, 1, 3 } };
    System.out.println(getResult(mat));
  }
}
 
// This code is contibuted by rohitsingh07052.


Python3
# Python Program for the above approach
 
# Function to compress an
# array to a single number
def compress(arr):
 
  # Stores the required integer
  ans = 0
  getBit = 1
 
  # Checking for each position
  for i in range(32):
 
    S = 0
    NS = 0
 
    for j in arr:
 
      # Count set and unset bits
      if getBit&j:
        S += 1
      else:
        NS += 1
 
    # If count of set bits exceeds
    # count of unset bits
    if S > NS:
  
      # Add value of set bits to ans
      ans += 2**i
    getBit <<= 1
 
  return ans
 
# Function to compress
# matrix to a single number
def getResult(mat):
 
  # Stores compressed array
  compressedArr = []
 
  for i in range(len(mat)):
    col = []
    for j in range(len(mat[0])):
      col.append(mat[j][i])
 
    # Compress all columns
    # to a single number 
    compressedArr.append(compress(col))
 
  return compress(compressedArr)
 
# Driver Code
mat = [ [ 3, 2, 4], [5, 6, 1], [8, 1, 3] ]
 
print( getResult(mat) )


C#
// C# program for the above approach
using System;
using System.Collections.Generic;
 
class GFG{
 
// Function to compress an
// array to a single number
static int[] append(int []arr, int x)
{
     
    // create a new List
    List arrlist = new List(arr);
     
    // Add the new element
    arrlist.Add(x);
     
    // Convert the Arraylist to array
    arr = arrlist.ToArray();
     
    // return the array
    return arr;
}
 
static int compress(int[] arr)
{
 
    // Stores the required integer
    int ans = 0;
    int getBit = 1;
     
    // Checking for each position
    for(int i = 0; i < 32; i++)
    {
     
        int S = 0;
        int NS = 0;
         
        for(int j = 0; j < arr.Length; j++)
        {
 
            // Count set and unset bit
            int and = getBit & arr[j];
            if (and == 1)
            {
                S += 1;
            }
            else
            {
                NS += 1;
            }
             
            // If count of set bits exceeds
            // count of unset bits
            if (S > NS)
            {
                 
                // Add value of set bits to ans
                ans += (int)Math.Pow(2, i);
            }
            getBit <<= 1;
        }
    }
     
    return ans;
}
 
// Function to compress
// matrix to a single number
static int getResult(int[,] mat)
{
     
    // Stores compressed array
    int[] compressedArr = {};
    int len = mat.GetLength(0);
    int len2 = mat.GetLength(1);
     
    for(int i = 0; i < len; i++)
    {
        int[] col = {};
         
        for (int j = 0; j < len2; j++)
        {
            col = append(col, mat[j,i]);
        }
         
        // Compress all columns
        // to a single number
        compressedArr = append(compressedArr,
                               compress(col));
    }
    return compress(compressedArr);
}
 
// Driver Code
public static void Main(String[] args)
{
    int[,] mat = { { 3, 2, 4 },
                   { 5, 6, 1 },
                   { 8, 1, 3 } };
                    
    Console.WriteLine(getResult(mat));
}
}
 
// This code is contributed by shikhasingrajput


输出:
1

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