Skip to content

Commit cfc5d47

Browse files
Sort Matrix by Diagnoal(py & js)
1 parent cc97fe2 commit cfc5d47

File tree

4 files changed

+154
-0
lines changed

4 files changed

+154
-0
lines changed

.DS_Store

2 KB
Binary file not shown.

Arrays/.DS_Store

8 KB
Binary file not shown.
Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "markdown",
5+
"id": "e78585ec",
6+
"metadata": {},
7+
"source": [
8+
"# Sort matrix by diagonal"
9+
]
10+
},
11+
{
12+
"cell_type": "code",
13+
"execution_count": 12,
14+
"id": "81aac26f",
15+
"metadata": {},
16+
"outputs": [],
17+
"source": [
18+
"from collections import defaultdict\n",
19+
"\n",
20+
"grid = [[1,7,3],[9,8,2],[4,5,6]]\n",
21+
"\n",
22+
"n = len(grid)\n",
23+
"diagonals = defaultdict(list)"
24+
]
25+
},
26+
{
27+
"cell_type": "code",
28+
"execution_count": 13,
29+
"id": "98da3a63",
30+
"metadata": {},
31+
"outputs": [
32+
{
33+
"name": "stdout",
34+
"output_type": "stream",
35+
"text": [
36+
"defaultdict(<class 'list'>, {0: [1, 8, 6], -1: [7, 2], -2: [3], 1: [9, 5], 2: [4]})\n"
37+
]
38+
}
39+
],
40+
"source": [
41+
"#Step 1: Collecting the diagonals\n",
42+
"for i in range(n):\n",
43+
" for j in range(n):\n",
44+
" diagonals[i-j].append(grid[i][j])\n",
45+
"\n",
46+
"print(diagonals)"
47+
]
48+
},
49+
{
50+
"cell_type": "code",
51+
"execution_count": 14,
52+
"id": "d6ef687f",
53+
"metadata": {},
54+
"outputs": [
55+
{
56+
"name": "stdout",
57+
"output_type": "stream",
58+
"text": [
59+
"defaultdict(<class 'list'>, {0: [8, 6, 1], -1: [2, 7], -2: [3], 1: [9, 5], 2: [4]})\n"
60+
]
61+
}
62+
],
63+
"source": [
64+
"#Step 2: Sort the diagnoals\n",
65+
"for key in diagonals:\n",
66+
" if key >=0: # Bottom - left triangle\n",
67+
" diagonals[key].sort(reverse=True) # Non - increasing order\n",
68+
" else: # Top - right triangle\n",
69+
" diagonals[key].sort() # Non - decreasing order\n",
70+
"\n",
71+
"print(diagonals)"
72+
]
73+
},
74+
{
75+
"cell_type": "code",
76+
"execution_count": 15,
77+
"id": "ca11a32c",
78+
"metadata": {},
79+
"outputs": [
80+
{
81+
"name": "stdout",
82+
"output_type": "stream",
83+
"text": [
84+
"[[8, 2, 3], [9, 6, 7], [4, 5, 1]]\n"
85+
]
86+
}
87+
],
88+
"source": [
89+
"#Step 3: Put them Back\n",
90+
"for i in range(n):\n",
91+
" for j in range(n):\n",
92+
" grid[i][j] = diagonals[i-j].pop(0)\n",
93+
"\n",
94+
"print(grid)"
95+
]
96+
}
97+
],
98+
"metadata": {
99+
"kernelspec": {
100+
"display_name": "Python 3",
101+
"language": "python",
102+
"name": "python3"
103+
},
104+
"language_info": {
105+
"codemirror_mode": {
106+
"name": "ipython",
107+
"version": 3
108+
},
109+
"file_extension": ".py",
110+
"mimetype": "text/x-python",
111+
"name": "python",
112+
"nbconvert_exporter": "python",
113+
"pygments_lexer": "ipython3",
114+
"version": "3.13.3"
115+
}
116+
},
117+
"nbformat": 4,
118+
"nbformat_minor": 5
119+
}

Arrays/Sort_matrix_by_diagonal.js

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
// Sort Matrix by Diagnoal
2+
3+
grid = [[1,7,3],[9,8,2],[4,5,6]];
4+
5+
const n = grid.length;
6+
const diagonals = {};
7+
8+
// Step 1: collect diagonals
9+
for(let i=0; i<n; i++){
10+
for(let j=0; j<n; j++){
11+
const key = i-j;
12+
if(!diagonals[key]) diagonals[key] = [];
13+
diagonals[key].push(grid[i][j]);
14+
}
15+
}
16+
17+
// Step 2: Sort the diagnoals
18+
for(const key in diagonals){
19+
const k = parseInt(key);
20+
if(key >=0 ){
21+
diagonals[key].sort((a,b)=>b-a); // non - increasing
22+
}else{
23+
diagonals[key].sort((a,b)=>a-b); // non - decreasing
24+
}
25+
}
26+
27+
// Step 3: Refil the grid
28+
for(let i=0; i<n; i++){
29+
for(let j =0; j<n; j++){
30+
const key = i-j;
31+
grid[i][j] = diagonals[key].shift();
32+
}
33+
}
34+
35+
console.log(grid)

0 commit comments

Comments
 (0)