|
9170 | 9170 | },
|
9171 | 9171 | {
|
9172 | 9172 | "cell_type": "code",
|
9173 |
| - "execution_count": 50, |
| 9173 | + "execution_count": 56, |
9174 | 9174 | "metadata": {},
|
9175 | 9175 | "outputs": [
|
9176 | 9176 | {
|
9177 | 9177 | "name": "stdout",
|
9178 | 9178 | "output_type": "stream",
|
9179 | 9179 | "text": [
|
9180 |
| - "[11, 25, 66, 1, 69, 7]\n", |
9181 |
| - "[23, 55, 17, 45, 15, 52]\n", |
9182 |
| - "[75, 31, 36, 44, 58, 8]\n", |
9183 |
| - "[22, 27, 33, 25, 68, 4]\n", |
9184 |
| - "[84, 28, 14, 11, 5, 50]\n" |
| 9180 | + "6 5\n", |
| 9181 | + "10\n", |
| 9182 | + "[[11, 25, 66, 1, 69, 7], [23, 55, 17, 45, 15, 52], [75, 31, 36, 44, 58, 8], [22, 27, 33, 25, 68, 4], [84, 28, 14, 11, 5, 50]]\n" |
9185 | 9183 | ]
|
9186 | 9184 | }
|
9187 | 9185 | ],
|
9188 | 9186 | "source": [
|
9189 | 9187 | "# 1329 : https://leetcode.com/problems/sort-the-matrix-diagonally/\n",
|
9190 | 9188 | "\n",
|
| 9189 | + "## ggot help from solution in comments : https://leetcode.com/problems/sort-the-matrix-diagonally/discuss/2659828/Easy-Solution-with\n", |
| 9190 | + "from collections import defaultdict\n", |
| 9191 | + "\n", |
9191 | 9192 | "mat = [[11,25,66,1,69,7],[23,55,17,45,15,52],[75,31,36,44,58,8],[22,27,33,25,68,4],[84,28,14,11,5,50]]\n",
|
| 9193 | + "col_mat = len(mat[0])\n", |
| 9194 | + "row_mat = len(mat)\n", |
| 9195 | + "\n", |
| 9196 | + "print(col_mat, row_mat)\n", |
| 9197 | + "\n", |
| 9198 | + "total_arrangements = (col_mat + row_mat) - 1\n", |
| 9199 | + "print(total_arrangements)\n", |
| 9200 | + "\n", |
| 9201 | + "diagonal_map = defaultdict(list)\n", |
| 9202 | + "for i in range(row_mat):\n", |
| 9203 | + " for j in range(col_mat):\n", |
| 9204 | + " diagonal_index = i - j\n", |
| 9205 | + " element = mat[i][j]\n", |
| 9206 | + " diagonal_map[diagonal_index].append(element)\n", |
| 9207 | + " # print(diagonal_map)\n", |
| 9208 | + "\n", |
| 9209 | + " for key in diagonal_map:\n", |
| 9210 | + " diagonal_map[key].sort(reverse=True)\n", |
| 9211 | + "\n", |
| 9212 | + " for j in range(row_mat):\n", |
| 9213 | + " diagonal_index = i - j\n", |
| 9214 | + " element = diagonal_map[diagonal_index].pop()\n", |
| 9215 | + " mat[i][j] = element\n", |
9192 | 9216 | "\n",
|
9193 |
| - "for val in mat: \n", |
9194 |
| - " print(val)" |
| 9217 | + "print(mat)\n" |
9195 | 9218 | ]
|
9196 | 9219 | },
|
9197 | 9220 | {
|
|
0 commit comments