TheRealSamuel/LeetCodeProblem
0574
1{2 "id": 2801,3 "name": "difference_of_number_of_distinct_values_on_diagonals",4 "difficulty": "Medium",5 "link": "https://leetcode.com/problems/difference-of-number-of-distinct-values-on-diagonals/",6 "date": "2023-05-21 00:00:00",7 "task_description": "Given a 2D `grid` of size `m x n`, you should find the matrix `answer` of size `m x n`. The cell `answer[r][c]` is calculated by looking at the diagonal values of the cell `grid[r][c]`: Let `leftAbove[r][c]` be the number of **distinct** values on the diagonal to the left and above the cell `grid[r][c]` not including the cell `grid[r][c]` itself. Let `rightBelow[r][c]` be the number of **distinct** values on the diagonal to the right and below the cell `grid[r][c]`, not including the cell `grid[r][c]` itself. Then `answer[r][c] = |leftAbove[r][c] - rightBelow[r][c]|`. A **matrix diagonal** is a diagonal line of cells starting from some cell in either the topmost row or leftmost column and going in the bottom-right direction until the end of the matrix is reached. For example, in the below diagram the diagonal is highlighted using the cell with indices `(2, 3)` colored gray: Red-colored cells are left and above the cell. Blue-colored cells are right and below the cell. Return the matrix `answer`. **Example 1:** **Input:** grid = [[1,2,3],[3,1,5],[3,2,1]] **Output:** Output: [[1,1,0],[1,0,1],[0,1,1]] **Explanation:** To calculate the `answer` cells: answer left-above elements leftAbove right-below elements rightBelow |leftAbove - rightBelow| [0][0] [] 0 [grid[1][1], grid[2][2]] |{1, 1}| = 1 1 [0][1] [] 0 [grid[1][2]] |{5}| = 1 1 [0][2] [] 0 [] 0 0 [1][0] [] 0 [grid[2][1]] |{2}| = 1 1 [1][1] [grid[0][0]] |{1}| = 1 [grid[2][2]] |{1}| = 1 0 [1][2] [grid[0][1]] |{2}| = 1 [] 0 1 [2][0] [] 0 [] 0 0 [2][1] [grid[1][0]] |{3}| = 1 [] 0 1 [2][2] [grid[0][0], grid[1][1]] |{1, 1}| = 1 [] 0 1 **Example 2:** **Input:** grid = [[1]] **Output:** Output: [[0]] **Constraints:** `m == grid.length` `n == grid[i].length` `1 <= m, n, grid[i][j] <= 50`",8 "public_test_cases": [9 {10 "label": "Example 1",11 "input": "grid = [[1,2,3],[3,1,5],[3,2,1]]",12 "output": "[[1,1,0],[1,0,1],[0,1,1]] "13 },14 {15 "label": "Example 2",16 "input": "grid = [[1]]",17 "output": "[[0]] Constraints: m == grid.length n == grid[i].length 1 <= m, n, grid[i][j] <= 5"18 }19 ],20 "private_test_cases": [21 {22 "input": [23 [24 49,25 10,26 37,27 8,28 13,29 47,30 22,31 21,32 20,33 28,34 29,35 37,36 1,37 49,38 39,39 46,40 36,41 18,42 20,43 37,44 32,45 17,46 26,47 30,48 33,49 26,50 21,51 23,52 2,53 11,54 28,55 40,56 11,57 758 ],59 [60 36,61 21,62 46,63 40,64 30,65 7,66 33,67 15,68 7,69 5,70 37,71 20,72 17,73 41,74 44,75 19,76 29,77 5,78 31,79 1,80 23,81 2,82 3,83 10,84 18,85 32,86 2,87 31,88 35,89 30,90 26,91 9,92 4,93 3394 ],95 [96 33,97 21,98 7,99 13,100 49,101 21,102 11,103 12,104 7,105 3,106 9,107 25,108 6,109 15,110 7,111 30,112 28,113 3,114 28,115 45,116 16,117 5,118 6,119 37,120 23,121 41,122 45,123 25,124 26,125 25,126 17,127 50,128 1,129 1130 ],131 [132 6,133 31,134 29,135 11,136 43,137 28,138 19,139 35,140 13,141 27,142 39,143 44,144 41,145 49,146 4,147 36,148 28,149 18,150 7,151 21,152 19,153 34,154 50,155 19,156 25,157 14,158 45,159 24,160 18,161 26,162 16,163 22,164 33,165 41166 ],167 [168 3,169 21,170 36,171 47,172 20,173 47,174 24,175 43,176 20,177 34,178 40,179 11,180 50,181 34,182 25,183 25,184 38,185 27,186 35,187 2,188 5,189 17,190 29,191 23,192 10,193 45,194 17,195 23,196 49,197 42,198 10,199 12,200 13,201 50202 ],203 [204 25,205 11,206 4,207 2,208 13,209 25,210 45,211 46,212 40,213 22,214 49,215 35,216 12,217 40,218 41,219 29,220 19,221 41,222 33,223 33,224 37,225 46,226 44,227 16,228 47,229 2,230 8,231 31,232 42,233 15,234 35,235 45,236 50,237 49238 ],239 [240 43,241 39,242 40,243 49,244 15,245 12,246 26,247 38,248 50,249 46,250 28,251 31,252 16,253 25,254 14,255 24,256 12,257 42,258 1,259 42,260 43,261 46,262 2,263 27,264 41,265 34,266 42,267 20,268 21,269 5,270 17,271 2,272 42,273 47274 ],275 [276 21,277 38,278 17,279 32,280 42,281 8,282 34,283 29,284 9,285 7,286 27,287 22,288 27,289 2,290 20,291 45,292 17,293 1,294 20,295 37,296 27,297 1,298 20,299 17,300 44,301 9,302 31,303 26,304 46,305 11,306 39,307 22,308 10,309 32310 ],311 [312 4,313 15,314 24,315 35,316 34,317 39,318 47,319 26,320 12,321 38,322 38,323 13,324 1,325 29,326 32,327 44,328 18,329 49,330 19,331 29,332 2,333 43,334 6,335 38,336 35,337 47,338 12,339 15,340 2,341 5,342 30,343 1,344 19,345 27346 ],347 [348 29,349 46,350 36,351 43,352 49,353 50,354 27,355 38,356 19,357 33,358 28,359 22,360 23,361 1,362 28,363 7,364 49,365 7,366 42,367 48,368 50,369 42,370 15,371 12,372 35,373 17,374 17,375 43,376 18,377 50,378 7,379 7,380 43,381 10382 ],383 [384 21,385 26,386 12,387 46,388 11,389 39,390 47,391 47,392 21,393 30,394 2,395 15,396 30,397 48,398 32,399 23,400 15,401 35,402 29,403 29,404 1,405 11,406 50,407 20,408 16,409 44,410 25,411 43,412 38,413 41,414 23,415 25,416 8,417 1418 ],419 [420 31,421 28,422 14,423 43,424 50,425 10,426 38,427 49,428 45,429 26,430 24,431 8,432 32,433 26,434 37,435 5,436 21,437 6,438 12,439 47,440 49,441 35,442 12,443 12,444 1,445 16,446 14,447 40,448 14,449 45,450 43,451 47,452 10,453 3454 ],455 [456 23,457 8,458 39,459 45,460 22,461 17,462 18,463 23,464 3,465 36,466 30,467 38,468 10,469 22,470 11,471 44,472 46,473 23,474 24,475 37,476 4,477 9,478 44,479 5,480 14,481 29,482 42,483 5,484 5,485 47,486 37,487 44,488 2,489 12490 ],491 [492 45,493 36,494 25,495 25,496 17,497 43,498 49,499 46,500 31,501 23,502 31,503 42,504 17,505 35,506 44,507 17,508 46,509 6,510 43,511 1,512 23,513 46,514 26,515 14,516 22,517 7,518 21,519 7,520 23,521 11,522 18,523 30,524 16,525 23526 ],527 [528 39,529 36,530 23,531 16,532 2,533 15,534 21,535 13,536 5,537 25,538 23,539 7,540 34,541 22,542 28,543 44,544 32,545 28,546 11,547 45,548 19,549 27,550 14,551 26,552 28,553 37,554 43,555 5,556 48,557 25,558 48,559 47,560 20,561 46562 ],563 [564 48,565 8,566 47,567 40,568 34,569 36,570 32,571 12,572 35,573 19,574 45,575 23,576 33,577 22,578 22,579 22,580 50,581 33,582 33,583 23,584 13,585 45,586 45,587 10,588 14,589 38,590 6,591 43,592 16,593 9,594 18,595 18,596 8,597 5598 ],599 [600 32,601 29,602 5,603 31,604 20,605 12,606 37,607 25,608 42,609 40,610 26,611 25,612 37,613 4,614 31,615 7,616 3,617 45,618 17,619 43,620 1,621 32,622 29,623 2,624 24,625 7,626 19,627 32,628 43,629 35,630 41,631 15,632 29,633 19634 ],635 [636 37,637 35,638 4,639 33,640 30,641 24,642 13,643 29,644 38,645 23,646 50,647 17,648 43,649 36,650 19,651 9,652 27,653 19,654 24,655 28,656 28,657 12,658 29,659 36,660 18,661 1,662 24,663 3,664 6,665 20,666 9,667 40,668 27,669 28670 ],671 [672 48,673 44,674 23,675 23,676 47,677 2,678 23,679 19,680 44,681 23,682 7,683 33,684 40,685 30,686 12,687 18,688 50,689 49,690 31,691 26,692 29,693 28,694 14,695 42,696 49,697 29,698 50,699 7,700 49,701 31,702 40,703 32,704 7,705 49706 ],707 [708 15,709 30,710 38,711 2,712 1,713 36,714 29,715 37,716 31,717 10,718 4,719 48,720 27,721 33,722 48,723 47,724 45,725 48,726 5,727 36,728 33,729 3,730 38,731 31,732 42,733 24,734 26,735 38,736 11,737 6,738 27,739 9,740 29,741 19742 ],743 [744 15,745 47,746 9,747 20,748 40,749 29,750 4,751 50,752 49,753 49,754 50,755 38,756 5,757 46,758 24,759 17,760 9,761 9,762 10,763 15,764 18,765 8,766 40,767 44,768 2,769 34,770 33,771 21,772 35,773 13,774 33,775 2,776 6,777 35778 ],779 [780 25,781 33,782 18,783 18,784 27,785 35,786 13,787 16,788 44,789 27,790 47,791 27,792 45,793 45,794 31,795 35,796 2,797 41,798 8,799 24,800 34,801 44,802 32,803 24,804 39,805 9,806 42,807 36,808 38,809 45,810 43,811 19,812 17,813 29814 ],815 [816 4,817 4,818 48,819 7,820 24,821 15,822 42,823 24,824 3,825 31,826 49,827 9,828 6,829 45,830 12,831 17,832 34,833 22,834 18,835 16,836 32,837 38,838 16,839 1,840 9,841 44,842 25,843 38,844 28,845 33,846 9,847 24,848 33,849 22850 ],851 [852 24,853 25,854 15,855 38,856 42,857 14,858 15,859 32,860 3,861 5,862 33,863 13,864 2,865 37,866 36,867 32,868 19,869 22,870 3,871 22,872 16,873 4,874 4,875 27,876 14,877 36,878 18,879 30,880 49,881 38,882 38,883 13,884 8,885 10886 ],887 [888 28,889 5,890 31,891 43,892 1,893 47,894 3,895 30,896 8,897 5,898 36,899 13,900 49,901 13,902 6,903 3,904 40,905 8,906 45,907 46,908 41,909 44,910 49,911 35,912 6,913 14,914 45,915 20,916 42,917 49,918 12,919 40,920 40,921 5922 ],923 [924 33,925 39,926 46,927 18,928 43,929 17,930 45,931 43,932 16,933 32,934 26,935 24,936 28,937 33,938 23,939 49,940 29,941 11,942 47,943 13,944 28,945 45,946 14,947 1,948 46,949 47,950 9,951 1,952 42,953 11,954 25,955 17,956 17,957 11958 ],959 [960 28,961 26,962 33,963 6,964 32,965 15,966 47,967 30,968 30,969 10,970 18,971 47,972 45,973 49,974 41,975 26,976 40,977 6,978 16,979 39,980 30,981 30,982 35,983 6,984 10,985 7,986 39,987 1,988 42,989 20,990 48,991 19,992 7,993 28994 ],995 [996 43,997 24,998 22,999 40,1000 29,1001 7,1002 28,1003 6,1004 45,1005 50,1006 17,1007 48,1008 26,1009 6,1010 41,1011 16,1012 25,1013 9,1014 43,1015 50,1016 8,1017 10,1018 35,1019 46,1020 3,1021 11,1022 46,1023 27,1024 40,1025 49,1026 23,1027 10,1028 42,1029 401030 ],1031 [1032 33,1033 1,1034 35,1035 11,1036 14,1037 35,1038 1,1039 23,1040 48,1041 15,1042 43,1043 20,1044 40,1045 26,1046 43,1047 17,1048 35,1049 39,1050 1,1051 22,1052 31,1053 40,1054 38,1055 5,1056 42,1057 14,1058 33,1059 14,1060 13,1061 29,1062 23,1063 26,1064 21,1065 361066 ]1067 ],1068 "output": [1069 [1070 27,1071 21,1072 23,1073 20,1074 23,1075 19,1076 21,1077 24,1078 20,1079 18,1080 17,1081 19,1082 17,1083 17,1084 16,1085 18,1086 13,1087 11,1088 14,1089 13,1090 9,1091 11,1092 11,1093 9,1094 9,1095 7,1096 6,1097 6,1098 5,1099 3,1100 3,1101 2,1102 1,1103 01104 ],1105 [1106 22,1107 25,1108 19,1109 22,1110 18,1111 21,1112 17,1113 20,1114 22,1115 18,1116 16,1117 15,1118 18,1119 16,1120 15,1121 15,1122 16,1123 12,1124 9,1125 12,1126 12,1127 8,1128 9,1129 9,1130 7,1131 7,1132 6,1133 4,1134 4,1135 3,1136 1,1137 1,1138 0,1139 11140 ],1141 [1142 24,1143 20,1144 23,1145 17,1146 21,1147 16,1148 20,1149 15,1150 19,1151 20,1152 17,1153 14,1154 14,1155 16,1156 15,1157 13,1158 13,1159 14,1160 11,1161 7,1162 11,1163 10,1164 6,1165 7,1166 7,1167 5,1168 5,1169 4,1170 3,1171 2,1172 1,1173 0,1174 1,1175 21176 ],1177 [1178 21,1179 23,1180 18,1181 21,1182 15,1183 20,1184 14,1185 18,1186 14,1187 17,1188 18,1189 15,1190 13,1191 13,1192 14,1193 13,1194 11,1195 11,1196 12,1197 9,1198 6,1199 9,1200 8,