Skip to content

Commit 560a6e9

Browse files
committed
sorted mat
1 parent e875524 commit 560a6e9

File tree

2 files changed

+63
-0
lines changed

2 files changed

+63
-0
lines changed

Diff for: DSA Crack Sheet/README.md

+1
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@
4747
- [Search a 2D Matrix](https://leetcode.com/problems/search-a-2d-matrix/ "view question") - [Cpp Solution](./solutions/Search%20a%202D%20Matrix.cpp)
4848
- [Median in a row-wise sorted Matrix](https://practice.geeksforgeeks.org/problems/median-in-a-row-wise-sorted-matrix1527/1 "view question") - [Cpp Solution](./solutions/Median%20in%20a%20row-wise%20sorted%20Matrix.cpp)
4949
- [Row with max 1s](https://practice.geeksforgeeks.org/problems/row-with-max-1s0023/1# "view question") - [Cpp Solution](./solutions/Row%20with%20max%201s.cpp)
50+
- [Sorted matrix](https://practice.geeksforgeeks.org/problems/sorted-matrix2333/1# "view question") - [Cpp Solution](./solutions/Sorted%20matrix.cpp)
5051
- []( "view question") - [Cpp Solution](./solutions/.cpp)
5152

5253
### Strings

Diff for: DSA Crack Sheet/solutions/Sorted matrix.cpp

+62
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
/*
2+
Sorted matrix
3+
=============
4+
5+
Given an NxN matrix Mat. Sort all elements of the matrix.
6+
7+
Example 1:
8+
Input:
9+
N=4
10+
Mat=[[10,20,30,40],
11+
[15,25,35,45]
12+
[27,29,37,48]
13+
[32,33,39,50]]
14+
Output:
15+
10 15 20 25
16+
27 29 30 32
17+
33 35 37 39
18+
40 45 48 50
19+
Explanation:
20+
Sorting the matrix gives this result.
21+
22+
Example 2:
23+
Input:
24+
N=3
25+
Mat=[[1,5,3],[2,8,7],[4,6,9]]
26+
Output:
27+
1 2 3
28+
4 5 6
29+
7 8 9
30+
Explanation:
31+
Sorting the matrix gives this result.
32+
Your Task:
33+
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.
34+
35+
Expected Time Complexity:O(N2LogN)
36+
Expected Auxillary Space:O(N2)
37+
38+
Constraints:
39+
1<=N<=1000
40+
1<=Mat[i][j]<=105
41+
*/
42+
43+
vector<vector<int>> sortedMatrix(int N, vector<vector<int>> Mat)
44+
{
45+
vector<int> sorted;
46+
for (auto &row : Mat)
47+
{
48+
for (auto &i : row)
49+
sorted.push_back(i);
50+
}
51+
sort(sorted.begin(), sorted.end());
52+
int i = 0;
53+
for (auto &row : Mat)
54+
{
55+
for (auto &e : row)
56+
{
57+
e = sorted[i];
58+
i++;
59+
}
60+
}
61+
return Mat;
62+
}

0 commit comments

Comments
 (0)