-
Notifications
You must be signed in to change notification settings - Fork 1
/
Sorted_matrix.cpp
87 lines (72 loc) · 1.78 KB
/
Sorted_matrix.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
/*
Problem Statement:
-----------------
Given an NxN matrix Mat. Sort all elements of the matrix.
Example 1:
---------
Input:
N=4
Mat=[[10,20,30,40],
[15,25,35,45]
[27,29,37,48]
[32,33,39,50]]
Output:
10 15 20 25
27 29 30 32
33 35 37 39
40 45 48 50
Explanation: Sorting the matrix gives this result.
Example 2:
---------
Input:
N=3
Mat=[[1,5,3],[2,8,7],[4,6,9]]
Output:
1 2 3
4 5 6
7 8 9
Explanation: Sorting the matrix gives this result.
Your Task: You don't need to read input or print anything. Your task is to complete the function sortedMatrix()
which takes the integer N and the matrix Mat as input parameters and returns the sorted matrix.
Expected Time Complexity:O(N^2 Log N)
Expected Auxillary Space:O(N^2)
*/
// Link --> https://practice.geeksforgeeks.org/problems/sorted-matrix2333/1
// Code:
class Solution
{
public:
vector <vector <int>> sortedMatrix(int N , vector <vector <int>> Mat)
{
int a[N*N];
int k = 0;
for(int i=0 ; i<N ; i++)
for(int j=0 ; j<N ; j++)
a[k++] = Mat[i][j];
sort(a , a+k);
k = 0;
for(int i=0 ; i<N ; i++)
for(int j=0 ; j<N ; j++)
Mat[i][j] = a[k++];
return Mat;
}
};
//Other Solution:
class Solution
{
public:
vector <vector <int>> sortedMatrix(int N , vector <vector <int>> Mat)
{
vector <int> temp;
int k = 0;
for(int i=0 ; i<N ; i++)
for(int j=0 ; j<N ; j++)
temp.push_back(Mat[i][j]);
sort(temp.begin(), temp.end());
k=0;
for(int i=0 ; i<N ; i++)
for(int j=0 ; j<N ; j++)
Mat[i][j] = temp[k++];
return Mat;
}
};