logo

معرفة ما إذا كانت المصفوفة المعطاة هي Toeplitz أم لا

نظرا لمصفوفة مربعة جنبا إلى جنب مع[][] من النظام ن مهمتك هي التحقق مما إذا كانت مصفوفة Toeplitz.

ملحوظة: مصفوفة توبليتز - وتسمى أيضًا مصفوفة الثبات القطري - هي مصفوفة تكون فيها عناصر كل قطر تنازلي متماثلة من اليسار إلى اليمين. بشكل مكافئ لأي حصيرة دخول [i] [j] فهي نفس mat [i-1] [j-1] أو mat [i-2] [j-2] و son on.



أمثلة:

مدخل: مع[][] = [ [6 7 8]
[4 6 7]
[1 4 6] ]
الإخراج: نعم
توضيح: جميع أقطار المصفوفة المعطاة هي [6 6 6] [7 7] [8] [4 4] [1]. لكل قطري حيث أن جميع العناصر متماثلة، تكون المصفوفة المعطاة هي Toeplitz Matrix.

مدخل: مع[][] = [ [6 3 8]
[4 9 7]
[1 4 6] ]
الإخراج: لا
توضيح: العناصر القطرية الأساسية للمصفوفة المعطاة هي [6 9 6]. وبما أن العناصر القطرية ليست هي نفسها، فإن المصفوفة المعطاة ليست مصفوفة توبليتز.



[النهج المتوقع - 1] - التحقق من كل قطري - O(n * n) الوقت وO(1) الفضاء

تتمثل الفكرة في اجتياز كل قطري مائل للأسفل في المصفوفة باستخدام كل عنصر في الصف الأول وكل عنصر في العمود الأول كنقطة بداية والتحقق من أن كل عنصر على طول هذا القطر يطابق القيمة الموجودة في رأسه.

اتبع الخطوات المذكورة أدناه:

  • يتركn = mat.size()وm = mat[0].size().
  • لكل عمود فهرسiمن0لm - 1يتصلcheckDiagonal(mat 0 i); إذا أعادت كاذبة فارجع على الفور كاذبة منisToeplitz.
  • لكل صف فهرسiمن0لn - 1يتصلcheckDiagonal(mat i 0); إذا أعادت كاذبة فارجع على الفور كاذبة منisToeplitz.
  • إذا كان كل يدعو إلىcheckDiagonalتنجح العودة الحقيقية.
  • فيcheckDiagonal(mat x y)يقارنmat[i][j]لmat[x][y]لكلi = x+1 j = y+1بينماi < n && j < m; قم بإرجاع خطأ عند عدم التطابق الأول وإلا قم بإرجاع صحيح بعد الوصول إلى الحافة.

أدناه يعطى تطبيق:



فرز قائمة صفيف جافا
C++
#include    using namespace std; // function to check if diagonal elements are same bool checkDiagonal(vector<vector<int>> &mat int x int y) {  int n = mat.size() m = mat[0].size();  for(int i = x + 1 j = y + 1;   i < n && j < m; i++ j++) {  if(mat[i][j] != mat[x][y])  return false;  }  return true; } // Function to check whether given // matrix is toeplitz matrix or not bool isToeplitz(vector<vector<int>> &mat) {  int n = mat.size() m = mat[0].size();  // check each descending diagonal starting from  // first row and first column of the matrix  for(int i = 0; i < m; i++)  if(!checkDiagonal(mat 0 i))  return false;  for(int i = 0; i < n; i++)   if(!checkDiagonal(mat i 0))  return false;    // if all diagonals are same return true  return true; } int main() {  vector<vector<int>> mat = {  {6 7 8}  {4 6 7}  {1 4 6}  };  if(isToeplitz(mat)) {  cout << 'Yes';  } else {  cout << 'No';  }  return 0; } 
Java
import java.util.*; class GfG {  // function to check if diagonal elements are same  static boolean checkDiagonal(List<List<Integer>> mat int x int y) {  int n = mat.size() m = mat.get(0).size();  for(int i = x + 1 j = y + 1;   i < n && j < m; i++ j++) {  if(!mat.get(i).get(j).equals(mat.get(x).get(y)))  return false;  }  return true;  }  // Function to check whether given  // matrix is toeplitz matrix or not  static boolean isToeplitz(List<List<Integer>> mat) {  int n = mat.size() m = mat.get(0).size();  // check each descending diagonal starting from  // first row and first column of the matrix  for(int i = 0; i < m; i++)  if(!checkDiagonal(mat 0 i))  return false;  for(int i = 0; i < n; i++)   if(!checkDiagonal(mat i 0))  return false;  // if all diagonals are same return true  return true;  }  public static void main(String[] args) {  List<List<Integer>> mat = Arrays.asList(  Arrays.asList(6 7 8)  Arrays.asList(4 6 7)  Arrays.asList(1 4 6)  );  if(isToeplitz(mat)) {  System.out.println('Yes');  } else {  System.out.println('No');  }  } } 
Python
# function to check if diagonal elements are same def checkDiagonal(mat x y): n m = len(mat) len(mat[0]) i j = x + 1 y + 1 while i < n and j < m: if mat[i][j] != mat[x][y]: return False i += 1 j += 1 return True # Function to check whether given # matrix is toeplitz matrix or not def isToeplitz(mat): n m = len(mat) len(mat[0]) # check each descending diagonal starting from # first row and first column of the matrix for i in range(m): if not checkDiagonal(mat 0 i): return False for i in range(n): if not checkDiagonal(mat i 0): return False # if all diagonals are same return true return True mat = [ [6 7 8] [4 6 7] [1 4 6] ] if isToeplitz(mat): print('Yes') else: print('No') 
C#
using System; using System.Collections.Generic; class GfG {  // function to check if diagonal elements are same  static bool checkDiagonal(List<List<int>> mat int x int y) {  int n = mat.Count m = mat[0].Count;  for(int i = x + 1 j = y + 1;   i < n && j < m; i++ j++) {  if(mat[i][j] != mat[x][y])  return false;  }  return true;  }  // Function to check whether given  // matrix is toeplitz matrix or not  static bool isToeplitz(List<List<int>> mat) {  int n = mat.Count m = mat[0].Count;  // check each descending diagonal starting from  // first row and first column of the matrix  for(int i = 0; i < m; i++)  if(!checkDiagonal(mat 0 i))  return false;  for(int i = 0; i < n; i++)   if(!checkDiagonal(mat i 0))  return false;  // if all diagonals are same return true  return true;  }  static void Main() {  var mat = new List<List<int>> {  new List<int> {6 7 8}  new List<int> {4 6 7}  new List<int> {1 4 6}  };  if(isToeplitz(mat)) {  Console.WriteLine('Yes');  } else {  Console.WriteLine('No');  }  } } 
JavaScript
// function to check if diagonal elements are same function checkDiagonal(mat x y) {  let n = mat.length m = mat[0].length;  for(let i = x + 1 j = y + 1;   i < n && j < m; i++ j++) {  if(mat[i][j] !== mat[x][y])  return false;  }  return true; } // Function to check whether given // matrix is toeplitz matrix or not function isToeplitz(mat) {  let n = mat.length m = mat[0].length;  // check each descending diagonal starting from  // first row and first column of the matrix  for(let i = 0; i < m; i++)  if(!checkDiagonal(mat 0 i))  return false;  for(let i = 0; i < n; i++)   if(!checkDiagonal(mat i 0))  return false;  // if all diagonals are same return true  return true; } let mat = [  [6 7 8]  [4 6 7]  [1 4 6] ]; if(isToeplitz(mat)) {  console.log('Yes'); } else {  console.log('No'); } 

الإخراج
Yes

[النهج المتوقع - 2] - التحقق قطريًا فوق العنصر - O(n * n) الوقت وO(1) الفضاء

تتمثل الفكرة في مسح كل خلية من الصف الثاني والعمود الثاني فصاعدًا ومقارنة كل قيمة بجارتها العلوية اليسرى. إذا اختلف أي عنصر عن العنصر الموجود فوقه قطريًا، فقد وجدت انتهاكًا لخاصية Toeplitz ويمكنك التوقف فورًا؛ إذا قمت بتمرير المصفوفة بأكملها دون عدم تطابق، فسيكون كل قطري ثابتًا.

اتبع الخطوات المذكورة أدناه:

  • يتركn = mat.size()وm = mat[0].size().
  • أعادiمن 1 إلىn - 1وضمن ذلكjمن 1 إلىm - 1.
  • لوmat[i][j] != mat[i - 1][j - 1]في أي لحظة العودةfalse.
  • بمجرد التحقق من جميع الأزواج مع عدم وجود أي عدم تطابقtrue.

أدناه يعطى تطبيق:

C++
#include    using namespace std; // Function to check whether given // matrix is toeplitz matrix or not bool isToeplitz(vector<vector<int>> &mat) {  int n = mat.size() m = mat[0].size();  // check diagonally above element of  // each element in the matrix  for(int i = 1; i < n; i++) {  for(int j = 1; j < m; j++) {  if(mat[i][j] != mat[i - 1][j - 1])  return false;  }  }    // if all diagonals are same return true  return true; } int main() {  vector<vector<int>> mat = {  {6 7 8}  {4 6 7}  {1 4 6}  };  if(isToeplitz(mat)) {  cout << 'Yes';  } else {  cout << 'No';  }  return 0; } 
Java
import java.util.*; class GfG {  // Function to check whether given  // matrix is toeplitz matrix or not  static boolean isToeplitz(List<List<Integer>> mat) {  int n = mat.size() m = mat.get(0).size();  // check diagonally above element of  // each element in the matrix  for(int i = 1; i < n; i++) {  for(int j = 1; j < m; j++) {  if(mat.get(i).get(j) != mat.get(i - 1).get(j - 1))  return false;  }  }    // if all diagonals are same return true  return true;  }  public static void main(String[] args) {  List<List<Integer>> mat = Arrays.asList(  Arrays.asList(6 7 8)  Arrays.asList(4 6 7)  Arrays.asList(1 4 6)  );  if(isToeplitz(mat)) {  System.out.println('Yes');  } else {  System.out.println('No');  }  } } 
Python
# Function to check whether given # matrix is toeplitz matrix or not def isToeplitz(mat): n m = len(mat) len(mat[0]) # check diagonally above element of # each element in the matrix for i in range(1 n): for j in range(1 m): if mat[i][j] != mat[i - 1][j - 1]: return False # if all diagonals are same return true return True mat = [ [6 7 8] [4 6 7] [1 4 6] ] if isToeplitz(mat): print('Yes') else: print('No') 
C#
using System; using System.Collections.Generic; class GfG {  // Function to check whether given  // matrix is toeplitz matrix or not  static bool isToeplitz(List<List<int>> mat) {  int n = mat.Count m = mat[0].Count;  // check diagonally above element of  // each element in the matrix  for(int i = 1; i < n; i++) {  for(int j = 1; j < m; j++) {  if(mat[i][j] != mat[i - 1][j - 1])  return false;  }  }    // if all diagonals are same return true  return true;  }  static void Main() {  var mat = new List<List<int>> {  new List<int> {6 7 8}  new List<int> {4 6 7}  new List<int> {1 4 6}  };  if(isToeplitz(mat)) {  Console.WriteLine('Yes');  } else {  Console.WriteLine('No');  }  } } 
JavaScript
// Function to check whether given // matrix is toeplitz matrix or not function isToeplitz(mat) {  let n = mat.length m = mat[0].length;  // check diagonally above element of  // each element in the matrix  for(let i = 1; i < n; i++) {  for(let j = 1; j < m; j++) {  if(mat[i][j] !== mat[i - 1][j - 1])  return false;  }  }    // if all diagonals are same return true  return true; } let mat = [  [6 7 8]  [4 6 7]  [1 4 6] ]; if(isToeplitz(mat)) {  console.log('Yes'); } else {  console.log('No'); } 

الإخراج
Yes

[نهج بديل] - استخدام التجزئة - O(n * n) الوقت وO(n) الفضاء

تتمثل الفكرة في تعيين معرف فريد لكل قطري أسفل اليمين (فهرس الصف مطروحًا منه فهرس العمود) واستخدام خريطة التجزئة لتسجيل أول قيمة تمت رؤيتها لهذا القطر. أثناء قيامك بمسح المصفوفة بأكملها، يمكنك حساب هذا المفتاح لكل خلية وإما التحقق من مطابقته للقيمة المخزنة أو تخزينه إذا كان جديدًا. يتيح لك عدم التطابق الفردي إمكانية الإنقاذ باستخدام خطأ؛ وإلا فإنك تستنتج أنه صحيح في النهاية.

اتبع الخطوات المذكورة أدناه:

  • تحديد أبعاد المصفوفة (عدد الصفوف وعدد الأعمدة) منmat.
  • قم بإنشاء خريطة هاشم فارغة mp لتعيين كل مفتاح قطري لقيمته التمثيلية.
  • المشي من خلال كل خلية فيmatمن خلال فهرس الصف الخاص بهiوفهرس العمودj.
  • لكل خلية شكل المفتاح القطري عن طريق الطرحjمنi.
  • لوmpيحمل هذا المفتاح بالفعل ويقارن العنصر الحالي بالقيمة المخزنة؛ إذا اختلفوا يعود كاذبة على الفور.
  • إذا لم يكن المفتاح موجودًا بعدmpسجل العنصر الحالي تحت هذا المفتاح.
  • إذا أكملت الاجتياز دون أي عدم تطابق، فسيتم إرجاع صحيح.

توضيح:

الرسم البياني أدناه يعطي تصورا أفضل لهذه الفكرة. خذ بعين الاعتبار القطر ذو اللون الأصفر. الفرق بين قيمة x وقيمة y لأي مؤشر على هذا القطر هو 2 (2-0 3-1 4-2 5-3). ويمكن ملاحظة الشيء نفسه بالنسبة لجميع أقطار الجسم.

معرفة ما إذا كانت المصفوفة المعطاة هي Toeplitz أم لا

للون الأحمر الفرق القطري هو 3. للون الأخضر الفرق القطري هو 0. للبرتقالي اللون الفرق القطري هو -2 وهكذا...

اللاتكس المشتق جزئيا

أدناه يعطى تطبيق:

C++
#include    using namespace std; // Function to check whether given // matrix is toeplitz matrix or not bool isToeplitz(vector<vector<int>> &mat) {  int n = mat.size() m = mat[0].size();    // HashMap to store keyvalue pairs  unordered_map<int int> mp;    for(int i = 0; i < n; i++) {  for(int j = 0; j < m; j++) {  int key = i - j;    // If key value exists in the hashmap  if (mp[key]) {    // check if the value is same   // as the current element  if (mp[key] != mat[i][j])  return false;  }    // Else we put keyvalue pair in hashmap  else {  mp[i - j] = mat[i][j];  }  }  }  return true; } int main() {  vector<vector<int>> mat = {  {6 7 8}  {4 6 7}  {1 4 6}  };  if(isToeplitz(mat)) {  cout << 'Yes';  } else {  cout << 'No';  }  return 0; } 
Java
// JAVA program to check whether given matrix // is a Toeplitz matrix or not import java.util.*; class GFG {  static boolean isToeplitz(int[][] matrix)  {  // row = number of rows  // col = number of columns  int row = matrix.length;  int col = matrix[0].length;  // HashMap to store keyvalue pairs  HashMap<Integer Integer> map  = new HashMap<Integer Integer>();  for (int i = 0; i < row; i++)   {  for (int j = 0; j < col; j++)   {  int key = i - j;  // if key value exists in the hashmap  if (map.containsKey(key))   {  // we check whether the current value  // stored in this key matches to element  // at current index or not. If not  // return false  if (map.get(key) != matrix[i][j])  return false;  }  // else we put keyvalue pair in hashmap  else {  map.put(i - j matrix[i][j]);  }  }  }  return true;  }  // Driver Code  public static void main(String[] args)  {  int[][] matrix = { { 12 23 -32 }  { -20 12 23 }  { 56 -20 12 }  { 38 56 -20 } };    // Function call  String result = (isToeplitz(matrix)) ? 'Yes' : 'No';  System.out.println(result);  } } 
Python
# Python3 program to check whether given matrix # is a Toeplitz matrix or not def isToeplitz(matrix): # row = number of rows # col = number of columns row = len(matrix) col = len(matrix[0]) # dictionary to store keyvalue pairs map = {} for i in range(row): for j in range(col): key = i-j # if key value exists in the map if (key in map): # we check whether the current value stored # in this key matches to element at current # index or not. If not return false if (map[key] != matrix[i][j]): return False # else we put keyvalue pair in map else: map[key] = matrix[i][j] return True # Driver Code if __name__ == '__main__': matrix = [[12 23 -32] [-20 12 23] [56 -20 12] [38 56 -20]] # Function call if (isToeplitz(matrix)): print('Yes') else: print('No') 
C#
using System; using System.Collections.Generic; class GfG {  // Function to check whether given  // matrix is toeplitz matrix or not  static bool isToeplitz(List<List<int>> mat) {  int n = mat.Count m = mat[0].Count;    // HashMap to store keyvalue pairs  Dictionary<intint> mp = new Dictionary<intint>();    for(int i = 0; i < n; i++) {  for(int j = 0; j < m; j++) {  int key = i - j;    // If key value exists in the hashmap  if (mp.ContainsKey(key)) {    // check if the value is same   // as the current element  if (mp[key] != mat[i][j])  return false;  }    // Else we put keyvalue pair in hashmap  else {  mp[i - j] = mat[i][j];  }  }  }  return true;  }  static void Main() {  var mat = new List<List<int>> {  new List<int> {6 7 8}  new List<int> {4 6 7}  new List<int> {1 4 6}  };  if(isToeplitz(mat)) {  Console.WriteLine('Yes');  } else {  Console.WriteLine('No');  }  } } 
JavaScript
// Function to check whether given // matrix is toeplitz matrix or not function isToeplitz(mat) {  let n = mat.length m = mat[0].length;    // HashMap to store keyvalue pairs  const mp = new Map();    for(let i = 0; i < n; i++) {  for(let j = 0; j < m; j++) {  let key = i - j;    // If key value exists in the hashmap  if (mp.has(key)) {    // check if the value is same   // as the current element  if (mp.get(key) !== mat[i][j])  return false;  }    // Else we put keyvalue pair in hashmap  else {  mp.set(i - j mat[i][j]);  }  }  }    return true; } let mat = [  [6 7 8]  [4 6 7]  [1 4 6] ]; if(isToeplitz(mat)) {  console.log('Yes'); } else {  console.log('No'); } 

الإخراج
Yes