TheRealSamuel/LeetCodeProblem
0572
1{2 "id": 3748,3 "name": "sort_matrix_by_diagonals",4 "difficulty": "Medium",5 "link": "https://leetcode.com/problems/sort-matrix-by-diagonals/",6 "date": "2025-02-02 00:00:00",7 "task_description": "You are given an `n x n` square matrix of integers `grid`. Return the matrix such that: The diagonals in the **bottom-left triangle** (including the middle diagonal) are sorted in **non-increasing order**. The diagonals in the **top-right triangle** are sorted in **non-decreasing order**. **Example 1:** **Input:** grid = [[1,7,3],[9,8,2],[4,5,6]] **Output:** [[8,2,3],[9,6,7],[4,5,1]] **Explanation:** The diagonals with a black arrow (bottom-left triangle) should be sorted in non-increasing order: `[1, 8, 6]` becomes `[8, 6, 1]`. `[9, 5]` and `[4]` remain unchanged. The diagonals with a blue arrow (top-right triangle) should be sorted in non-decreasing order: `[7, 2]` becomes `[2, 7]`. `[3]` remains unchanged. **Example 2:** **Input:** grid = [[0,1],[1,2]] **Output:** [[2,1],[1,0]] **Explanation:** The diagonals with a black arrow must be non-increasing, so `[0, 2]` is changed to `[2, 0]`. The other diagonals are already in the correct order. **Example 3:** **Input:** grid = [[1]] **Output:** [[1]] **Explanation:** Diagonals with exactly one element are already in order, so no changes are needed. **Constraints:** `grid.length == grid[i].length == n` `1 <= n <= 10` `-105 <= grid[i][j] <= 105`",8 "public_test_cases": [9 {10 "label": "Example 1",11 "input": "grid = [[1,7,3],[9,8,2],[4,5,6]]",12 "output": "[[8,2,3],[9,6,7],[4,5,1]] "13 },14 {15 "label": "Example 2",16 "input": "grid = [[0,1],[1,2]]",17 "output": "[[2,1],[1,0]] "18 },19 {20 "label": "Example 3",21 "input": "grid = [[1]]",22 "output": "[[1]] "23 }24 ],25 "private_test_cases": [26 {27 "input": [28 [29 73413,30 8967531 ],32 [33 64042,34 342635 ]36 ],37 "output": [38 [39 73413,40 8967541 ],42 [43 64042,44 342645 ]46 ]47 },48 {49 "input": [50 [51 93534,52 -60538,53 1100,54 -2908055 ],56 [57 73904,58 73681,59 -42006,60 5803661 ],62 [63 -54576,64 43994,65 -44130,66 -919167 ],68 [69 35790,70 -98159,71 -43670,72 -7121973 ]74 ],75 "output": [76 [77 93534,78 -60538,79 1100,80 -2908081 ],82 [83 73904,84 73681,85 -42006,86 5803687 ],88 [89 -54576,90 43994,91 -44130,92 -919193 ],94 [95 35790,96 -98159,97 -43670,98 -7121999 ]100 ]101 },102 {103 "input": [104 [105 59508,106 -30375,107 53549108 ],109 [110 76794,111 -45364,112 68655113 ],114 [115 59076,116 -95778,117 -72798118 ]119 ],120 "output": [121 [122 59508,123 -30375,124 53549125 ],126 [127 76794,128 -45364,129 68655130 ],131 [132 59076,133 -95778,134 -72798135 ]136 ]137 },138 {139 "input": [140 [141 70237,142 -28097,143 -75510,144 -93714,145 -13815,146 -81959,147 -59992,148 -43975,149 -70863150 ],151 [152 40288,153 50377,154 -15278,155 -54865,156 -78871,157 38194,158 -64139,159 -23739,160 83299161 ],162 [163 75198,164 26853,165 45303,166 17073,167 -52554,168 -69846,169 41075,170 -10375,171 61633172 ],173 [174 71858,175 25443,176 -12504,177 17141,178 46172,179 -23439,180 16688,181 46570,182 24044183 ],184 [185 59184,186 60566,187 13801,188 -28186,189 -2540,190 59190,191 -3452,192 81433,193 67105194 ],195 [196 90462,197 37330,198 57191,199 -30681,200 -44432,201 -22472,202 59534,203 27280,204 83274205 ],206 [207 99727,208 15152,209 -37289,210 36698,211 -39528,212 -47895,213 -50758,214 75176,215 62571216 ],217 [218 83565,219 -16059,220 -17953,221 -40686,222 35575,223 -41645,224 -74717,225 -80703,226 78122227 ],228 [229 12632,230 65938,231 -48947,232 -75351,233 -84100,234 -18388,235 -96082,236 -97690,237 -92412238 ]239 ],240 "output": [241 [242 70237,243 -28097,244 -75510,245 -93714,246 -13815,247 -81959,248 -59992,249 -43975,250 -70863251 ],252 [253 40288,254 50377,255 -15278,256 -54865,257 -78871,258 38194,259 -64139,260 -23739,261 83299262 ],263 [264 75198,265 26853,266 45303,267 17073,268 -52554,269 -69846,270 41075,271 -10375,272 61633273 ],274 [275 71858,276 25443,277 -12504,278 17141,279 46172,280 -23439,281 16688,282 46570,283 24044284 ],285 [286 59184,287 60566,288 13801,289 -28186,290 -2540,291 59190,292 -3452,293 81433,294 67105295 ],296 [297 90462,298 37330,299 57191,300 -30681,301 -44432,302 -22472,303 59534,304 27280,305 83274306 ],307 [308 99727,309 15152,310 -37289,311 36698,312 -39528,313 -47895,314 -50758,315 75176,316 62571317 ],318 [319 83565,320 -16059,321 -17953,322 -40686,323 35575,324 -41645,325 -74717,326 -80703,327 78122328 ],329 [330 12632,331 65938,332 -48947,333 -75351,334 -84100,335 -18388,336 -96082,337 -97690,338 -92412339 ]340 ]341 },342 {343 "input": [344 [345 93680,346 -89587,347 -96793,348 -88056,349 -67363,350 -57242,351 -67539,352 -78121,353 -55829,354 -57391355 ],356 [357 70507,358 28181,359 -65217,360 -76332,361 -42429,362 -38150,363 -48555,364 -2009,365 -63212,366 39636367 ],368 [369 69116,370 50346,371 23408,372 -40122,373 -75329,374 29472,375 -21407,376 -29091,377 -468,378 29793379 ],380 [381 64817,382 39118,383 38117,384 4200,385 31191,386 -62081,387 42875,388 4261,389 43215,390 1967391 ],392 [393 73153,394 49917,395 23947,396 32443,397 -25788,398 51430,399 -30848,400 66550,401 62937,402 91163403 ],404 [405 48378,406 66018,407 37406,408 -4699,409 21998,410 -41176,411 78126,412 19189,413 79570,414 81497415 ],416 [417 -23104,418 19536,419 53987,420 29462,421 -10253,422 16954,423 -47341,424 80533,425 52244,426 81496427 ],428 [429 66168,430 -30888,431 -18525,432 40048,433 23718,434 -52041,435 -14933,436 -49448,437 90366,438 99859439 ],440 [441 55807,442 60419,443 -45089,444 -30668,445 8678,446 -10026,447 -56978,448 -16041,449 -78687,450 91900451 ],452 [453 -31370,454 -47928,455 -12268,456 -84613,457 -76766,458 -42972,459 -74771,460 -88090,461 -58183,462 -79213463 ]464 ],465 "output": [466 [467 93680,468 -89587,469 -96793,470 -88056,471 -67363,472 -57242,473 -67539,474 -78121,475 -55829,476 -57391477 ],478 [479 70507,480 28181,481 -65217,482 -76332,483 -42429,484 -38150,485 -48555,486 -2009,487 -63212,488 39636489 ],490 [491 69116,492 50346,493 23408,494 -40122,495 -75329,496 29472,497 -21407,498 -29091,499 -468,500 29793501 ],502 [503 64817,504 39118,505 38117,506 4200,507 31191,508 -62081,509 42875,510 4261,511 43215,512 1967513 ],514 [515 73153,516 49917,517 23947,518 32443,519 -25788,520 51430,521 -30848,522 66550,523 62937,524 91163525 ],526 [527 48378,528 66018,529 37406,530 -4699,531 21998,532 -41176,533 78126,534 19189,535 79570,536 81497537 ],538 [539 -23104,540 19536,541 53987,542 29462,543 -10253,544 16954,545 -47341,546 80533,547 52244,548 81496549 ],550 [551 66168,552 -30888,553 -18525,554 40048,555 23718,556 -52041,557 -14933,558 -49448,559 90366,560 99859561 ],562 [563 55807,564 60419,565 -45089,566 -30668,567 8678,568 -10026,569 -56978,570 -16041,571 -78687,572 91900573 ],574 [575 -31370,576 -47928,577 -12268,578 -84613,579 -76766,580 -42972,581 -74771,582 -88090,583 -58183,584 -79213585 ]586 ]587 }588 ],589 "haskell_template": "sortMatrix :: [[Int]] -> [[Int]]\nsortMatrix grid ",590 "ocaml_template": "let sortMatrix (grid: int list list) : int list list = ",591 "scala_template": "def sortMatrix(grid: List[List[Int]]): List[List[Int]] = { \n \n}",592 "java_template": "class Solution {\n public int[][] sortMatrix(int[][] grid) {\n \n }\n}",593 "python_template": "class Solution(object):\n def sortMatrix(self, grid):\n \"\"\"\n :type grid: List[List[int]]\n :rtype: List[List[int]]\n \"\"\"\n "594}