TheRealSamuel/LeetCodeProblem
0561
1{2 "id": 3031,3 "name": "construct_product_matrix",4 "difficulty": "Medium",5 "link": "https://leetcode.com/problems/construct-product-matrix/",6 "date": "2023-10-08 00:00:00",7 "task_description": "Given a **0-indexed** 2D integer matrix `grid` of size `n * m`, we define a **0-indexed** 2D matrix `p` of size `n * m` as the **product** matrix of `grid` if the following condition is met: Each element `p[i][j]` is calculated as the product of all elements in `grid` except for the element `grid[i][j]`. This product is then taken modulo `12345`. Return _the product matrix of_ `grid`. **Example 1:** ``` **Input:** grid = [[1,2],[3,4]] **Output:** [[24,12],[8,6]] **Explanation:** p[0][0] = grid[0][1] * grid[1][0] * grid[1][1] = 2 * 3 * 4 = 24 p[0][1] = grid[0][0] * grid[1][0] * grid[1][1] = 1 * 3 * 4 = 12 p[1][0] = grid[0][0] * grid[0][1] * grid[1][1] = 1 * 2 * 4 = 8 p[1][1] = grid[0][0] * grid[0][1] * grid[1][0] = 1 * 2 * 3 = 6 So the answer is [[24,12],[8,6]]. ``` **Example 2:** ``` **Input:** grid = [[12345],[2],[1]] **Output:** [[2],[0],[0]] **Explanation:** p[0][0] = grid[0][1] * grid[0][2] = 2 * 1 = 2. p[0][1] = grid[0][0] * grid[0][2] = 12345 * 1 = 12345. 12345 % 12345 = 0. So p[0][1] = 0. p[0][2] = grid[0][0] * grid[0][1] = 12345 * 2 = 24690. 24690 % 12345 = 0. So p[0][2] = 0. So the answer is [[2],[0],[0]]. ``` **Constraints:** `1 <= n == grid.length <= 105` `1 <= m == grid[i].length <= 105` `2 <= n * m <= 105` `1 <= grid[i][j] <= 109`",8 "public_test_cases": [9 {10 "label": "Example 1",11 "input": "grid = [[1,2],[3,4]]",12 "output": "[[24,12],[8,6]] "13 },14 {15 "label": "Example 2",16 "input": "grid = [[12345],[2],[1]]",17 "output": "[[2],[0],[0]] "18 }19 ],20 "private_test_cases": [21 {22 "input": [23 [24 12075,25 12075,26 7245,27 2145,28 10350,29 11805,30 1153531 ],32 [33 11940,34 12165,35 2145,36 10725,37 11535,38 2145,39 1153540 ],41 [42 10350,43 10350,44 11535,45 5970,46 11940,47 10350,48 724549 ]50 ],51 "output": [52 [53 12075,54 12075,55 7245,56 2145,57 10350,58 11805,59 1153560 ],61 [62 11940,63 12165,64 2145,65 10725,66 11535,67 2145,68 1153569 ],70 [71 10350,72 10350,73 11535,74 5970,75 11940,76 10350,77 724578 ]79 ]80 },81 {82 "input": [83 [84 60,85 120,86 9975,87 2550,88 24089 ],90 [91 9375,92 8535,93 1680,94 11460,95 1065096 ],97 [98 4305,99 9975,100 240,101 8265,102 825103 ],104 [105 5448,106 3360,107 1335,108 4440,109 8790110 ]111 ],112 "output": [113 [114 60,115 120,116 9975,117 2550,118 240119 ],120 [121 9375,122 8535,123 1680,124 11460,125 10650126 ],127 [128 4305,129 9975,130 240,131 8265,132 825133 ],134 [135 5448,136 3360,137 1335,138 4440,139 8790140 ]141 ]142 },143 {144 "input": [145 [146 6705147 ],148 [149 4794150 ],151 [152 3855153 ],154 [155 12270156 ],157 [158 3240159 ],160 [161 11550162 ],163 [164 6750165 ],166 [167 4755168 ],169 [170 11250171 ]172 ],173 "output": [174 [175 6705176 ],177 [178 4794179 ],180 [181 3855182 ],183 [184 12270185 ],186 [187 3240188 ],189 [190 11550191 ],192 [193 6750194 ],195 [196 4755197 ],198 [199 11250200 ]201 ]202 },203 {204 "input": [205 [206 9285207 ],208 [209 2235210 ],211 [212 4700213 ],214 [215 3375216 ],217 [218 9615219 ],220 [221 3600222 ]223 ],224 "output": [225 [226 9285227 ],228 [229 2235230 ],231 [232 4700233 ],234 [235 3375236 ],237 [238 9615239 ],240 [241 3600242 ]243 ]244 },245 {246 "input": [247 [248 9780,249 9345,250 11940,251 5865,252 6480253 ],254 [255 7320,256 11640,257 9240,258 1080,259 4410260 ],261 [262 30,263 3435,264 12075,265 10755,266 150267 ],268 [269 1215,270 8415,271 7410,272 8925,273 12225274 ],275 [276 8340,277 4500,278 4785,279 768,280 855281 ]282 ],283 "output": [284 [285 9780,286 9345,287 11940,288 5865,289 6480290 ],291 [292 7320,293 11640,294 9240,295 1080,296 4410297 ],298 [299 30,300 3435,301 12075,302 10755,303 150304 ],305 [306 1215,307 8415,308 7410,309 8925,310 12225311 ],312 [313 8340,314 4500,315 4785,316 768,317 855318 ]319 ]320 }321 ],322 "haskell_template": "constructProductMatrix :: [[Int]] -> [[Int]]\nconstructProductMatrix grid ",323 "ocaml_template": "let constructProductMatrix (grid: int list list) : int list list = ",324 "scala_template": "def constructProductMatrix(grid: List[List[Int]]): List[List[Int]] = { \n \n}",325 "java_template": "class Solution {\n public int[][] constructProductMatrix(int[][] grid) {\n \n }\n}",326 "python_template": "class Solution(object):\n def constructProductMatrix(self, grid):\n \"\"\"\n :type grid: List[List[int]]\n :rtype: List[List[int]]\n \"\"\"\n "327}