question_slug
stringlengths 3
77
| title
stringlengths 1
183
| slug
stringlengths 12
45
| summary
stringlengths 1
160
⌀ | author
stringlengths 2
30
| certification
stringclasses 2
values | created_at
stringdate 2013-10-25 17:32:12
2025-04-12 09:38:24
| updated_at
stringdate 2013-10-25 17:32:12
2025-04-12 09:38:24
| hit_count
int64 0
10.6M
| has_video
bool 2
classes | content
stringlengths 4
576k
| upvotes
int64 0
11.5k
| downvotes
int64 0
358
| tags
stringlengths 2
193
| comments
int64 0
2.56k
|
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
reshape-the-matrix | 1ms in java very easy code | 1ms-in-java-very-easy-code-by-galani_jen-jqdz | Code | Galani_jenis | NORMAL | 2024-12-22T06:58:27.726808+00:00 | 2024-12-22T06:58:27.726808+00:00 | 358 | false | 

# Code
```java []
class Solution {
public int[][] matrixReshape(int[][] mat, int r, int c) {
if (mat.length * mat[0].length != r * c) {
return mat;
}
int[][] output = new int[r][c];
for (int i = 0; i < r * c; i++) {
output[i / c][i % c] = mat[i / mat[0].length][i % mat[0].length];
}
return output;
}
}
``` | 4 | 0 | ['Java'] | 1 |
reshape-the-matrix | ✅Simple || Java || Beats 100% runtime || Easy to understand. | simple-java-beats-100-runtime-easy-to-un-04f4 | Intuition\n Describe your first thoughts on how to solve this problem. \nWe have to arrange the elements of the matrix into new matrix of given row and column.\ | Pratik-Shrivastava | NORMAL | 2023-01-06T08:54:16.500871+00:00 | 2023-01-06T08:54:16.500922+00:00 | 801 | false | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nWe have to arrange the elements of the matrix into new matrix of given row and column.\nWe have to check whether it is possible to construct a new array or not.\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n 1. Check the base conditions.\n 2. Create a new array.\n 3. Traverse and store the elements.\nFollow the code below to understand the solution.\n\n **If this solution helped you, give it an up-vote to help others** \n\n# Complexity\n- Time complexity: O(n^2)\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity: O(1)\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```\nclass Solution {\n public int[][] matrixReshape(int[][] mat, int r, int c) {\n\n int oldRow = mat.length;\n int oldCol = mat[0].length;\n\n\n //We have to check whether it is possible to construct-\n //-a new array or not.\n //we can check it by comparing the total elements.\n if(oldCol*oldRow != r*c) return mat;\n\n int [] [] arr = new int[r][c];\n int i = 0; // row of new array\n int j = 0; // col of new array\n int k = 0; // row of old array\n int l = 0; // col of old array\n\n //Traverse the new array and put elements from the old array.\n for(i = 0; i < r; i++)\n {\n for(j = 0; j < c; j++)\n {\n arr[i][j] = mat[k][l];\n\n //We also need to update the indices of the old array.\n //If column length is reached then increment row\n // and reset column to 0;\n l++;\n if(l == oldCol)\n {\n k++;\n l = 0;\n }\n\n }\n }\n //Finally, return the new array;\n return arr;\n \n }\n}\n``` | 4 | 0 | ['Java'] | 0 |
reshape-the-matrix | simple solution | 1ms | single Loop | with explaination | simple-solution-1ms-single-loop-with-exp-k0b9 | \t\t\t\n\t\t\tclass Solution {\n\t\t\t\tpublic int[][] matrixReshape(int[][] nums, int r, int c) {\n\t\t\t\t\n\t\t\t\tint m = nums.length, n = nums[0].length;\n | RohiniK98 | NORMAL | 2022-10-14T14:52:57.333925+00:00 | 2022-10-14T14:56:23.850140+00:00 | 467 | false | \t\t\t\n\t\t\tclass Solution {\n\t\t\t\tpublic int[][] matrixReshape(int[][] nums, int r, int c) {\n\t\t\t\t\n\t\t\t\tint m = nums.length, n = nums[0].length;\n\t\t\t\t\n\t\t\t\tif (r * c != m * n)\n\t\t\t\t\treturn nums;\n\t\t\t\t\t\n\t\t\t\tint[][] reshaped = new int[r][c];\n\t\t\t\t\n\t\t\t\tfor (int i = 0; i < r * c; i++)\n\t\t\t\t\treshaped[i/c][i%c] = nums[i/n][i%n];\n\t\t\t\t\t\n\t\t\t\treturn reshaped;\n\t\t\t }\n\t\t\t}\n\t\t\t\n\t\t\t\n\t\t\t\n#### \t\t\tTime Complexity --> O(N*M)\n#### \t\t\tSpace Complexity --> O(r*c) | 4 | 0 | ['Java'] | 0 |
reshape-the-matrix | Short JavaScript Solution | short-javascript-solution-by-sronin-s8nv | Found this solution helpful? Consider showing support by upvoting this post.\nHave a question? Kindly leave a comment below.\nThank you and happy hacking!\n\n\n | sronin | NORMAL | 2022-09-19T01:07:18.934590+00:00 | 2022-10-04T19:15:50.052093+00:00 | 655 | false | Found this solution helpful? Consider showing support by upvoting this post.\nHave a question? Kindly leave a comment below.\nThank you and happy hacking!\n\n\n```\nvar matrixReshape = function (mat, r, c) {\n if (mat.length * mat[0].length !== r * c) return mat //Checks if a reshape is possible.\n let elements = []\n let reshapedMat = []\n\t\n for (let row of mat) elements.push(...row)\n\n for (let i = 0; i < elements.length; i += c) {\n reshapedMat.push(elements.slice(i, i + c))\n }\n\n return reshapedMat\n};\n``` | 4 | 0 | ['JavaScript'] | 0 |
reshape-the-matrix | Beats 97%: Python List Comprehension | beats-97-python-list-comprehension-by-ca-btsa | \nclass Solution:\n def matrixReshape(self, mat: List[List[int]], r: int, c: int) -> List[List[int]]:\n flat = [item for sublist in mat for item in su | Camille2985 | NORMAL | 2022-09-15T16:56:18.758346+00:00 | 2022-09-15T16:57:29.864607+00:00 | 714 | false | ```\nclass Solution:\n def matrixReshape(self, mat: List[List[int]], r: int, c: int) -> List[List[int]]:\n flat = [item for sublist in mat for item in sublist]\n if r * c != len(flat): return mat \n output = [flat[(row *c) :c * (row +1)] for row in range(r)]\n return output\n \n``` | 4 | 0 | ['Python'] | 1 |
reshape-the-matrix | [JAVA] O(nm) solution 1 ms, best solution | java-onm-solution-1-ms-best-solution-by-ke08z | \nclass Solution {\n public int[][] matrixReshape(int[][] mat, int r, int c) {\n int n = mat.length, m = mat[0].length;\n \n if(r * c != m * | Jugantar2020 | NORMAL | 2022-07-27T22:15:41.153519+00:00 | 2022-07-27T22:15:41.153567+00:00 | 220 | false | ```\nclass Solution {\n public int[][] matrixReshape(int[][] mat, int r, int c) {\n int n = mat.length, m = mat[0].length;\n \n if(r * c != m * n) {\n return mat;\n }\n \n int result[][] = new int[r][c];\n for(int i = 0; i < r * c; i ++) {\n result[i / c][i % c] = mat[i / m][i % m];\n }\n return result;\n }\n}\n```\n# PLEASE UPVOTE IF IT WAS HELPFULL | 4 | 0 | ['Matrix', 'Java'] | 1 |
reshape-the-matrix | Python solution 88.68% Faster | python-solution-8868-faster-by-dodleyand-z2ta | \ndef matrixReshape(self, mat: List[List[int]], r: int, c: int) -> List[List[int]]:\n if (len(mat) * len(mat[0]) != r * c):\n return mat\n | dodleyandu | NORMAL | 2022-06-24T02:20:44.008189+00:00 | 2022-06-24T02:20:44.008224+00:00 | 225 | false | ```\ndef matrixReshape(self, mat: List[List[int]], r: int, c: int) -> List[List[int]]:\n if (len(mat) * len(mat[0]) != r * c):\n return mat\n flat_mat = []\n prop_mat = []\n for i in range(len(mat)):\n for j in mat[i]:\n flat_mat.append(j)\n end = c\n for i in range(0,len(flat_mat),c):\n prop_mat.append(flat_mat[i:end])\n end += c\n \n return prop_mat\n``` | 4 | 0 | ['Python'] | 0 |
reshape-the-matrix | C++ Simple + Understandable Solution || Clean Code + Two Loop (r X c) | c-simple-understandable-solution-clean-c-6ds1 | \t\t\t\t\t\t\u21C8\nIf this really help please leave me a reputation \u270C.\n## Clean code:-\n\n\nclass Solution {\npublic:\n vector<vector<int>> matrixResh | suryaprakashspro | NORMAL | 2022-02-25T08:01:44.654012+00:00 | 2022-02-25T08:05:55.519417+00:00 | 114 | false | \t\t\t\t\t\t\u21C8\nIf this really help please leave me a reputation \u270C.\n## Clean code:-\n\n```\nclass Solution {\npublic:\n vector<vector<int>> matrixReshape(vector<vector<int>>& mat, int r, int c) {\n \n int rows = mat.size(), columns = mat[0].size(), length = rows * columns; \n // Fetch size of the input matrix (rows and columns) and total length of the input matrix.\n \n if(length != (r * c)) return mat; \n // If the given reshape size is invalid then the return input matrix itself.\n \n vector<vector<int>> output(r, vector<int>(c));\n // To store the output matrix.\n \n for(int i = 0; i < length; i++) {\n \n output[i / c][i % c] = mat[i / columns][i % columns];\n // i/c - will return row (The number after point is neglected as i and c is declared as integer).\n // i%c - will return column (The number after point is neglected as i and c is declared as integer).\n // i / columns and i % columns also do the same.\n }\n \n return output; \n }\n};\n```\n### Time Complexity:-\n##### O(r X c) or O(n\xB2)\n\n===========================================================================\n\n## Understandable Code:-\n\n```\nclass Solution {\npublic:\n vector<vector<int>> matrixReshape(vector<vector<int>>& mat, int r, int c) {\n \n int rows = mat.size(), columns = mat[0].size(); // Fetch size of the input matrix (rows and columns).\n \n if((rows * columns) != (r * c)) {\n \n return mat; // If the given reshape size is invalid then the return input matrix itself.\n }\n \n vector<vector<int>> output(r, vector<int>(c, 0));\n int rowOut = 0, colOut = 0; // To insert elements in specified r X c format.\n \n for(int i = 0; i<rows; i++) {\n for(int j = 0; j<columns; j++) {\n \n output[rowOut][colOut] = mat[i][j];\n colOut++; // Increment output columns.\n \n if(colOut == c) { \n \n rowOut++; // Move to the next row after we fill all the column elements in previous row.\n colOut = 0; // After we reach the specified column size reset the column.\n \n }\n }\n }\n \n return output;\n \n }\n};\n```\n### Time Complexity:-\n##### O(r X c) or O(n\xB2) | 4 | 0 | ['C'] | 0 |
reshape-the-matrix | Go solution using range, append and mod | go-solution-using-range-append-and-mod-b-ljap | go\nfunc matrixReshape(mat [][]int, r int, c int) [][]int {\n\tif len(mat) == 0 || len(mat[0]) == 0 {\n\t\treturn mat\n\t} else if r*c != len(mat)*len(mat[0]) { | jeremychase | NORMAL | 2022-02-15T22:07:03.156958+00:00 | 2022-02-15T22:07:03.156992+00:00 | 256 | false | ```go\nfunc matrixReshape(mat [][]int, r int, c int) [][]int {\n\tif len(mat) == 0 || len(mat[0]) == 0 {\n\t\treturn mat\n\t} else if r*c != len(mat)*len(mat[0]) {\n\t\treturn mat\n\t}\n\n\tresult := [][]int{}\n\tp := 0\n\n\tfor _, row := range mat {\n\t\tfor _, v := range row {\n\t\t\tif p%c == 0 {\n\t\t\t\tresult = append(result, []int{v})\n\t\t\t} else {\n\t\t\t\tresult[p/c] = append(result[p/c], v)\n\t\t\t}\n\n\t\t\tp++\n\t\t}\n\t}\n\n\treturn result\n}\n``` | 4 | 0 | ['Go'] | 2 |
reshape-the-matrix | 11 ms, faster than 70.37% of C++ online submissions | 11-ms-faster-than-7037-of-c-online-submi-a3xb | \nclass Solution {\npublic:\n vector<vector<int>> matrixReshape(vector<vector<int>>& mat, int r, int c) {\n \n if( mat.size() * mat[0].size() | anni007 | NORMAL | 2022-02-04T15:29:49.267775+00:00 | 2022-02-04T15:29:49.267825+00:00 | 159 | false | ```\nclass Solution {\npublic:\n vector<vector<int>> matrixReshape(vector<vector<int>>& mat, int r, int c) {\n \n if( mat.size() * mat[0].size() != r*c)\n return mat;\n \n vector<vector<int>>ans(r,vector<int>(c));\n \n int row=0, col =0;\n \n for( int i = 0 ; i <mat.size() ; i++ ){\n \n for( int j = 0 ; j < mat[0].size() ; j++ ){\n \n if( col == c ){\n row++;\n col=0;\n }\n ans[row][col] = mat[i][j];\n col++;\n }\n }\n return ans;\n }\n};\n```\n\n**PLEASE UPVOTE IF THIS SOLUTION IS HELPFUL FOR YOU** | 4 | 0 | ['C++'] | 0 |
reshape-the-matrix | C++ easy solution || without converting to 1-D array || beginner friendly | c-easy-solution-without-converting-to-1-n8ta7 | Please upvote if you find it helpful :)\n\nclass Solution {\npublic:\n vector<vector<int>> matrixReshape(vector<vector<int>>& mat, int r, int c) {\n i | pragya710 | NORMAL | 2022-01-20T07:50:15.465478+00:00 | 2022-01-20T07:50:15.465515+00:00 | 283 | false | *Please **upvote** if you find it helpful :)*\n```\nclass Solution {\npublic:\n vector<vector<int>> matrixReshape(vector<vector<int>>& mat, int r, int c) {\n int mr = mat.size();\n int nc = mat[0].size();\n vector<vector<int> > ans;\n if(mr*nc != r*c)\n return mat;\n int k=0,l=0;\n for(int i=0;i<r;i++) {\n vector<int> v;\n for(int j=0;j<c;j++) {\n v.push_back(mat[k][l]);\n l++;\n if(l>=nc) {\n k++;\n l=0;\n }\n }\n ans.push_back(v);\n }\n return ans;\n }\n};\n``` | 4 | 0 | ['Array', 'C'] | 0 |
reshape-the-matrix | Python 3 (84ms) | O(m*n) | Creating New Matrix & Inserting Our Values | Easy Solution | python-3-84ms-omn-creating-new-matrix-in-9vlk | Creating New Matrix of 0s and then Inserting our values one by one.\nTakes O(m * n) Time & Space.\n\n\nclass Solution:\n def matrixReshape(self, mat: List[Li | MrShobhit | NORMAL | 2022-01-18T13:51:28.182442+00:00 | 2022-01-18T13:51:28.182474+00:00 | 198 | false | Creating New Matrix of 0s and then Inserting our values one by one.\nTakes O(m * n) Time & Space.\n\n```\nclass Solution:\n def matrixReshape(self, mat: List[List[int]], r: int, c: int) -> List[List[int]]:\n n,m=len(mat[0]),len(mat)\n if n*m!=r*c:\n return mat\n k=0\n tmp=[]\n output=[[0 for i in range(c)] for j in range(r)]\n for i in mat:\n for j in i:\n tmp.append(j)\n for i in range(r):\n for j in range(c):\n output[i][j] = tmp[k]\n k+=1\n return output\n``` | 4 | 0 | ['Matrix', 'Python'] | 1 |
reshape-the-matrix | ✅📌 Best Solution || 100%(0ms) || Clean Code | best-solution-1000ms-clean-code-by-premb-qirw | \nclass Solution {\n public int[][] matrixReshape(int[][] mat, int r, int c) {\n int[][] matrix = new int[r][c];\n if(r*c != mat.length*mat[0]. | prembhimavat | NORMAL | 2021-12-20T12:46:19.319140+00:00 | 2021-12-20T12:46:34.114681+00:00 | 301 | false | ```\nclass Solution {\n public int[][] matrixReshape(int[][] mat, int r, int c) {\n int[][] matrix = new int[r][c];\n if(r*c != mat.length*mat[0].length) return mat;\n int m = 0;\n int n = 0;\n for(int i=0;i<mat.length;i++){\n for(int j=0;j<mat[0].length;j++){\n if(m!=r){\n matrix[m][n]=mat[i][j];\n n++;\n if(n==c){\n m++;\n n=0;\n }\n }\n }\n }\n return matrix;\n }\n}\n```\n**Feel free to ask questions in the comment section.** | 4 | 0 | ['Java'] | 0 |
reshape-the-matrix | Java Simple Solution (0 ms, faster than 100.00%) | java-simple-solution-0-ms-faster-than-10-749k | Runtime: 0 ms, faster than 100.00% of Java online submissions.\nMemory Usage: 39.7 MB, less than 90.75% of Java online submissions.\n\nclass Solution {\n pub | Madhav1301 | NORMAL | 2021-10-26T01:43:49.164886+00:00 | 2021-10-26T01:46:07.946039+00:00 | 202 | false | **Runtime: 0 ms, faster than 100.00% of Java online submissions.\nMemory Usage: 39.7 MB, less than 90.75% of Java online submissions.**\n```\nclass Solution {\n public int[][] matrixReshape(int[][] mat, int r, int c) {\n int[][] ans = new int[r][c];\n \n if(r*c != mat.length*mat[0].length)\n return mat;\n \n int row = 0;\n int col = 0;\n for(int i=0; i<mat.length; i++){\n for(int j=0; j<mat[0].length; j++){\n ans[row][col++] = mat[i][j];\n \n if(col == c){\n col = 0;\n row++;\n }\n }\n }\n return ans;\n }\n}\n``` | 4 | 2 | ['Java'] | 1 |
reshape-the-matrix | Python3 easy solution O(n*m) with explanation and problem solving logic | python3-easy-solution-onm-with-explanati-lce3 | \nclass Solution:\n def matrixReshape(self, mat: List[List[int]], r: int, c: int) -> List[List[int]]:\n \n # first step is quite obvious - chec | ajinkya2021 | NORMAL | 2021-09-16T18:04:47.717292+00:00 | 2021-09-16T18:04:47.717341+00:00 | 355 | false | ```\nclass Solution:\n def matrixReshape(self, mat: List[List[int]], r: int, c: int) -> List[List[int]]:\n \n # first step is quite obvious - check if transformation is possible\n # check if rows*columns dimensions are same for og and transformed matrix\n \n if len(mat)*len(mat[0]) != r*c:\n return mat\n \n # create a new blank matrix with new dimensions (r*c)\n \n new_mat = [[0 for i in range(c)] for i in range(r)]\n \n # now let\'s dive into the problem\n # we will iterate through each zero in the new matrix and modify it according to the value in the old matrix \n # so we will need new pointers that move through old matrix\n \n # let\'s initiate two new pointers\n \n old_row = old_col = 0\n \n # let\'s begin the loop\n \n for i in range(r):\n for j in range(c):\n new_mat[i][j] = mat[old_row][old_col] # here we set new mat (0,0) to old mat (0,0)\n \n # let\'s decide where to go from now\n \n # if index runs out of new dimensions, reset the column to zero and change row to +1; that is..\n # .. traverse to the first column of next row and start from there\n \n if old_col+1 > len(mat[0])-1:\n old_col=0\n old_row+=1\n else:\n old_col+=1\n \n return new_mat\n \n```\n | 4 | 0 | ['Python', 'Python3'] | 1 |
reshape-the-matrix | java 100% faster solution | java-100-faster-solution-by-singhakshita-o0hk | \npublic int[][] matrixReshape(int[][] mat, int r, int c) {\n if(r*c != mat[0].length*mat.length){\n return mat;\n }\n int ans [ | singhakshita1210 | NORMAL | 2021-08-29T14:19:02.321068+00:00 | 2021-08-29T14:19:32.856587+00:00 | 436 | false | ```\npublic int[][] matrixReshape(int[][] mat, int r, int c) {\n if(r*c != mat[0].length*mat.length){\n return mat;\n }\n int ans [][] = new int[r][c];\n int m =0,n=0;\n for(int i=0;i<r;i++){\n for(int j=0;j<c;j++){\n ans[i][j] = mat[m][n];\n if(n< mat[0].length-1){\n n++;\n }else{\n m++;\n n=0; \n }\n \n }\n }\n return ans;\n }\n```\n | 4 | 1 | ['Java'] | 1 |
reshape-the-matrix | 4ms golang solution using channels and go routine | 4ms-golang-solution-using-channels-and-g-i7ae | \nfunc matrixReshape(mat [][]int, r int, c int) [][]int {\n n,m := len(mat), len(mat[0])\n \n if n * m != r * c {\n return mat\n }\n \n | kaiiiiii | NORMAL | 2021-07-05T12:22:59.940212+00:00 | 2021-07-05T12:22:59.940250+00:00 | 230 | false | ```\nfunc matrixReshape(mat [][]int, r int, c int) [][]int {\n n,m := len(mat), len(mat[0])\n \n if n * m != r * c {\n return mat\n }\n \n ans := make([][]int, r)\n for i := range ans {\n ans[i] = make([]int,c)\n }\n \n ch,done := make(chan int),make(chan struct{})\n go receiveVal(&mat,n,m,ch)\n go sendVal(&ans,r,c,ch,done)\n <- done\n return ans\n}\n\nfunc receiveVal(mat *[][]int,n,m int,ch chan<- int) {\n for i := 0; i < n; i++ {\n for j := 0; j < m; j++ {\n ch <- (*mat)[i][j]\n }\n }\n}\n\nfunc sendVal(ans *[][]int,n,m int,ch <-chan int,done chan struct{}) {\n for i := 0; i < n; i++ {\n for j := 0; j < m; j++ {\n (*ans)[i][j] = <- ch\n }\n }\n done <- struct{}{}\n}\n``` | 4 | 0 | ['Go'] | 0 |
reshape-the-matrix | C++|| beginner frindly || Easy to understand | c-beginner-frindly-easy-to-understand-by-a2pu | just check if the number of elements in the reshaped matrix and original array are equal or not.\nif then store in another matrix with given row and column.\n\n | VineetKumar2023 | NORMAL | 2021-07-05T07:28:14.523224+00:00 | 2021-07-05T07:28:14.523267+00:00 | 164 | false | just check if the number of elements in the reshaped matrix and original array are equal or not.\nif then store in another matrix with given row and column.\n\nfor assigning values,\niterate over the size of new matrix and assign the value as shown in the code.\n\nHere\'s the code:\n```\nclass Solution {\npublic:\n vector<vector<int>> matrixReshape(vector<vector<int>>& mat, int r, int c) {\n // size of the mat matrix\n int a=mat.size();\n int b=mat[0].size();\n \n // check if the size is equal to the new matrix or not\n if(a*b!=r*c)\n return mat;\n // 2-d vector of the given size\n vector<vector<int>> ans(r,vector<int>(c,0));\n \n int p=0;\n int q=0;\n \n // assign values of mat matrix to new matrix\n for(int i=0;i<r;i++)\n {\n for(int j=0;j<c;j++)\n {\n ans[i][j]=mat[p%a][(q++)%b];\n if(q%b==0)\n p++;\n }\n }\n return ans;\n }\n};\n``` | 4 | 0 | ['C', 'C++'] | 0 |
reshape-the-matrix | C++ || Easy || faster than 96% || single loop | c-easy-faster-than-96-single-loop-by-pri-yoe6 | \nvector<vector<int>>out(r,vector<int>(c,0));\n int n=mat.size();//row size\n int m=mat[0].size();//column size\n if((n*m)==(r*c))\n | priyamesh28 | NORMAL | 2021-06-17T08:16:47.010877+00:00 | 2021-06-17T08:16:47.010921+00:00 | 168 | false | ```\nvector<vector<int>>out(r,vector<int>(c,0));\n int n=mat.size();//row size\n int m=mat[0].size();//column size\n if((n*m)==(r*c))\n {\n for(int i=0;i<(r*c);i++)\n {\n out[i/c][i%c]=mat[i/m][i%m];//simple evaluation of matrix\n }\n return out;\n }\n else\n return mat;\n```\n\nIf you find any issue in understanding the solutions then comment below, will try to help you.\nIf you found my solution useful.\nSo please do upvote and encourage me to document all leetcode problems\uD83D\uDE03\nHappy Coding :) | 4 | 1 | ['C'] | 2 |
reshape-the-matrix | Python3 simple solution | python3-simple-solution-by-eklavyajoshi-jubn | \nclass Solution:\n def matrixReshape(self, nums: List[List[int]], r: int, c: int) -> List[List[int]]:\n n = len(nums)\n m = len(nums[0])\n | EklavyaJoshi | NORMAL | 2021-03-02T05:49:23.808627+00:00 | 2021-03-02T05:49:23.808678+00:00 | 305 | false | ```\nclass Solution:\n def matrixReshape(self, nums: List[List[int]], r: int, c: int) -> List[List[int]]:\n n = len(nums)\n m = len(nums[0])\n if n*m != r*c:\n return nums\n else:\n l = []\n res = []\n for i in range(n):\n l.extend(nums[i])\n for i in range(r):\n res.append(l[i*c:i*c+c])\n return res\n```\n**If you like the solution, please vote for this** | 4 | 0 | ['Python3'] | 0 |
reshape-the-matrix | python 3 short solution, O(mr), 88ms (97%) | python-3-short-solution-omr-88ms-97-by-p-dnro | \nclass Solution:\n def matrixReshape(self, nums: List[List[int]], r: int, c: int) -> List[List[int]]:\n #\n m = len(nums); n = len(nums[0])\n | philno | NORMAL | 2020-12-27T20:55:13.917067+00:00 | 2021-01-30T22:07:48.158590+00:00 | 391 | false | ```\nclass Solution:\n def matrixReshape(self, nums: List[List[int]], r: int, c: int) -> List[List[int]]:\n #\n m = len(nums); n = len(nums[0])\n if m*n != r*c: return nums\n flattern = []\n for i in range(m):\n flattern += nums[i]\n return [flattern[i*c:i*c+c] for i in range(r)]\n```\n\n | 4 | 0 | ['Python', 'Python3'] | 2 |
largest-local-values-in-a-matrix | Fastest (100%) || Easy || Clean & Concise || Space Optimized | fastest-100-easy-clean-concise-space-opt-rqva | \n\n\n\n# Approach: Sliding Window\n\n - Loop through each cell of the input grid except for the border cells.\n\n - For each cell (non - border region), sc | gameboey | NORMAL | 2024-05-12T00:15:34.403593+00:00 | 2024-05-13T06:44:23.971490+00:00 | 34,232 | false | \n\n\n\n# Approach: Sliding Window\n\n - Loop through each cell of the input grid except for the border cells.\n\n - For each cell (non - border region), scan the 3x3 subgrid centered around it and find the maximum value. \n\n - Store the maximum values in a result grid of size (n - 2) x (m - 2), excluding border cells at indices `(i - 1, j - 1)` which is a shift by 1 to match the problem\'s requiremnt that `res[i][j]`should correspond to the largest value of 3x3 matrix in the `gird` matrix centred around row `i + 1` and col `j + 1`. \n\n - Return the result grid containing the largest value of each contiguous 3x3 subgrid in the input grid.\n\nThis approach efficiently identifies the largest value within each 3x3 subgrid centered around non-border cells in the input grid.\n\n\n\n# Complexity\n- Time complexity: O(n^2)\n\n- Space complexity: O(n^2)\n\n# Code\n```C++ []\n#pragma GCC optimize ("Ofast")\n#pragma GCC target ("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx")\n#pragma GCC optimize ("-ffloat-store")\n\nclass Solution {\npublic:\n vector<vector<int>> largestLocal(vector<vector<int>>& grid) {\n ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);\n int n = grid.size();\n vector<vector<int>> res(n - 2, vector<int>(n - 2));\n\n for(int i = 1; i < n - 1; ++i) {\n for(int j = 1; j < n - 1; ++j) {\n int temp = 0;\n\n for(int k = i - 1; k <= i + 1; ++k) {\n for(int l = j - 1; l <= j + 1; ++l) {\n temp = max(temp, grid[k][l]);\n }\n }\n\n res[i - 1][j - 1] = temp;\n }\n }\n\n return res;\n }\n};\n```\n```java []\nclass Solution {\n public int[][] largestLocal(int[][] grid) {\n int n = grid.length;\n int[][] res = new int[n - 2][n - 2];\n\n for(int i = 1; i < n - 1; ++i) {\n for(int j = 1; j < n - 1; ++j) {\n int temp = 0;\n\n for(int k = i - 1; k <= i + 1; ++k) {\n for(int l = j - 1; l <= j + 1; ++l) {\n temp = Math.max(temp, grid[k][l]);\n }\n }\n\n res[i - 1][j - 1] = temp;\n }\n }\n\n return res;\n }\n}\n```\n```python []\nclass Solution:\n def largestLocal(self, grid: List[List[int]]) -> List[List[int]]:\n n, res = len(grid), []\n\n for i in range(1, n - 1):\n temp_row = []\n for j in range(1, n - 1):\n temp = 0\n\n for k in range(i - 1, i + 2):\n for l in range(j - 1, j + 2):\n temp = max(temp, grid[k][l])\n\n temp_row.append(temp)\n res.append(temp_row)\n\n return res\n```\n```javascript []\n/**\n * @param {number[][]} grid\n * @return {number[][]}\n */\nvar largestLocal = function(grid) {\n const n = grid.length;\n const res = [];\n\n for (let i = 1; i < n - 1; ++i) {\n const tempRow = [];\n for (let j = 1; j < n - 1; ++j) {\n let temp = 0;\n\n for (let k = i - 1; k <= i + 1; ++k) {\n for (let l = j - 1; l <= j + 1; ++l) {\n temp = Math.max(temp, grid[k][l]);\n }\n }\n\n tempRow.push(temp);\n }\n res.push(tempRow);\n }\n\n return res;\n};\n```\n# Approach: Sliding Window (Space Optimized)\n\n- We optimized the space by not creating a new `n x n` matrix but instead resizing our given input matrix `grid` in-place.\n\n# Complexity\n- Time complexity: O(n^2)\n\n- Space complexity: O(1)\n\n# Code\n```C++ []\n#pragma GCC optimize ("Ofast")\n#pragma GCC target ("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx")\n#pragma GCC optimize ("-ffloat-store")\n\nclass Solution {\npublic:\n vector<vector<int>> largestLocal(vector<vector<int>>& grid) {\n ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);\n int n = grid.size();\n\n for(int i = 1; i < n - 1; ++i) {\n for(int j = 1; j < n - 1; ++j) {\n int temp = 0;\n\n for(int k = i - 1; k <= i + 1; ++k) {\n for(int l = j - 1; l <= j + 1; ++l) {\n temp = max(temp, grid[k][l]);\n }\n }\n\n grid[i - 1][j - 1] = temp;\n }\n }\n\n grid.resize(n - 2);\n for (int i = 0; i < grid.size(); ++i) {\n grid[i].resize(n - 2);\n }\n\n return grid;\n }\n};\n```\n```python []\nclass Solution:\n def largestLocal(self, grid):\n n = len(grid)\n\n for i in range(1, n - 1):\n for j in range(1, n - 1):\n temp = 0\n\n for k in range(i - 1, i + 2):\n for l in range(j - 1, j + 2):\n temp = max(temp, grid[k][l])\n\n grid[i - 1][j - 1] = temp\n\n n = len(grid)\n grid = [row[:n-2] for row in grid[:n-2]]\n\n return grid\n```\n**And there you have it, my dear friends! Behold the magnificence of our newly crafted matrix, a testament to the beauty of mathematics! Ah, but let us not forget the thrill of discovery, the dance of numbers in their symphonic glory! Yohoho! Math may not have bones, but it sure does have soul!**\n\n\n\n\n\n | 99 | 5 | ['Array', 'Sliding Window', 'Matrix', 'C++', 'Java', 'Python3', 'JavaScript'] | 10 |
largest-local-values-in-a-matrix | Four Loops | four-loops-by-votrubac-tk1a | It\'s easy to overthink this problem. \n\nFor each output cell, we need to process 9 input cells, total 9 * (n - 2) * (n - 2) operations.\n\nC++\ncpp\nvector<ve | votrubac | NORMAL | 2022-08-14T04:11:04.949712+00:00 | 2022-08-14T04:20:59.730241+00:00 | 8,850 | false | It\'s easy to overthink this problem. \n\nFor each output cell, we need to process 9 input cells, total `9 * (n - 2) * (n - 2)` operations.\n\n**C++**\n```cpp\nvector<vector<int>> largestLocal(vector<vector<int>>& g) {\n int n = g.size();\n vector<vector<int>> res(n - 2, vector<int>(n - 2));\n for (int i = 0; i < n - 2; ++i)\n for (int j = 0; j < n - 2; ++j)\n for (int ii = i; ii < i + 3; ++ii)\n for (int jj = j; jj < j + 3; ++jj)\n res[i][j] = max(res[i][j], g[ii][jj]);\n return res;\n}\n``` | 72 | 1 | [] | 19 |
largest-local-values-in-a-matrix | ✅C++ | ✅Simple and efficient solution | ✅TC:O((n-2)^2) | c-simple-and-efficient-solution-tcon-22-1eaxn | Please upvote if it helps :)\n\nclass Solution \n{\npublic:\n vector<vector<int>> largestLocal(vector<vector<int>>& grid) \n {\n int n=grid.size(); | Yash2arma | NORMAL | 2022-08-14T04:03:19.484263+00:00 | 2022-08-15T06:16:08.957247+00:00 | 8,955 | false | **Please upvote if it helps :)**\n```\nclass Solution \n{\npublic:\n vector<vector<int>> largestLocal(vector<vector<int>>& grid) \n {\n int n=grid.size();\n vector<vector<int>> res(n-2, vector<int> (n-2));\n \n //find max of 3x3 grid centred around row (i+1) and column (j+1)\n\t\t//for eg:1 starting from (1,1) so (1,1),(1,2),(2,1),(2,2) will be the centre of 3x3 grid-1,2,3,4 respectively\n for(int i=1; i<=n-2; i++)\n {\n for(int j=1; j<=n-2; j++)\n {\n int maxi=0;\n maxi = max({maxi, grid[i-1][j-1], grid[i-1][j], grid[i-1][j+1]});\n maxi = max({maxi, grid[i][j-1], grid[i][j], grid[i][j+1]});\n maxi = max({maxi, grid[i+1][j-1], grid[i+1][j], grid[i+1][j+1]});\n \n res[i-1][j-1] = maxi;\n }\n }\n return res;\n }\n};\n``` | 41 | 3 | ['C', 'Matrix', 'C++'] | 7 |
largest-local-values-in-a-matrix | ✅BEATS 100% | ✅SUPER EASY | ✅CLEAN, CONCISE, OPTIMIZED | ✅MULTI LANG | ✅CODING MADE FUN | beats-100-super-easy-clean-concise-optim-qp2y | Submission SS :\n\n\n\n# Intuition : Sliding Window\n\n# Approach\nHere\'s a structured and concise breakdown:\n\n1. Calculate Size of Resulting 2D Array (ans): | arib21 | NORMAL | 2024-05-12T04:53:45.997197+00:00 | 2024-05-12T07:08:49.984172+00:00 | 6,964 | false | # Submission SS :\n\n\n\n# Intuition : Sliding Window\n\n# Approach\nHere\'s a structured and concise breakdown:\n\n1. **Calculate Size of Resulting 2D Array (`ans`)**:\n - Subtract 2 from the length of the input grid (`n`) to determine the size of the resulting array (`m`).\n - Initialize a 2D array `ans` with dimensions `(m) x (m)`.\n\n2. **Iterate Over Each Position in `ans`**:\n - Iterate from `(0, 0)` to `(m-1, m-1)`.\n - For each position `(i, j)` in `ans`, find the largest value in the corresponding 3x3 region of the input grid.\n\n3. **Find Largest Value in Each 3x3 Region**:\n - Call the `largestLocalUtil` method for each position `(i, j)` in `ans`.\n - In the `largestLocalUtil` method:\n - Initialize a variable `max` to 0.\n - Iterate over a 3x3 grid starting from position `(i, j)`.\n - Update `max` to hold the maximum value found in the region.\n - Return the maximum value found in the 3x3 region (`max`).\n\n4. **Store Maximum Values in `ans`**:\n - Populate `ans` with the maximum values found in each local 3x3 region of the input grid.\n\n5. **Return Resulting 2D Array `ans`**:\n - `ans` contains the largest values in each local 3x3 region of the input grid.\n\n# Complexity\n- Time complexity: $$O(n^2)$$\n\n- Space complexity: $$O((n-2)^2)$$ ~ $$O(n^2)$$, \n\n# Code\n``` Java []\nclass Solution {\n\n public int largestLocalUtil(int[][] grid, int x, int y) {\n int max = 0;\n \n for (int i = x ; i < x+3 ; i++) {\n for (int j = y ; j < y+3 ; j++) {\n max = Math.max(max, grid[i][j]);\n }\n }\n \n return max;\n }\n \n public int[][] largestLocal(int[][] grid) {\n int n = grid.length;\n \n int m = n-2;\n \n int[][] ans = new int[m][m];\n \n for (int i = 0 ; i < m ; i++) {\n for (int j = 0 ; j < m ; j++) {\n ans[i][j] = largestLocalUtil(grid, i, j);\n }\n }\n \n return ans;\n }\n}\n```\n``` C++ []\nclass Solution {\n\npublic:\n\n int largestLocalUtil(std::vector<std::vector<int>>& grid, int x, int y) {\n int max = 0;\n \n for (int i = x ; i < x+3 ; i++) {\n for (int j = y ; j < y+3 ; j++) {\n max = std::max(max, grid[i][j]);\n }\n }\n \n return max;\n }\n \n vector<vector<int>> largestLocal(vector<vector<int>>& grid) {\n int n = grid.size();\n \n int m = n-2;\n \n std::vector<std::vector<int>> ans(m, std::vector<int>(m, 0));\n \n for (int i = 0 ; i < m ; i++) {\n for (int j = 0 ; j < m ; j++) {\n ans[i][j] = largestLocalUtil(grid, i, j);\n }\n }\n \n return ans;\n }\n};\n```\n``` Python []\nclass Solution:\n\n def largestLocalUtil(self, grid, x, y):\n max_val = 0\n \n for i in range(x, x+3):\n for j in range(y, y+3):\n max_val = max(max_val, grid[i][j])\n \n return max_val\n \n def largestLocal(self, grid):\n n = len(grid)\n m = n - 2\n \n ans = [[0] * m for _ in range(m)]\n \n for i in range(m):\n for j in range(m):\n ans[i][j] = self.largestLocalUtil(grid, i, j)\n \n return ans\n\n``` \n\n**Space Optimized Approach :**\n* One can do the space optimization in C++ and Python by modifying the given matrix and resizing to n-2.\n\n**Complexity**\n* Space complexity: $$O(1)$$\n``` C++ []\nclass Solution {\n\npublic:\n\n int largestLocalUtil(vector<vector<int>>& grid, int x, int y) {\n int curMax = 0;\n \n for (int i = x-1 ; i <= x+1 ; i++) {\n for (int j = y-1 ; j <= y+1 ; j++) {\n curMax = max(curMax, grid[i][j]);\n }\n }\n \n return curMax;\n }\n \n vector<vector<int>> largestLocal(vector<vector<int>>& grid) {\n int n = grid.size();\n \n for (int i = 1 ; i < n-1 ; i++) {\n for (int j = 1 ; j < n-1 ; j++) {\n grid[i-1][j-1] = largestLocalUtil(grid, i, j);\n }\n }\n \n int m = n-2;\n \n grid.resize(m);\n \n for (int i = 0 ; i < m ; i++) grid[i].resize(m);\n \n return grid;\n }\n};\n```\n``` Python []\nclass Solution(object):\n\n def largestLocalUtil(self, grid, x, y):\n cur_max = 0\n \n for i in range(x-1, x+2):\n for j in range(y-1, y+2):\n cur_max = max(cur_max, grid[i][j])\n \n return cur_max\n \n def largestLocal(self, grid):\n n = len(grid)\n \n for i in range(1, n-1):\n for j in range(1, n-1):\n grid[i-1][j-1] = self.largestLocalUtil(grid, i, j)\n \n m = n-2\n \n grid = [row[:m] for row in grid[:m]]\n \n return grid\n \n```\n\n\n\n | 36 | 0 | ['Array', 'Math', 'C', 'Sliding Window', 'Matrix', 'Simulation', 'Python', 'Java', 'Python3'] | 5 |
largest-local-values-in-a-matrix | [Python3] simulation | python3-simulation-by-ye15-pl5h | Please pull this commit for solutions of weekly 306. \n\n\nclass Solution:\n def largestLocal(self, grid: List[List[int]]) -> List[List[int]]:\n n = l | ye15 | NORMAL | 2022-08-14T04:02:51.400454+00:00 | 2022-08-15T02:00:56.126678+00:00 | 4,853 | false | Please pull this [commit](https://github.com/gaosanyong/leetcode/commit/abc9891d642b2454c148af46a140ff3497f7ce3c) for solutions of weekly 306. \n\n```\nclass Solution:\n def largestLocal(self, grid: List[List[int]]) -> List[List[int]]:\n n = len(grid)\n ans = [[0]*(n-2) for _ in range(n-2)]\n for i in range(n-2): \n for j in range(n-2): \n ans[i][j] = max(grid[ii][jj] for ii in range(i, i+3) for jj in range(j, j+3))\n return ans \n``` | 30 | 0 | ['Python3'] | 11 |
largest-local-values-in-a-matrix | Reuse matrix grid Extra space O(1) MaxPooling vs Sliding window||3ms Beats 99.54% | reuse-matrix-grid-extra-space-o1-maxpool-ug08 | Intuition\n Describe your first thoughts on how to solve this problem. \nThat is the max computation for 3x3 submatrix.\nmax pool with 3x3 window & stride 1 wit | anwendeng | NORMAL | 2024-05-12T00:30:37.482326+00:00 | 2024-05-12T07:31:55.638705+00:00 | 3,715 | false | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nThat is the max computation for 3x3 submatrix.\nmax pool with 3x3 window & stride 1 without padding\n\n2nd approach using the sliding window to reduce the amount of computations with 3ms Beating 99.54%\n# Approach\n<!-- Describe your approach to solving the problem. -->\n[Please turn on English subtitles if necessary]\n[https://youtu.be/kiG_jJmFtzU?si=hmldrhvI7XTs0Qif](https://youtu.be/kiG_jJmFtzU?si=hmldrhvI7XTs0Qif)\nReuse the 2d vector grid\n\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n $$O(9n^2)\\to O(6n^2)$$ \n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\nextra: $O(1)$\n# C++ Code\n```\n#pragma GCC optimize("O3", "unroll-loops")\nclass Solution {\npublic:\n vector<vector<int>> largestLocal(vector<vector<int>>& grid) {\n const int n=grid.size();\n for(int i=1; i<n-1; i++){ \n for(int j=1; j<n-1; j++){\n for(int r=i-1; r<=i+1; r++)\n for( int c=j-1; c<=j+1; c++)\n grid[i-1][j-1]=max(grid[i-1][j-1], grid[r][c]);\n }\n grid[i-1].resize(n-2);\n }\n grid.resize(n-2);\n return grid;\n }\n};\n\n```\n# Python code\n```\nclass Solution:\n def largestLocal(self, grid: List[List[int]]) -> List[List[int]]:\n # max pool with 3x3 window & stride 1 without padding\n n=len(grid)\n ans=[[0]*(n-2) for _ in range(n-2)]\n for i in range(1, n-1):\n for j in range(1, n-1):\n ans[i-1][j-1]=0\n for r in range(i-1, i+2):\n for c in range (j-1, j+2):\n ans[i-1][j-1]=max(ans[i-1][j-1], grid[r][c])\n return ans\n \n```\n# 2nd C++ using sliding window reducing computations||3ms Beats 99.54%\nUse a sized 3 extra array `maxC` to compute the max for 3x1 submatrices. In each row, moving `j` & compute the next `maxC[(j+1)%3]`, i.e. sliding window, computational amount is reduced.\n```\n#pragma GCC optimize("O3", "unroll-loops")\nclass Solution {\npublic:\n static vector<vector<int>> largestLocal(vector<vector<int>>& grid) {\n const int n=grid.size();\n int maxC[3]={0};\n for(int i=1; i<n-1; i++){ \n for(int j=1; j<n-1; j++){\n if (j==1){\n maxC[0]=max({grid[i-1][0], grid[i][0], grid[i+1][0]});\n maxC[1]=max({grid[i-1][1], grid[i][1], grid[i+1][1]});\n }\n maxC[(j+1)%3]=max({grid[i-1][j+1], grid[i][j+1], grid[i+1][j+1]}); \n grid[i-1][j-1]=max({maxC[0], maxC[1], maxC[2]});\n }\n grid[i-1].resize(n-2);\n }\n grid.resize(n-2);\n return grid;\n }\n};\n\n\n``` | 23 | 1 | ['Sliding Window', 'Matrix', 'C++', 'Python3'] | 4 |
largest-local-values-in-a-matrix | ✅ C++ | ✅ 100% faster | Easy | Explained in comments. | c-100-faster-easy-explained-in-comments-0q91z | \nclass Solution {\npublic:\n vector<vector<int>> largestLocal(vector<vector<int>>& grid) {\n \n int n = grid.size();\n \n // Thi | avaneeshyadav | NORMAL | 2022-08-14T04:52:22.903911+00:00 | 2022-08-18T16:25:39.475909+00:00 | 4,277 | false | ```\nclass Solution {\npublic:\n vector<vector<int>> largestLocal(vector<vector<int>>& grid) {\n \n int n = grid.size();\n \n // This will be our final matrix of size (n-2)*(n-2)\n vector<vector<int>> ans(n-2,vector<int>(n-2));\n \n \n /* We call our \'maxIn3x3\' function from index [(0,0) to (n-2,n-2)) excluding (n-2) i.e upto (n-3).\n\t\tWe will keep storing value returned by this function in \'ans\' array at corresponding (i,j) co-ordinate during loop traversal. */\n for(int i=0;i<n-2;i++){\n for(int j=0;j<n-2;j++)\n ans[i][j] = maxIn3x3(grid, i, j);\n } \n \n // Happy return calculated ans matrix :)\n return ans;\n }\n \n \n // A function that takes a starting cordinate (i,j) and returns max value in 3x3 size matrix starting from this cordinate.\n int maxIn3x3(vector<vector<int>> &arr, int i, int j)\n\t{\n int maxVal = INT_MIN;\n \n // Start from the index (i,j) and iterate for 3x3 matrix starting from this index and keep track of maximum value.\n for(int x = i ; x < i+3 ; x++){\n for(int y = j; y < j+3 ; y++)\n maxVal = max(arr[x][y], maxVal);\n }\n\n // Happy return the maximum value of this 3x3 size matrix :)\n return maxVal;\n }\n \n};\n``` | 21 | 0 | ['C', 'C++'] | 5 |
largest-local-values-in-a-matrix | ✅Python || Easy Approach || Brute force | python-easy-approach-brute-force-by-chuh-nprg | \nclass Solution:\n def largestLocal(self, grid: List[List[int]]) -> List[List[int]]:\n\n n = len(grid)\n ans = []\n\n for i in range(n | chuhonghao01 | NORMAL | 2022-08-14T04:07:06.351409+00:00 | 2022-08-14T04:07:06.351447+00:00 | 3,345 | false | ```\nclass Solution:\n def largestLocal(self, grid: List[List[int]]) -> List[List[int]]:\n\n n = len(grid)\n ans = []\n\n for i in range(n - 2):\n res = []\n\n for j in range(n - 2):\n k = []\n k.append(grid[i][j])\n k.append(grid[i][j + 1])\n k.append(grid[i][j + 2])\n k.append(grid[i + 1][j])\n k.append(grid[i + 1][j + 1])\n k.append(grid[i + 1][j + 2])\n k.append(grid[i + 2][j])\n k.append(grid[i + 2][j + 1])\n k.append(grid[i + 2][j + 2])\n m = max(k)\n res.append(m)\n\n ans.append(res)\n \n return ans\n``` | 21 | 1 | ['Python', 'Python3'] | 8 |
largest-local-values-in-a-matrix | 🔥 🔥 🔥 Video Explanation | Easy to understand || Beats 100% of users || 4 Languages🔥 🔥 🔥 | video-explanation-easy-to-understand-bea-rmzh | Detailed Video Explanation Here\n\n\n\n\n# Intuition\n- Imagine you\'re given a large square garden divided into smaller plots arranged in a grid formation. Eac | bhanu_bhakta | NORMAL | 2024-05-12T00:08:55.981215+00:00 | 2024-05-12T23:35:05.505183+00:00 | 2,605 | false | **[Detailed Video Explanation Here](https://www.youtube.com/watch?v=O-HF5tFhgYw?sub_confirmation=1)**\n\n\n\n\n# Intuition\n- Imagine you\'re given a large square garden divided into smaller plots arranged in a grid formation. Each plot has a number assigned to it, representing the quality of the soil in that plot. Your task is to identify the best soil quality in every small, contiguous, 3x3 section of the garden.\n\n\n\n# Approach\n- **The Game Board:**\n**Grid:** This represents the garden. It\'s a 2D list where each element represents a plot in the garden.\n**Result Grid:** This will store the highest quality of soil found in each 3x3 section. It\'s slightly smaller than the original grid since the edges don\'t have enough neighboring plots to form a complete 3x3 section.\n**Gameplay:**\nSetup the Result Board: Before starting, you prepare a smaller board (result) that will eventually display the best soil quality scores from each relevant section of the garden. It\'s sized based on the original grid, but each dimension is reduced by 2 because the edges can\'t form a full 3x3 grid.\n\n**Scanning the Garden:**\n\nYou start from the top left of the garden and plan to move across and down, checking every possible 3x3 section.\nFor each position (starting top-left corner of a 3x3 section), you call upon a helper function findLargest.\nFinding the Treasure (findLargest function):\n\n**Objective:** From a given starting point, inspect all plots within a 3x3 area centered on that plot.\n\n**Procedure:** Check each of the 9 plots in this 3x3 section. Keep track of the highest quality score seen so far.\n\n**Outcome:** Return the highest score found.\n\n**Updating the Leaderboard (result grid):**\n\nAfter finding the highest score in a section, update your result board at the position corresponding to where you started scanning in that section.\n\n**Completing the Game:**\n\nContinue this process, scanning through all viable starting points on the grid.\n\nOnce every possible 3x3 section has been evaluated and the results have been recorded, the game ends.\n\n**Results:**\n\nThe result grid now contains the best soil quality score for each 3x3 section of the garden, ready to be used for whatever decisions need to be made next (like where to plant the most demanding crops).\n\n# Complexity\n- Time complexity:\nO(N^2)\n\n- Space complexity:\nO(N)\n\n# Code\n```Python []\nclass Solution:\n def largestLocal(self, grid: List[List[int]]) -> List[List[int]]:\n nRows, nCols = len(grid), len(grid[0])\n\n result = [[0] * (nCols - 2) for _ in range(nRows - 2)]\n\n def findLargest(row, col):\n best = grid[row][col]\n for i in range(row, row + 3):\n for j in range(col, col + 3):\n best = max(best, grid[i][j])\n return best\n\n for row in range(nCols - 2):\n for col in range(nRows - 2):\n result[row][col] = findLargest(row, col)\n return result\n\n```\n```C++ []\nclass Solution {\npublic:\n vector<vector<int>> largestLocal(vector<vector<int>>& grid) {\n int nRows = grid.size();\n int nCols = grid[0].size();\n\n std::vector<std::vector<int>> result(nRows - 2,\n std::vector<int>(nCols - 2));\n\n auto findLargest = [&grid](int row, int col) {\n int best = grid[row][col];\n for (int i = row; i < row + 3; ++i) {\n for (int j = col; j < col + 3; ++j) {\n best = std::max(best, grid[i][j]);\n }\n }\n return best;\n };\n\n for (int row = 0; row < nRows - 2; ++row) {\n for (int col = 0; col < nCols - 2; ++col) {\n result[row][col] = findLargest(row, col);\n }\n }\n\n return result;\n }\n};\n```\n```Java []\nclass Solution {\n public int[][] largestLocal(int[][] grid) {\n int nRows = grid.length;\n int nCols = grid[0].length;\n\n int[][] result = new int[nRows - 2][nCols - 2];\n\n for (int row = 0; row < nRows - 2; row++) {\n for (int col = 0; col < nCols - 2; col++) {\n result[row][col] = findLargest(grid, row, col);\n }\n }\n\n return result;\n }\n\n private int findLargest(int[][] grid, int row, int col) {\n int best = grid[row][col];\n for (int i = row; i < row + 3; i++) {\n for (int j = col; j < col + 3; j++) {\n best = Math.max(best, grid[i][j]);\n }\n }\n return best;\n }\n}\n```\n```Javascript []\n/**\n * @param {number[][]} grid\n * @return {number[][]}\n */\nvar largestLocal = function (grid) {\n const nRows = grid.length;\n const nCols = grid[0].length;\n\n let result = new Array(nRows - 2).fill().map(() => new Array(nCols - 2).fill(0));\n\n for (let row = 0; row < nRows - 2; row++) {\n for (let col = 0; col < nCols - 2; col++) {\n result[row][col] = findLargest(grid, row, col);\n }\n }\n\n return result;\n};\n\nfunction findLargest(grid, row, col) {\n let best = grid[row][col];\n for (let i = row; i < row + 3; i++) {\n for (let j = col; j < col + 3; j++) {\n best = Math.max(best, grid[i][j]);\n }\n }\n return best;\n}\n```\n**Please Upvote**\n\n | 19 | 3 | ['Matrix', 'Java', 'Python3', 'JavaScript'] | 3 |
largest-local-values-in-a-matrix | ✅ JavaScript || 👁 explanation of all cases || Easy to understand | javascript-explanation-of-all-cases-easy-7qic | \n\n# Complexity\n- Time complexity:\n O(n^2)\n Add your time complexity here, e.g. O(n) \n\n- Space complexity:\n O(n^2) \n Add your space complexity h | hasanalsayyed651998 | NORMAL | 2022-11-30T10:37:48.182875+00:00 | 2022-12-06T14:13:13.554575+00:00 | 1,171 | false | \n\n# Complexity\n- Time complexity:\n O(n^2)\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:\n O(n^2) \n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Explanation\ncreate (n-2,n-2) new empty matrix\niterate throw whole grid one time and each time check the current 3x3 matrix max number and save it in the new matrix\n\nHere are all the cases of given **Example 1**\n\n\n\n\n\n\n\n\n# Code\n```\n/**\n * @param {number[][]} grid\n * @return {number[][]}\n */\nvar largestLocal = function(grid) {\n // declare (n-2 x n-2) matrix\n const matrix = new Array(grid.length -2).fill(0)\n .map(() => new Array(grid[0].length-2).fill(0));\n \n for (let i=0; i< grid[i].length -2 ; i++){\n for(let j=0; j<grid.length -2 ;j++){\n\n //find the max in each 3x3 martix\n matrix[i][j] = Math.max( \n grid[i][j], grid[i][j+1], grid[i][j+2],\n grid[i+1][j], grid[i+1][j+1], grid[i+1][j+2],\n grid[i+2][j], grid[i+2][j+1], grid[i+2][j+2]\n );\n \n }\n }\n\n return matrix;\n};\n``` | 17 | 0 | ['Matrix', 'JavaScript'] | 4 |
largest-local-values-in-a-matrix | Brutal Force | brutal-force-by-fllght-kynq | Java\njava\npublic int[][] largestLocal(int[][] grid) {\n int[][] result = new int[grid.length - 2][grid.length - 2];\n\n for (int i = 0; i < resu | FLlGHT | NORMAL | 2022-08-14T06:55:23.952619+00:00 | 2023-05-12T05:23:48.032519+00:00 | 2,996 | false | #### Java\n```java\npublic int[][] largestLocal(int[][] grid) {\n int[][] result = new int[grid.length - 2][grid.length - 2];\n\n for (int i = 0; i < result.length; ++i) {\n for (int j = 0; j < result.length; ++j) {\n\t\t\t\n int largest = Integer.MIN_VALUE;\n for (int row = i; row < i + 3; ++row) {\n for (int column = j; column < j + 3; ++column) {\n largest = Math.max(largest, grid[row][column]);\n }\n }\n result[i][j] = largest;\n }\n }\n return result;\n }\n```\n\n#### C++\n\n```c++\nvector<vector<int>> largestLocal(vector<vector<int>>& grid) {\n vector<vector<int>> result(grid.size() - 2, vector<int>(grid.size() - 2));\n\n for (int i = 0; i < result.size(); ++i) {\n for (int j = 0; j < result.size(); ++j) {\n\t\t\t\n int largest = INT_MIN;\n for (int row = i; row < i + 3; ++row) {\n for (int column = j; column < j + 3; ++column) {\n largest = max(largest, grid[row][column]);\n }\n }\n result[i][j] = largest;\n }\n }\n return result;\n }\n```\n\nMy repositories with leetcode problems solving - [Java](https://github.com/FLlGHT/algorithms/tree/master/j-algorithms/src/main/java), [C++](https://github.com/FLlGHT/algorithms/tree/master/c-algorithms/src/main/c%2B%2B) | 17 | 0 | ['C', 'Java'] | 4 |
largest-local-values-in-a-matrix | Simple | O(n^2) | Just traverse in matrix and optimally stored values in answer vector | Refer Code | simple-on2-just-traverse-in-matrix-and-o-9q2n | \n\n# Code\n\nclass Solution {\npublic:\n vector<vector<int>> largestLocal(vector<vector<int>>& grid) {\n \n int n = grid.size();\n int | YASH_SHARMA_ | NORMAL | 2024-05-12T05:53:59.178652+00:00 | 2024-05-12T05:53:59.178676+00:00 | 1,725 | false | \n\n# Code\n```\nclass Solution {\npublic:\n vector<vector<int>> largestLocal(vector<vector<int>>& grid) {\n \n int n = grid.size();\n int m = grid[0].size();\n\n vector<vector<int>> ans;\n\n for(int i = 0 ; i < n-2 ; i++)\n {\n vector<int> temp;\n for(int j = 0 ; j < m-2 ; j++)\n {\n int v1 = max({grid[i][j] , grid[i][j+1] , grid[i][j+2]});\n int v2 = max({grid[i+1][j] , grid[i+1][j+1] , grid[i+1][j+2]});\n int v3 = max({grid[i+2][j] , grid[i+2][j+1] , grid[i+2][j+2]});\n\n int val = max({v1 , v2 , v3});\n\n temp.push_back(val);\n }\n ans.push_back(temp);\n }\n\n return ans;\n }\n};\n``` | 16 | 1 | ['Array', 'Matrix', 'C++'] | 2 |
largest-local-values-in-a-matrix | Python3 || 5 lines, iteration || T/S: 92% / 71% | python3-5-lines-iteration-ts-92-71-by-sp-k1ux | Pretty much explains itself.\n\nclass Solution:\n def largestLocal(self, grid: List[List[int]]) -> List[List[int]]:\n\n n = len(grid)-2\n ans = | Spaulding_ | NORMAL | 2022-08-14T17:34:55.889287+00:00 | 2024-06-13T21:43:24.935829+00:00 | 1,002 | false | Pretty much explains itself.\n```\nclass Solution:\n def largestLocal(self, grid: List[List[int]]) -> List[List[int]]:\n\n n = len(grid)-2\n ans = [[0]*n for _ in range(n)]\n\n for i,j in product(range(n),range(n)):\n ans[i][j] = max(grid[I][J] for I,J in\n product(range(i,i+3),range(j,j+3)))\n\n return ans\n```\n\n[https://leetcode.com/problems/largest-local-values-in-a-matrix/submissions/1255744400/](https://leetcode.com/problems/largest-local-values-in-a-matrix/submissions/1255744400/)\n\nI could be wrong, but I think that time complexity is *O*(*N* ^2) and space complexity is *O*(*N* ^2), in which *M* ~ `len(grid)` and *N* ~ `len(grid[0])`. | 14 | 0 | ['Python', 'Python3'] | 3 |
largest-local-values-in-a-matrix | 🔥 [Python3] Short brute-force, using list comprehension | python3-short-brute-force-using-list-com-v90q | python3 []\nclass Solution:\n def largestLocal(self, grid: List[List[int]]) -> List[List[int]]:\n N = len(grid)-2\n res = [[0] * N for _ in ran | yourick | NORMAL | 2023-05-11T16:57:34.439971+00:00 | 2023-08-09T23:03:04.204959+00:00 | 1,474 | false | ```python3 []\nclass Solution:\n def largestLocal(self, grid: List[List[int]]) -> List[List[int]]:\n N = len(grid)-2\n res = [[0] * N for _ in range(N)]\n for i,j in product(range(N), range(N)):\n res[i][j] = max(grid[r][c] for r, c in product(range(i, i+3), range(j, j+3)))\n\n return res\n```\n```python3 []\nclass Solution:\n def largestLocal(self, grid: List[List[int]]) -> List[List[int]]:\n N = len(grid)-2\n res = [[0] * N for _ in range(N)]\n for i in range(N):\n for j in range(N):\n res[i][j] = max([grid[r][c] for r in range(i, i+3) for c in range(j, j+3)])\n\n return res\n``` | 13 | 0 | ['Matrix', 'Python', 'Python3'] | 5 |
largest-local-values-in-a-matrix | 100% Fast Java.. Easy to Understand Full Explaination End to End | 100-fast-java-easy-to-understand-full-ex-qakb | FULL CODE\n\n public int[][] largestLocal(int[][] grid) {\n int n = grid.length;\n int [][] res = new int [n-2][n-2];\n for(int i=0; i<n-2;i++) | devloverabhi | NORMAL | 2022-08-14T06:03:00.064531+00:00 | 2022-08-14T06:03:00.064574+00:00 | 2,224 | false | # **FULL CODE**\n```\n public int[][] largestLocal(int[][] grid) {\n int n = grid.length;\n int [][] res = new int [n-2][n-2];\n for(int i=0; i<n-2;i++){\n for(int j=0; j<n-2;j++){\n res[i][j]= getMaxVal(i,j,grid);\n }\n }\n\treturn res;\n \n }\n\nint getMaxVal(int i, int j, int[][] grid)\n{\n int Max = Integer.MIN_VALUE;\n for(int a= i; a<=i+2; a++){\n for(int b= j; b<=j+2; b++){\n Max = Math.max(grid[a][b],Max); }\n } \n return Max;\n}\n```\n# **EXPLAINATION**\n1. Initialize the empty array with size `n-2` or `grid.length-2`. i.e. `int [][] res = new int [n-2][n-2];`\n2. loop the result array elements -> res i.e. loop till i and j till n-2, we are iterating this because we will store the Max element of each quadrant (will discuss later).\n3. create a method getMaxVal, take input the values of i and j from the loop and also the original array.\n4. we will use this method to the max value from the each quadrant, and has we know by the question the max quadrant value is 3 therefore we keep this mind while iteration i and j till only i+2 and j+2 also while iteration we will calculate the max of each quadrant.\n5. after getting the max we will return. max value will be set in the res array. | 11 | 0 | ['Java'] | 4 |
largest-local-values-in-a-matrix | 💯JAVA Solution Explained in HINDI | java-solution-explained-in-hindi-by-the_-uocd | https://youtu.be/8oOjpEJJax4\n\nFor explanation, please watch the above video and do like, share and subscribe the channel. \u2764\uFE0F Also, please do upvote | The_elite | NORMAL | 2024-05-12T04:00:34.161764+00:00 | 2024-05-12T04:00:34.161794+00:00 | 1,518 | false | https://youtu.be/8oOjpEJJax4\n\nFor explanation, please watch the above video and do like, share and subscribe the channel. \u2764\uFE0F Also, please do upvote the solution if you liked it.\n\n# Subscribe:- [ReelCoding](https://www.youtube.com/@reelcoding?sub_confirmation=1)\n\nSubscribe Goal:- 400\nCurrent Subscriber:- 359\n\n# Complexity\n- Time complexity: O(n^2)\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity: O(n^2)\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```\nclass Solution {\n public int[][] largestLocal(int[][] grid) {\n \n int n = grid.length;\n\n int maxLocal[][] = new int[n-2][n-2];\n\n for(int i = 0; i < n - 2; i++) {\n for(int j = 0; j < n - 2; j++) {\n maxLocal[i][j] = findMax(grid, i, j);\n }\n }\n return maxLocal;\n }\n\n private int findMax(int grid[][], int x, int y) {\n int maxEle = 0;\n for(int i = x; i < x + 3; i++) {\n for(int j = y; j < y + 3; j++) {\n maxEle = Math.max(maxEle, grid[i][j]);\n }\n }\n return maxEle;\n }\n}\n``` | 10 | 0 | ['Java'] | 1 |
largest-local-values-in-a-matrix | ✅Detailed Explanation🔥🔥Extremely Simple and effective🔥O(n^2) Time and O(n) Space🔥🔥🔥 | detailed-explanationextremely-simple-and-17ym | \uD83C\uDFAFProblem Explanation:\nGiven an n by n matrix, the task is to find the maximum for each 3 by 3 square in this matrix.\n\n# \uD83D\uDCE5Input:\n- grid | heir-of-god | NORMAL | 2024-05-12T09:06:55.194732+00:00 | 2024-05-12T09:14:09.495535+00:00 | 440 | false | # \uD83C\uDFAFProblem Explanation:\nGiven an n by n matrix, the task is to find the maximum for each 3 by 3 square in this matrix.\n\n# \uD83D\uDCE5Input:\n- grid: integer matrix n by n\n\n# \uD83D\uDCE4Output:\nMatrix n - 2 by n - 2 which contain maximums for each square 3 by 3\n\n# \uD83E\uDD14 Intuition\n- Okay, firstly just put up with the fact that this stupid question just doesn\'t have effective approach and brute force just ok\n- I have no idea about what to write here, because all intuition is just **"traverse matrix"**\n\n# \uD83E\uDDE0 Approach\n- Okay, as was said, this problem has nothing but a brute force solution. That is, we will go through each cell, which is potentially the center of some three by three square and will look for the maximum among it and all its neighbors, for this we will write an auxiliary function ```get_maximum```.\n- Further, as stated in the description itself, the centers of the square we need are located from 1 to n - 1 index (not inclusive), that is, the last index that we will see is n - 2, that is, the penultimate index in the matrix.\n- I tried to optimize space complexity, so I tried to reuse the original matrix, but no matter how we get out, we need to reduce its size by 2 - since in Python this can be done mainly by slices, that\u2019s what I did. However, in fact, at the end, we still create a new matrix, so the space complexity should not change and remain O(n^2)\n\n# \uD83D\uDCD2 Complexity\n- \u23F0 Time complexity: O(n^2), we traverse through every of n^2 elements one time, checking also 8 elements around on every step but it\'s still O(n^2) \n- \uD83E\uDDFA Space complexity: O(n^2), we "recreate" the matrix to fit our sizes, but I believe that in some languages this can be done in O(1)\n\n# \uD83E\uDDD1\u200D\uD83D\uDCBB Code\n```\nclass Solution:\n def largestLocal(self, grid: list[list[int]]) -> list[list[int]]:\n n: int = len(grid)\n\n def get_maximum(row_i, col_i) -> int:\n res = 0\n for row in range(row_i - 1, row_i + 2, 1):\n for col in range(col_i - 1, col_i + 2, 1):\n if grid[row][col] > res:\n res = grid[row][col]\n return res\n\n for row_ind in range(1, n - 1):\n for col_ind in range(1, n - 1):\n grid[row_ind - 1][col_ind - 1] = get_maximum(row_ind, col_ind)\n\n grid = [\n row_ind[: n - 2] for row_ind in grid[: n - 2]\n ] # every time we put maximum one to the left and upper, so our result will be from 0 to n - 2 indexes\n\n return grid\n```\n\n## \uD83D\uDCA1I encourage you to check out [my profile](https://leetcode.com/heir-of-god/) and [Project-S](https://github.com/Heir-of-God/Project-S) project for detailed explanations and code for different problems (not only Leetcode). Happy coding and learning! \uD83D\uDCDA\n\n## If you have any doubts or questions feel free to ask them in comments. I will be glad to help you with understanding\u2764\uFE0F\u2764\uFE0F\u2764\uFE0F\n\n\n | 9 | 0 | ['Array', 'Sliding Window', 'Matrix', 'Simulation', 'Python', 'Python3'] | 6 |
largest-local-values-in-a-matrix | Beginner friendly [Java/JavaScript] Solutuion | beginner-friendly-javajavascript-solutui-rvbs | Java\n\nclass Solution {\n int count = 2;\n public int[][] largestLocal(int[][] grid) {\n int n = grid.length;\n int[][] arr = new int[n-1][ | HimanshuBhoir | NORMAL | 2022-08-18T06:52:13.801777+00:00 | 2022-08-20T01:53:28.492213+00:00 | 2,094 | false | **Java**\n```\nclass Solution {\n int count = 2;\n public int[][] largestLocal(int[][] grid) {\n int n = grid.length;\n int[][] arr = new int[n-1][n-1];\n for(int i=0; i<arr.length; i++){\n for(int j=0; j<arr.length; j++){\n arr[i][j] = Math.max(grid[i][j], Math.max(grid[i][j+1], Math.max(grid[i+1][j], grid[i+1][j+1])));\n }\n }\n return --count == 0 ? arr : largestLocal(arr);\n }\n}\n```\n**JavaScript**\n```\nvar largestLocal = function(grid, count = 2) {\n let n = grid.length\n let arr = []\n for(let i=0; i<n-1; i++) arr[i] = []\n for(let i=0; i<arr.length; i++){\n for(let j=0; j<arr.length; j++){\n arr[i][j] = Math.max(grid[i][j], Math.max(grid[i][j+1], Math.max(grid[i+1][j], grid[i+1][j+1])))\n }\n }\n return --count == 0 ? arr : largestLocal(arr, count)\n};\n``` | 9 | 0 | ['Java', 'JavaScript'] | 1 |
largest-local-values-in-a-matrix | Python | Easy | python-easy-by-khosiyat-7n0t | see the Successfully Accepted Submission\n\n# Code\n\nclass Solution:\n def largestLocal(self, grid: List[List[int]]) -> List[List[int]]:\n n = len(gr | Khosiyat | NORMAL | 2024-05-12T04:38:11.818023+00:00 | 2024-05-12T04:38:11.818050+00:00 | 418 | false | [see the Successfully Accepted Submission](https://leetcode.com/problems/largest-local-values-in-a-matrix/submissions/1255807734/?source=submission-ac)\n\n# Code\n```\nclass Solution:\n def largestLocal(self, grid: List[List[int]]) -> List[List[int]]:\n n = len(grid)\n result = []\n\n for i in range(n - 2):\n row = []\n for j in range(n - 2):\n submatrix = [grid[x][j:j+3] for x in range(i, i+3)]\n max_val = max(max(sub) for sub in submatrix)\n row.append(max_val)\n result.append(row)\n\n return result\n \n```\n | 8 | 0 | ['Python3'] | 1 |
largest-local-values-in-a-matrix | ✅ [Python] Two loop solution | python-two-loop-solution-by-amikai-4zt4 | \nclass Solution:\n def largestLocal(self, grid: List[List[int]]) -> List[List[int]]:\n n = len(grid)\n matrix = [[1]* (n-2) for i in range(n-2 | amikai | NORMAL | 2022-08-14T04:02:48.107221+00:00 | 2022-08-14T04:05:08.352494+00:00 | 1,864 | false | ```\nclass Solution:\n def largestLocal(self, grid: List[List[int]]) -> List[List[int]]:\n n = len(grid)\n matrix = [[1]* (n-2) for i in range(n-2)]\n for i in range(1, n - 1):\n for j in range(1, n - 1):\n matrix[i-1][j-1] = max(grid[i-1][j-1], grid[i-1][j], grid[i-1][j+1],\n grid[i][j-1], grid[i][j], grid[i][j+1],\n grid[i+1][j-1], grid[i+1][j], grid[i+1][j+1])\n return matrix\n```\n | 8 | 0 | ['Python', 'Python3'] | 2 |
largest-local-values-in-a-matrix | simple two pass solution with explanation | simple-two-pass-solution-with-explanatio-qrk9 | Intuition\n- We are using divide and conquer to solve this problem. \n- The idea is first find max for each column then find max for each row on grid obtained f | anupsingh556 | NORMAL | 2024-05-12T08:58:05.441457+00:00 | 2024-05-12T09:03:58.584761+00:00 | 391 | false | # Intuition\n- We are using divide and conquer to solve this problem. \n- The idea is first find max for each column then find max for each row on grid obtained from previos result\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n- Get rowMax grid by taking max of 3 subsequent element. \n- We can use sliding window for this but window size is always 3 so we are taking 3 elements in each iteration and finding thier max\n- Repeat same operation but change row to column and set max value in result grid.\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity: O(n*n) we are traversing grid twice\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity: O(n*n) needed for storing row max and result\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n\n```c++ []\nclass Solution {\npublic:\n vector<vector<int>> largestLocal(vector<vector<int>>& g) {\n int n = g.size();\n vector<vector<int>> rowMax(n);\n for(int i=0;i<n;i++) {\n for(int j=0;j<(n-2);j++) {\n int currMax = max(g[i][j], max(g[i][j+1], g[i][j+2]));\n rowMax[i].push_back(currMax);\n }\n }\n\n vector<vector<int>> res(n-2, vector<int>(n-2, 0));\n for(int j=0;j<(n-2);j++) {\n for(int i=0;i<(n-2);i++) {\n int currMax = max(rowMax[i][j], max(rowMax[i+1][j], rowMax[i+2][j]));\n res[i][j]=currMax;\n }\n }\n return res;\n }\n};\n```\n```go []\n\nfunc largestLocal(g [][]int) [][]int {\n\tn := len(g)\n\trowMax := make([][]int, n)\n\tfor i := 0; i < n; i++ {\n\t\trowMax[i] = make([]int, n-2)\n\t\tfor j := 0; j < n-2; j++ {\n\t\t\tcurrMax := max(g[i][j], max(g[i][j+1], g[i][j+2]))\n\t\t\trowMax[i][j] = currMax\n\t\t}\n\t}\n\n\tres := make([][]int, n-2)\n\tfor i := 0; i < n-2; i++ {\n\t\tres[i] = make([]int, n-2)\n\t\tfor j := 0; j < n-2; j++ {\n\t\t\tcurrMax := max(rowMax[i][j], max(rowMax[i+1][j], rowMax[i+2][j]))\n\t\t\tres[i][j] = currMax\n\t\t}\n\t}\n\treturn res\n}\n```\n```python []\nclass Solution(object):\n def largestLocal(self, g):\n n = len(g)\n row_max = []\n for i in range(n):\n row_max.append([])\n for j in range(n - 2):\n curr_max = max(g[i][j], g[i][j + 1], g[i][j + 2])\n row_max[i].append(curr_max)\n\n res = [[0] * (n - 2) for _ in range(n - 2)]\n for j in range(n - 2):\n for i in range(n - 2):\n curr_max = max(row_max[i][j], row_max[i + 1][j], row_max[i + 2][j])\n res[i][j] = curr_max\n return res\n```\n | 7 | 0 | ['C++'] | 2 |
largest-local-values-in-a-matrix | Easy python solution using 2 for loops | easy-python-solution-using-2-for-loops-b-mpsu | Intuition\nTo find themaximum value in every contiguous 3X3 matrix we iterate over the each cell in the grid except the last two rows and columns. \nFor each c | k_chandrika | NORMAL | 2024-05-12T00:30:55.038817+00:00 | 2024-05-12T00:30:55.038834+00:00 | 506 | false | # Intuition\nTo find themaximum value in every contiguous 3X3 matrix we iterate over the each cell in the grid except the last two rows and columns. \nFor each cell we consider 3X3 submatrix centred around that cell and find the maximum value within it\n\n# Approach\n1. Iterating over grid cells:\n - we iterate over each cell in the grid except the last two rows and columns because we need a 3X3 submatrix centred around each cell, and those cells on the borders don\'t have enough space for submatrices.\n2. Finding maximum value in submatrix:\n - For each cell (\'i,j\') in the grid, we extract the 3X3 submatrix cnetred around it using list slicing. \n - Then, we find the maximum value within its submatrix using max function.\n3. Constructing resulting matrix:\n - We store the maximum value found for each submatrix in the resulting matrix \'res\'.\n4. Returning the result\n\n# Complexity\n- Time complexity: *******O(n^2)*******\n\n\n- Space complexity: *O(n^2)*\n\n# Code\n```\nclass Solution:\n def largestLocal(self, grid: List[List[int]]) -> List[List[int]]:\n #initialize an empty list to store the resulting matrix\n res = []\n\n #iterate over each row of the grid, leaving last two rows\n for i in range(len(grid)-2):\n\n #append an empty list to res for the current row\n res.append([])\n\n #iterate over each column of the grid, leaaving last two columns \n for j in range(len(grid[0])-2):\n\n #extract the curretn 3*3 submatrix and find the maximum value within it\n max_val = max(\n grid[i][j:j+3]+ #top row of submatrix\n grid[i+1][j:j+3]+ #middle row of the submatrix\n grid[i+2][j:j+3] #bottom row of the submatrix\n )\n\n #append the max_val to the current row of the res matrix\n res[i].append(max_val)\n \n #return resulting matrix\n return res\n``` | 7 | 0 | ['Python3'] | 2 |
largest-local-values-in-a-matrix | Python Elegant & Short | 100% faster | python-elegant-short-100-faster-by-kyryl-cp9q | \n\n\n\nclass Solution:\n\t"""\n\tTime: O(n^2)\n\tMemory: O(1)\n\t"""\n\n\tdef largestLocal(self, grid: List[List[int]]) -> List[List[int]]:\n\t\tn = len(grid | Kyrylo-Ktl | NORMAL | 2022-08-15T08:59:45.373570+00:00 | 2022-09-30T13:27:00.523359+00:00 | 2,114 | false | \n\n\n```\nclass Solution:\n\t"""\n\tTime: O(n^2)\n\tMemory: O(1)\n\t"""\n\n\tdef largestLocal(self, grid: List[List[int]]) -> List[List[int]]:\n\t\tn = len(grid)\n\t\treturn [[self.local_max(grid, r, c, 1) for c in range(1, n - 1)] for r in range(1, n - 1)]\n\n\t@staticmethod\n\tdef local_max(grid: List[List[int]], row: int, col: int, radius: int) -> int:\n\t\treturn max(\n\t\t\tgrid[r][c]\n\t\t\tfor r in range(row - radius, row + radius + 1)\n\t\t\tfor c in range(col - radius, col + radius + 1)\n\t\t)\n```\n\nIf you like this solution remember to **upvote it** to let me know.\n\n | 7 | 0 | ['Python', 'Python3'] | 2 |
largest-local-values-in-a-matrix | ✅ JavaScript | Easy | javascript-easy-by-thakurballary-dbod | \n/**\n * @param {number[][]} grid\n * @return {number[][]}\n */\nvar largestLocal = function(grid) {\n const ans = [];\n \n for (let r = 0; r < gr | thakurballary | NORMAL | 2022-08-14T04:04:58.862711+00:00 | 2022-08-26T16:09:12.903585+00:00 | 886 | false | ```\n/**\n * @param {number[][]} grid\n * @return {number[][]}\n */\nvar largestLocal = function(grid) {\n const ans = [];\n \n for (let r = 0; r < grid.length - 2; r++) {\n const row = [];\n for (let c = 0; c < grid[r].length - 2; c++) {\n row.push(Math.max(\n grid[r][c], grid[r][c + 1], grid[r][c + 2],\n grid[r + 1][c], grid[r + 1][c + 1], grid[r + 1][c + 2],\n grid[r + 2][c], grid[r + 2][c + 1], grid[r + 2][c + 2]\n ));\n }\n ans.push(row);\n }\n\n return ans;\n};\n``` | 7 | 0 | ['Matrix', 'JavaScript'] | 2 |
largest-local-values-in-a-matrix | C++||SLIDING WINDOW|| | csliding-window-by-sujalgupta09-2jgf | \n\n# Code\n\nclass Solution {\npublic:\n vector<vector<int>> largestLocal(vector<vector<int>>& grid) {\n int n = grid.size();\n\n for(int i = | sujalgupta09 | NORMAL | 2024-05-12T06:14:30.695893+00:00 | 2024-05-12T06:14:30.695929+00:00 | 574 | false | \n\n# Code\n```\nclass Solution {\npublic:\n vector<vector<int>> largestLocal(vector<vector<int>>& grid) {\n int n = grid.size();\n\n for(int i = 1; i < n - 1; ++i) {\n for(int j = 1; j < n - 1; ++j) {\n int temp = 0;\n\n for(int k = i - 1; k <= i + 1; ++k) {\n for(int l = j - 1; l <= j + 1; ++l) {\n temp = max(temp, grid[k][l]);\n }\n }\n\n grid[i - 1][j - 1] = temp;\n }\n }\n\n grid.resize(n - 2);\n for (int i = 0; i < grid.size(); ++i) {\n grid[i].resize(n - 2);\n }\n\n return grid;\n }\n};\n``` | 6 | 0 | ['Array', 'Matrix', 'C++'] | 0 |
largest-local-values-in-a-matrix | ✅Easy✨||C++|| Beats 100% || With Explanation || | easyc-beats-100-with-explanation-by-olak-qivl | Intuition\n Describe your first thoughts on how to solve this problem. \nImagine you\'re given a large square garden divided into smaller plots arranged in a gr | olakade33 | NORMAL | 2024-05-12T04:42:18.080051+00:00 | 2024-05-12T04:42:18.080069+00:00 | 2,702 | false | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nImagine you\'re given a large square garden divided into smaller plots arranged in a grid formation. Each plot has a number assigned to it, representing the quality of the soil in that plot. Your task is to identify the best soil quality in every small, contiguous, 3x3 section of the garden.\n# Approach\n<!-- Describe your approach to solving the problem. -->\n1. The Game Board:\n. Grid: This represents the garden. It\'s a 2D list where each element represents a plot in the garden.\n. Result Grid: This will store the highest quality of soil found in each 3x3 section. It\'s slightly smaller than the original grid since the edges don\'t have enough neighboring plots to form a complete 3x3 section.\n. Gameplay:\nSetup the Result Board: Before starting, you prepare a smaller board (result) that will eventually display the best soil quality scores from each relevant section of the garden. It\'s sized based on the original grid, but each dimension is reduced by 2 because the edges can\'t form a full 3x3 grid.\n\n2. Scanning the Garden:\n\n . You start from the top left of the garden and plan to move across and down, checking every possible 3x3 section.\n. For each position (starting top-left corner of a 3x3 section), you call upon a helper function findLargest.\nFinding the Treasure (findLargest function):\n\n3. Objective: From a given starting point, inspect all plots within a 3x3 area centered on that plot.\n\n4. Procedure: Check each of the 9 plots in this 3x3 section. Keep track of the highest quality score seen so far.\n\n5. Outcome: Return the highest score found.\n\n6. Updating the Leaderboard (result grid):\n\n. After finding the highest score in a section, update your result board at the position corresponding to where you started scanning in that section.\n\n7. Completing the Game:\n\n. Continue this process, scanning through all viable starting points on the grid.\n\n. Once every possible 3x3 section has been evaluated and the results have been recorded, the game ends.\n\n8. Results:\n\n. The result grid now contains the best soil quality score for each 3x3 section of the garden, ready to be used for whatever decisions need to be made next (like where to plant the most demanding crops).\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```\nclass Solution {\npublic:\n vector<vector<int>> largestLocal(vector<vector<int>>& grid) {\n int nRows = grid.size();\n int nCols = grid[0].size();\n\n std::vector<std::vector<int>> result(nRows - 2,\n std::vector<int>(nCols - 2));\n\n auto findLargest = [&grid](int row, int col) {\n int best = grid[row][col];\n for (int i = row; i < row + 3; ++i) {\n for (int j = col; j < col + 3; ++j) {\n best = std::max(best, grid[i][j]);\n }\n }\n return best;\n };\n\n for (int row = 0; row < nRows - 2; ++row) {\n for (int col = 0; col < nCols - 2; ++col) {\n result[row][col] = findLargest(row, col);\n }\n }\n\n return result;\n }\n};\n``` | 6 | 0 | ['C++'] | 0 |
largest-local-values-in-a-matrix | [Python] Easy Solution | Beat 98% | python-easy-solution-beat-98-by-kg-profi-xb21 | \n# Code\n\nclass Solution:\n def largestLocal(self, grid: List[List[int]]) -> List[List[int]]:\n n = len(grid)\n result = []\n \n | KG-Profile | NORMAL | 2024-05-12T02:24:22.938487+00:00 | 2024-05-12T02:25:16.457319+00:00 | 62 | false | \n# Code\n```\nclass Solution:\n def largestLocal(self, grid: List[List[int]]) -> List[List[int]]:\n n = len(grid)\n result = []\n \n for i in range(1, n - 1): # Skip the first and last rows\n row_result = []\n for j in range(1, n - 1): # Skip the first and last columns\n max_value = max(\n grid[i - 1][j - 1], grid[i - 1][j], grid[i - 1][j + 1],\n grid[i][j - 1], grid[i][j], grid[i][j + 1],\n grid[i + 1][j - 1], grid[i + 1][j], grid[i + 1][j + 1]\n )\n row_result.append(max_value)\n result.append(row_result)\n \n return result\n``` | 6 | 0 | ['Python3'] | 0 |
largest-local-values-in-a-matrix | Beginner Friendly Approach [ 99.90% ] [ 2ms ] | beginner-friendly-approach-9990-2ms-by-r-yrob | Intuition\n Describe your first thoughts on how to solve this problem. \n\n# Approach\n1. Initialize Variables:\n - Get the size of the grid (n).\n - Initia | RajarshiMitra | NORMAL | 2024-06-22T06:38:09.541637+00:00 | 2024-06-22T06:38:09.541671+00:00 | 78 | false | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n1. **Initialize Variables:**\n - Get the size of the grid (`n`).\n - Initialize a 2D array `res` with dimensions `(n-2) x (n-2)` to store the results.\n\n2. **Iterate Over Each Possible 3x3 Subgrid:**\n - Use two nested loops to iterate through each possible starting point `(i, j)` of a 3x3 subgrid in the original `grid`.\n\n3. **Find Maximum in Each 3x3 Subgrid:**\n - Initialize `max` to 0 at the start of each subgrid computation.\n - Use two additional nested loops to iterate through each element `(k, l)` in the 3x3 subgrid starting at `(i, j)`.\n - Update `max` with the maximum value found in the current subgrid.\n\n4. **Store Maximum Value:**\n - After finding the maximum value for the current 3x3 subgrid, store it in the corresponding position in the result array `res`.\n\n5. **Return Result:**\n - After all subgrids have been processed, return the result array `res`.\n\n# Complexity\n- Time complexity:\nO(n^2)\n\n- Space complexity:\nO(n^2)\n\n# Code\n```\nclass Solution {\n public int[][] largestLocal(int[][] grid) {\n int n=grid.length;\n int max=0;\n int res[][]=new int[n-2][n-2];\n for(int i=0; i<n-2; i++)\n {\n for(int j=0; j<n-2; j++)\n {\n max=0;\n for(int k=i; k<i+3; k++)\n {\n for(int l=j; l<j+3; l++){\n max=Math.max(max,grid[k][l]);\n }\n\n }\n res[i][j]=max;\n }\n }\n return res;\n }\n}\n```\n\n\n | 5 | 0 | ['Java'] | 0 |
largest-local-values-in-a-matrix | Java beats 100% | java-beats-100-by-deleted_user-ptqa | Java beats 100%\n\n\n\n\n# Code\n\nclass Solution {\n\n public int largestLocalUtil(int[][] grid, int x, int y) {\n int max = 0;\n \n fo | deleted_user | NORMAL | 2024-05-12T11:37:27.378258+00:00 | 2024-05-12T11:37:27.378279+00:00 | 40 | false | Java beats 100%\n\n\n\n\n# Code\n```\nclass Solution {\n\n public int largestLocalUtil(int[][] grid, int x, int y) {\n int max = 0;\n \n for (int i = x ; i < x+3 ; i++) {\n for (int j = y ; j < y+3 ; j++) {\n max = Math.max(max, grid[i][j]);\n }\n }\n \n return max;\n }\n \n public int[][] largestLocal(int[][] grid) {\n int n = grid.length;\n \n int m = n-2;\n \n int[][] ans = new int[m][m];\n \n for (int i = 0 ; i < m ; i++) {\n for (int j = 0 ; j < m ; j++) {\n ans[i][j] = largestLocalUtil(grid, i, j);\n }\n }\n \n return ans;\n }\n}\n``` | 5 | 0 | ['Java'] | 0 |
largest-local-values-in-a-matrix | ✅ One Line Solution | one-line-solution-by-mikposp-11w2 | (Disclaimer: this is not an example to follow in a real project - it is written for fun and training mostly)\n\n# Code #1\nTime complexity: O(n^2). Space comple | MikPosp | NORMAL | 2024-05-12T09:00:39.608004+00:00 | 2024-05-12T09:02:57.114686+00:00 | 1,137 | false | (Disclaimer: this is not an example to follow in a real project - it is written for fun and training mostly)\n\n# Code #1\nTime complexity: $$O(n^2)$$. Space complexity: $$O(n^2)$$.\n```\nclass Solution:\n def largestLocal(self, g: List[List[int]]) -> List[List[int]]:\n return [[max(max(r[j:j+3]) for r in g[i:i+3]) for j in range(len(g)-2)] for i in range(len(g)-2)]\n```\n\n# Code #2\nTime complexity: $$O(n^2)$$. Space complexity: $$O(n^2)$$.\n```\nclass Solution:\n def largestLocal(self, g: List[List[int]]) -> List[List[int]]:\n return [[max(g[i+p][j+q] for p,q in product(*[(0,1,2)]*2)) for j in range(len(g)-2)] for i in range(len(g)-2)]\n```\n\n(Disclaimer 2: all code above is just a product of fantasy, it is not claimed to be pure impeccable oneliners - please, remind about drawbacks only if you know how to make it better. PEP 8 is violated intentionally) | 5 | 1 | ['Matrix', 'Python', 'Python3'] | 1 |
largest-local-values-in-a-matrix | Need for loop for for loops xD | need-for-loop-for-for-loops-xd-by-movsar-eiaa | python\nclass Solution:\n def largestLocal(self, grid: List[List[int]]) -> List[List[int]]:\n n = len(grid)\n\n res = [([0] * (n - 2)) for _ in | movsar | NORMAL | 2024-05-12T01:56:36.385624+00:00 | 2024-05-12T01:56:36.385645+00:00 | 240 | false | ```python\nclass Solution:\n def largestLocal(self, grid: List[List[int]]) -> List[List[int]]:\n n = len(grid)\n\n res = [([0] * (n - 2)) for _ in range(n - 2)]\n\n for x in range(n - 2):\n for y in range(n - 2):\n local_max = 0\n for i in range(x, x + 3):\n for j in range(y, y + 3):\n local_max = max(local_max, grid[i][j])\n res[x][y] = local_max\n\n return res\n\n``` | 5 | 1 | ['Python3'] | 1 |
largest-local-values-in-a-matrix | 🏆💢💯 Faster✅💯 Lesser🧠 🎯 C++✅Python3🐍✅Java✅C✅Python🐍✅💥🔥💫Explained☠💥🔥 Beats 💯 | faster-lesser-cpython3javacpythonexplain-971d | Intuition\n\n\n\nC++ []\nclass Solution {\npublic:\n vector<vector<int>> largestLocal(vector<vector<int>>& grid) {\n int n = grid.size();\n vec | Edwards310 | NORMAL | 2024-05-12T01:07:19.260717+00:00 | 2024-05-12T01:07:19.260747+00:00 | 319 | false | # Intuition\n\n\n\n```C++ []\nclass Solution {\npublic:\n vector<vector<int>> largestLocal(vector<vector<int>>& grid) {\n int n = grid.size();\n vector<vector<int>> v(n - 2, vector<int>(n - 2, 0));\n for (int i = 1; i <= n - 2; i++) {\n for (int j = 1; j <= n - 2; j++) {\n int maxi = 0;\n maxi = max({maxi, grid[i - 1][j - 1], grid[i - 1][j],\n grid[i - 1][j + 1]});\n maxi = max({maxi, grid[i][j - 1], grid[i][j], grid[i][j + 1]});\n maxi = max({maxi, grid[i + 1][j - 1], grid[i + 1][j],\n grid[i + 1][j + 1]});\n\n v[i - 1][j - 1] = maxi;\n }\n }\n return v;\n }\n};\n```\n```python3 []\nclass Solution:\n def largestLocal(self, grid: List[List[int]]) -> List[List[int]]:\n n = len(grid)\n ans = [[0] * (n - 2) for _ in range(n - 2)]\n for i in range(n - 2):\n for j in range(n - 2):\n ans[i][j] = max(\n grid[ii][jj] for ii in range(i, i + 3) for jj in range(j, j + 3)\n )\n return ans\n```\n```C []\n/**\n * Return an array of arrays of size *returnSize.\n * The sizes of the arrays are returned as *returnColumnSizes array.\n * Note: Both returned array and *columnSizes array must be malloced, assume\n * caller calls free().\n */\nint** largestLocal(int** grid, int gridSize, int* gridColSize, int* returnSize, int** returnColumnSizes) {\n int** arr = (int**)malloc((gridSize - 2) * sizeof(int*));\n for (int i = 0; i < gridSize - 2; i++) {\n arr[i] = (int*)malloc((gridSize - 2) * sizeof(int));\n }\n int arrSize = gridSize - 2;\n *returnSize = arrSize;\n for (int i = 0; i < arrSize; i++) {\n for (int j = 0; j < arrSize; j++) {\n arr[i][j] = maxElement(grid, i, j);\n }\n }\n *returnColumnSizes = (int*)malloc(arrSize * sizeof(int));\n for (int i = 0; i < arrSize; i++) {\n (*returnColumnSizes)[i] = arrSize;\n }\n return arr;\n}\n\nint maxElement(int** grid, int r, int c) {\n int max = INT_MIN;\n for (int i = r; i < r + 3; i++) {\n for (int j = c; j < c + 3; j++) {\n max = fmax(grid[i][j], max);\n }\n }\n return max;\n}\n```\n```Java []\nclass Solution {\n public int[][] largestLocal(int[][] grid) {\n int n = grid.length;\n int[][] maxLocal = new int[n - 2][n - 2];\n for (int i = 0; i < n - 2; ++i) {\n for (int j = 0; j < n - 2; ++j) {\n int max = 0;\n for (int k = i; k < i + 3; ++k) {\n for (int l = j; l < j + 3; ++l) {\n max = Math.max(max, grid[k][l]);\n }\n }\n maxLocal[i][j] = max;\n }\n }\n\n return maxLocal;\n }\n}\n```\n```python []\nclass Solution(object):\n def largestLocal(self, grid):\n """\n :type grid: List[List[int]]\n :rtype: List[List[int]]\n """\n mtx = []\n for i in range(len(grid) - 2):\n mtx.append([])\n for j in range(len(grid) - 2):\n mtx[i].append(max(grid[i][j : j + 3] + grid[i + 1][j : j + 3] + grid[i + 2][j : j + 3]))\n return mtx\n```\n\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n O(N^4)\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n O(n)\n# Code\n```\nclass Solution(object):\n def largestLocal(self, grid):\n """\n :type grid: List[List[int]]\n :rtype: List[List[int]]\n """\n mtx = []\n for i in range(len(grid) - 2):\n mtx.append([])\n for j in range(len(grid) - 2):\n mtx[i].append(max(grid[i][j : j + 3] + grid[i + 1][j : j + 3] + grid[i + 2][j : j + 3]))\n return mtx\n```\n# ***Please Upvote if it\'s useful for you...\n***\n | 5 | 0 | ['Array', 'C', 'Matrix', 'Python', 'C++', 'Java', 'Python3'] | 3 |
largest-local-values-in-a-matrix | my_getMaxOfV | my_getmaxofv-by-rinatmambetov-lemd | # Intuition \n\n\n\n\n\n\n\n\n\n\n\n# Code\n\nint getMaxOfV(vector<vector<int>> &v) {\n int max(0);\n for (auto &&line : v) {\n for (auto &&elem : line) | RinatMambetov | NORMAL | 2023-05-16T12:19:56.892958+00:00 | 2023-05-16T12:19:56.893002+00:00 | 643 | false | <!-- # Intuition -->\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n<!-- # Approach -->\n<!-- Describe your approach to solving the problem. -->\n\n<!-- # Complexity\n- Time complexity: -->\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n<!-- - Space complexity: -->\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```\nint getMaxOfV(vector<vector<int>> &v) {\n int max(0);\n for (auto &&line : v) {\n for (auto &&elem : line) {\n if (elem > max) max = elem;\n }\n }\n return max;\n}\n\nclass Solution {\n public:\n vector<vector<int>> largestLocal(vector<vector<int>> &grid) {\n size_t size = grid.size();\n vector<vector<int>> result = {};\n\n for (size_t startRow = 0; startRow <= size - 3; startRow++) {\n vector<int> tempV = {};\n for (size_t startCol = 0; startCol <= size - 3; startCol++) {\n vector<vector<int>> largeV = {};\n for (size_t row = startRow; row < startRow + 3; row++) {\n vector<int> smallV(grid[row].begin() + startCol,\n grid[row].begin() + startCol + 3);\n largeV.push_back(smallV);\n }\n tempV.push_back(getMaxOfV(largeV));\n }\n result.push_back(tempV);\n }\n\n return result;\n }\n};\n``` | 5 | 0 | ['C++'] | 1 |
largest-local-values-in-a-matrix | The best solution for beginners | the-best-solution-for-beginners-by-sahil-x2yq | \n\n"a" and "b" denotes row and column respectively do not confuse, as a beginner.\n\n\nINSTEAD OF WRITING "a < i+3" you can also write "a <= i+2" ;\n\nDo work | sahilaroraesl | NORMAL | 2023-03-27T08:57:39.911441+00:00 | 2023-03-27T08:57:39.911487+00:00 | 1,593 | false | \n\n**"a"** and **"b"** denotes **row** and **column** respectively do not confuse, as a beginner.\n\n\nINSTEAD OF WRITING **"a < i+3"** you can also write **"a <= i+2"** ;\n\n**Do work on your fundamentals, Try to read and understand the code.**\n\nTry to put your 1% efforts daily.\n\n# Code\n```\nclass Solution {\n public int[][] largestLocal(int[][] grid) {\n int n = grid.length;\n int res[][] = new int[n-2][n-2];\n for (int i = 0; i < n-2; i++) {\n for (int j = 0; j < n-2; j++) {\n res[i][j] = findMax(grid, i, j);\n }\n }\n return res;\n }\n public int findMax(int[][] grid, int i, int j) {\n int max = Integer.MIN_VALUE;\n for (int a = i; a < i+3; a++) {\n for (int b = j; b < j+3; b++) {\n max = Math.max(grid[a][b], max);\n }\n }\n return max;\n }\n\n}\n``` | 5 | 0 | ['Java'] | 2 |
largest-local-values-in-a-matrix | ✅ [Swift] Two loops, 100% speed , easy to understand | swift-two-loops-100-speed-easy-to-unders-r0rx | \n\n\nclass Solution {\n func largestLocal(_ grid: [[Int]]) -> [[Int]] {\n var size = grid.count - 2\n\t var result = Array(repeating: Array(repeat | lmvtsv | NORMAL | 2022-11-26T22:49:14.581692+00:00 | 2022-11-26T22:49:14.581715+00:00 | 208 | false | \n\n```\nclass Solution {\n func largestLocal(_ grid: [[Int]]) -> [[Int]] {\n var size = grid.count - 2\n\t var result = Array(repeating: Array(repeating: 0, count: size), count: size)\n\n\t for i in 0..<size {\n\t\t for j in 0..<size {\n\t\t\t var maxLocal = max(grid[i][j], grid[i][j + 1], grid[i][j + 2])\n\t\t\t maxLocal = max(maxLocal, grid[i + 1][j], grid[i + 1][j + 1], grid[i + 1][j + 2])\n\t\t\t maxLocal = max(maxLocal, grid[i + 2][j], grid[i + 2][j + 1], grid[i + 2][j + 2])\n\t\t\t result[i][j] = maxLocal\n\t\t }\n\t }\n\t return result\n }\n}\n``` | 5 | 0 | ['Swift'] | 1 |
largest-local-values-in-a-matrix | 🍁 C++ || 2 SOLUTION || EASY 🍁 | c-2-solution-easy-by-venom-xd-x45s | //SOL 1\n\n\t\tint m(vector>& grid, int row, int col) {\n\t\tint ret = 0;\n\t\tret = max(ret, grid[row][col]);\n\t\tret = max(ret, grid[row][col + 1]);\n\t\tret | venom-xd | NORMAL | 2022-08-14T12:52:02.570800+00:00 | 2022-08-14T12:52:17.213808+00:00 | 1,652 | false | //SOL 1\n\n\t\tint m(vector<vector<int>>& grid, int row, int col) {\n\t\tint ret = 0;\n\t\tret = max(ret, grid[row][col]);\n\t\tret = max(ret, grid[row][col + 1]);\n\t\tret = max(ret, grid[row][col + 2]);\n\t\tret = max(ret, grid[row + 1][col]);\n\t\tret = max(ret, grid[row + 1][col + 1]);\n\t\tret = max(ret, grid[row + 1][col + 2]);\n\t\tret = max(ret, grid[row + 2][col]);\n\t\tret = max(ret, grid[row + 2][col + 1]);\n\t\tret = max(ret, grid[row + 2][col + 2]);\n\t\treturn ret;\n\t}\n\n\tclass Solution {\n\tpublic:\n\t\tvector<vector<int>> largestLocal(vector<vector<int>>& grid) {\n\t\t\tint n = grid.size();\n\t\t\tvector<vector<int>> ret(n - 2, vector<int>(n - 2));\n\n\t\t\tfor (int i = 0; i < n - 2; ++i) {\n\t\t\t\tfor (int j = 0; j < n - 2; ++j) {\n\t\t\t\t\tret[i][j] = m(grid, i, j);\n\t\t\t\t}\n\t\t\t}\n\t\t\treturn ret;\n\t\t}\n\t};\n\n//SOL 2\n\n\n\tclass Solution {\n\tpublic:\n\t\tvector<vector<int>> largestLocal(vector<vector<int>>& grid) {\n\t\t\tint m = grid.size(), n = grid[0].size(); \n\t\t\tvector<vector<int>> ans(m-2, vector<int>(n-2)); \n\t\t\tfor (int i = 1; i < m-1; ++i) \n\t\t\t\tfor (int j = 1; j < n-1; ++j) {\n\t\t\t\t\tint cand = 0; \n\t\t\t\t\tfor (int ii = i-1; ii <= i+1; ++ii) \n\t\t\t\t\t\tfor (int jj = j-1; jj <= j+1; ++jj) \n\t\t\t\t\t\t\tcand = max(cand, grid[ii][jj]); \n\t\t\t\t\tans[i-1][j-1] = cand; \n\t\t\t\t}\n\t\t\treturn ans; \n\t\t}\n\t};\n | 5 | 0 | ['C', 'C++'] | 3 |
largest-local-values-in-a-matrix | [Go] ♻️ In-place with max on 6 elements (instead of 9) | go-in-place-with-max-on-6-elements-inste-1gc6 | Intuition\n Describe your first thoughts on how to solve this problem. \nSpace reuse and computation results reuse \u267B\uFE0F\n\n # Approach \n Describe your | vWAj0nMjOtK33vIH0jpLww | NORMAL | 2024-05-12T07:12:04.863101+00:00 | 2024-05-12T07:12:04.863122+00:00 | 179 | false | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n**Space** reuse and computation **results** reuse \u267B\uFE0F\n\n<!-- # Approach -->\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity: $$O(n^2)$$\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity: $$O(1)$$\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```\nfunc largestLocal(grid [][]int) [][]int {\n\tI, J := len(grid)-2, len(grid[0])-2\n\tfor i, row := range grid[:I] {\n\t\trow[0] = max(row[0], grid[i+1][0], grid[i+2][0])\n\t\trow[1] = max(row[1], grid[i+1][1], grid[i+2][1])\n\t\tfor j, x := range row[:J] {\n\t\t\trow[j+2] = max(row[j+2], grid[i+1][j+2], grid[i+2][j+2])\n\t\t\trow[j] = max(x, row[j+1], row[j+2])\n\t\t}\n\t\tgrid[i] = row[:J]\n\t}\n\treturn grid[:I]\n}\n``` | 4 | 0 | ['Go'] | 1 |
largest-local-values-in-a-matrix | 🔥💯Beats 100% of users with Java🎉||✅ one line loop code || 2 Sec to understand 🎉🎊 | beats-100-of-users-with-java-one-line-lo-z5uy | Sreenshot\n\n\n\n---\n\n\n# Intuition\n\n> Follow the steps and you will also solve this in 2 Sec.\n Describe your first thoughts on how to solve this problem. | Prakhar-002 | NORMAL | 2024-05-12T06:45:48.997173+00:00 | 2024-05-12T06:53:22.084354+00:00 | 121 | false | # Sreenshot\n\n\n\n---\n\n\n# Intuition\n\n> Follow the steps and you will also solve this in 2 Sec.\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n- We need to return a `(n - 2) x (n - 2) `grid combination.\n- In which each element `[i][j]` will be the `max` value of a maxtrix.\n- Matrix that is made by `9` Elements which are present in main `Grid`\n- Matrix will be contain `3 col and 3 row`.\n- `row and col` we\'ll get by the `element\'s position`.\n- `Row` will be `i to i + 3`.\n- `Col` will be `j to j + 3`.\n\n---\n\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n1. Take the value of `(grid.lenght - 2 )` in a veriable .\n\n2. And make a Array of `(n - 2) x (n - 2) ` \n ```\n int n = grid.length - 2;\n\n // Making a submit Array of (n - 2) x (n - 2)\n int[][] subArr = new int[n][n];\n ```\n3. **Now we\'ll write our one line code in 2 for loop :**\n\n - We\'ll take our max value from a function and store too our array\n\n - We\'ll run 2 for loop\n\n ```\n for (int i = 0; i < n; i++) {\n for (int j = 0; j < n; j++) {\n // We\'ll take max value from our another function...\n subArr[i][j] = giveMaxValue(grid, i, j);\n }\n }\n ```\n\n4. Make a `function` with `3 Args` That will `Grid Row Col`.\n\n5. And we\'ll `return MaxValue` of this `given Matrix`.\n\n - Take a `MaxValue variable` assign with 0 \n - `Apply 2 for loops` and `Find the max value` of given matrix.\n\n ```\n int maxValue = 0;\n\n for (int i = row; i < row + 3; i++) {\n for (int j = col; j < col + 3; j++) {\n maxValue = Math.max(maxValue, grid[i][j]);\n }\n }\n\n ```\n - Finally return the maxValue.\n\n6. `return our Array`.\n\n---\n\n\n# Complexity\n- Time complexity: $$O(n^2)$$\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity: $$O(n^2)$$\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n---\n\n\n# Code\n```\nclass Solution {\n\n public int giveMaxValue(int[][] grid, int row, int col) {\n int maxValue = 0;\n\n for (int i = row; i < row + 3; i++) {\n for (int j = col; j < col + 3; j++) {\n // max value to our maxValue variable...\n maxValue = Math.max(maxValue, grid[i][j]);\n }\n }\n\n return maxValue;\n }\n\n public int[][] largestLocal(int[][] grid) {\n int n = grid.length - 2;\n\n // Making a submit Array of (n - 2) x (n - 2)\n int[][] subArr = new int[n][n];\n\n for (int i = 0; i < n; i++) {\n for (int j = 0; j < n; j++) {\n // Every element of submit array will give by this fun\n subArr[i][j] = giveMaxValue(grid, i, j);\n }\n }\n\n return subArr;\n }\n}\n```\n\n\n | 4 | 0 | ['Array', 'Matrix', 'Java'] | 1 |
largest-local-values-in-a-matrix | BEATS 100% || IN DEPTH|| LEARN SIMULATION. | beats-100-in-depth-learn-simulation-by-a-gruv | Intuition\n Describe your first thoughts on how to solve this problem. \nhe code effectively utilizes a nested loop structure to explore all possible 3x3 sub-ma | Abhishekkant135 | NORMAL | 2024-05-12T06:36:19.347109+00:00 | 2024-05-12T06:36:19.347137+00:00 | 252 | false | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nhe code effectively utilizes a nested loop structure to explore all possible 3x3 sub-matrices within the larger grid. For each sub-matrix, it calls the `FindMax` function to determine the element with the maximum value. By storing these maximum values in the `ans` array, the code provides the results of finding the sub-matrices with the largest values within the original grid.\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n\n1. **Initializing Results:**\n - `int n = grid.length`: Gets the length (number of rows) of the input grid `grid`.\n - `int[][] ans = new int[n - 2][n - 2]`: Creates a new 2D array `ans` to store the results. It has dimensions `n-2` x `n-2` because the largest possible sub-matrix within an `n` x `n` grid would be a 3x3 sub-matrix, leaving a border of 1 element around the original grid.\n\n2. **Iterating Through Possible Sub-Matrices:**\n - `for (int i = 0; i < n - 2; i++)`: Iterates through rows of the grid, starting from index 0 and going up to `n-2` to account for the sub-matrix size (3x3) and the border.\n - `for (int j = 0; j < n - 2; j++)`: Iterates through columns of the grid within the current row, again starting from index 0 and going up to `n-2` for the same reason.\n - `ans[i][j] = FindMax(grid, i, j)`: Calls the `FindMax` function (defined below) to find the maximum value within the 3x3 sub-matrix starting at row `i` and column `j` of the original grid `grid`. The resulting maximum value is stored in the corresponding position `ans[i][j]` of the results array.\n\n3. **Finding the Maximum in a Sub-Matrix:**\n - `int FindMax(int[][] grid, int i, int j)`: This function takes a sub-matrix section of the grid defined by its starting row index `i` and column index `j`.\n - `int max = 0`: Initializes a variable `max` to store the maximum value found within the sub-matrix.\n - `for (int k = i; k <= i + 2; k++)`: Iterates through rows of the sub-matrix (3 rows).\n - `for (int l = j; l <= j + 2; l++)`: Iterates through columns of the sub-matrix (3 columns).\n - `max = Math.max(max, grid[k][l])`: Compares the current element `grid[k][l]` of the sub-matrix with the current `max` value. The `Math.max` function ensures `max` is always updated to hold the largest value encountered so far within the sub-matrix.\n - `return max`: After iterating through all elements of the sub-matrix, the function returns the final `max` value, which represents the maximum value found within that sub-matrix.\n\n4. **Returning the Results:**\n - `return ans`: After iterating through all possible sub-matrices and finding their maximum values, the function returns the `ans` array containing the maximum values for each sub-matrix.\n\n\n```python []\nclass Solution:\n def largestLocal(self, grid):\n n = len(grid)\n ans = [[0] * (n - 2) for _ in range(n - 2)]\n \n for i in range(n - 2):\n for j in range(n - 2):\n ans[i][j] = self.FindMax(grid, i, j)\n \n return ans\n \n def FindMax(self, grid, i, j):\n maxVal = 0\n for k in range(i, i + 3):\n for l in range(j, j + 3):\n maxVal = max(maxVal, grid[k][l])\n return maxVal\n\n```\n```C++ []\n#include <vector>\n#include <algorithm>\n\nclass Solution {\npublic:\n std::vector<std::vector<int>> largestLocal(std::vector<std::vector<int>>& grid) {\n int n = grid.size();\n std::vector<std::vector<int>> ans(n - 2, std::vector<int>(n - 2, 0));\n \n for (int i = 0; i < n - 2; i++) {\n for (int j = 0; j < n - 2; j++) {\n ans[i][j] = FindMax(grid, i, j);\n }\n }\n \n return ans;\n }\n \n int FindMax(std::vector<std::vector<int>>& grid, int i, int j) {\n int maxVal = 0;\n for (int k = i; k <= i + 2; k++) {\n for (int l = j; l <= j + 2; l++) {\n maxVal = std::max(maxVal, grid[k][l]);\n }\n }\n return maxVal;\n }\n};\n\n```\n\n\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\nO(9N^2)\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\nO(N^2)\n# Code\n```\nclass Solution {\n public int[][] largestLocal(int[][] grid) {\n int n=grid.length;\n int [][] ans=new int[n-2][n-2];\n for(int i=0;i<n-2;i++){\n for(int j=0;j<n-2;j++){\n ans[i][j]=FindMax(grid,i,j);\n }\n }\n return ans;\n }\n public int FindMax(int[][] grid, int i,int j) {\n int max=0;\n for(int k=i;k<=i+2;k++){\n for(int l=j;l<=j+2;l++){\n max=Math.max(max,grid[k][l]);\n }\n }\n return max;\n }\n}\n``` | 4 | 0 | ['Simulation', 'Python', 'C++', 'Java'] | 0 |
largest-local-values-in-a-matrix | ✅ Easy C++ Solution | easy-c-solution-by-moheat-pomm | Code\n\nint findMax(vector<vector<int>>& grid,int i,int j)\n{\n int maxi = INT_MIN;\n for(int x=i;x<i+3;x++)\n {\n for(int y=j;y<j+3;y++)\n | moheat | NORMAL | 2024-05-12T02:25:48.760271+00:00 | 2024-05-12T02:25:48.760298+00:00 | 719 | false | # Code\n```\nint findMax(vector<vector<int>>& grid,int i,int j)\n{\n int maxi = INT_MIN;\n for(int x=i;x<i+3;x++)\n {\n for(int y=j;y<j+3;y++)\n {\n maxi = max(maxi,grid[x][y]);\n }\n }\n return maxi;\n}\n\nclass Solution {\npublic:\n vector<vector<int>> largestLocal(vector<vector<int>>& grid) {\n int n = grid.size();\n vector<vector<int>> v(n - 2, vector<int>(n - 2));\n for(int i=0;i<n-2;i++)\n {\n for(int j=0;j<n-2;j++)\n {\n int max = findMax(grid,i,j);\n v[i][j] = max;\n }\n }\n return v;\n }\n};\n``` | 4 | 0 | ['C++'] | 1 |
largest-local-values-in-a-matrix | simple and easy understand solution | simple-and-easy-understand-solution-by-s-25of | if it\'s help, please up \u2B06 vote! \u2764\uFE0F\n\n# Code\n\nclass Solution {\npublic:\n vector<vector<int>> largestLocal(vector<vector<int>>& grid) \n | shishirRsiam | NORMAL | 2024-03-19T11:56:41.297230+00:00 | 2024-03-19T11:56:41.297271+00:00 | 783 | false | # if it\'s help, please up \u2B06 vote! \u2764\uFE0F\n\n# Code\n```\nclass Solution {\npublic:\n vector<vector<int>> largestLocal(vector<vector<int>>& grid) \n {\n int n = grid.size();\n vector<vector<int>>ans;\n vector<int>tmp;\n int mx = 0;\n for(int i=0;i<n-2;i++)\n {\n for(int j=0;j<n-2;j++)\n {\n for(int x=i;x<i+3;x++)\n {\n for(int y=j;y<j+3;y++)\n {\n // cout<<x<<y<<" ";\n mx = max(mx, grid[x][y]);\n }\n // cout<<endl;\n }\n // cout<<endl;\n tmp.push_back(mx);\n mx = 0;\n }\n ans.push_back(tmp);\n tmp.clear();\n }\n return ans;\n }\n};\n``` | 4 | 1 | ['Array', 'C', 'Matrix', 'Simulation', 'Python', 'C++', 'Java', 'Python3', 'JavaScript', 'C#'] | 6 |
largest-local-values-in-a-matrix | Javascript - Bulky Math.max, 90% Runtime | javascript-bulky-mathmax-90-runtime-by-m-w60j | \nconst largestLocal = (grid) => {\n let ans = [];\n for (let i = 1; i < grid.length - 1; i++) {\n let row = [];\n for (let j = 1; j < grid.length - 1; | MCCP96 | NORMAL | 2022-09-01T13:08:21.700347+00:00 | 2022-09-01T13:08:21.700426+00:00 | 326 | false | ```\nconst largestLocal = (grid) => {\n let ans = [];\n for (let i = 1; i < grid.length - 1; i++) {\n let row = [];\n for (let j = 1; j < grid.length - 1; j++) {\n row.push(\n Math.max(\n grid[i - 1][j - 1], grid[i - 1][j], grid[i - 1][j + 1],\n grid[i][j - 1], grid[i][j], grid[i][j + 1],\n grid[i + 1][j - 1], grid[i + 1][j], grid[i + 1][j + 1]\n )\n );\n }\n ans.push(row);\n }\n return ans;\n};\n``` | 4 | 0 | ['JavaScript'] | 2 |
largest-local-values-in-a-matrix | Java | For Loops | Easy Implementation | java-for-loops-easy-implementation-by-di-dfxb | \nclass Solution {\n public int[][] largestLocal(int[][] grid) {\n int n = grid.length;\n int arr[][] = new int[n-2][n-2];\n for(int i=0 | Divyansh__26 | NORMAL | 2022-08-26T05:25:44.331374+00:00 | 2022-09-20T13:58:29.007719+00:00 | 464 | false | ```\nclass Solution {\n public int[][] largestLocal(int[][] grid) {\n int n = grid.length;\n int arr[][] = new int[n-2][n-2];\n for(int i=0;i<n-2;i++){\n for(int j=0;j<n-2;j++){\n arr[i][j]=local(grid,i,j);\n }\n }\n return arr;\n }\n public int local(int g[][],int x,int y){\n int max = Integer.MIN_VALUE;\n for(int i=x;i<x+3;i++){\n for(int j=y;j<y+3;j++){\n if(g[i][j]>max)\n max=g[i][j];\n }\n }\n return max;\n }\n}\n```\nKindly upvote if you like the code. | 4 | 0 | ['Array', 'Matrix', 'Java'] | 1 |
largest-local-values-in-a-matrix | C# | 1-Liner | c-1-liner-by-dana-n-cynl | Uses LINQ and C# Ranges to efficiently determine local maximums.\n\ncs\npublic int[][] LargestLocal(int[][] grid) => \n Enumerable\n .Range(0, grid.Le | dana-n | NORMAL | 2022-08-17T22:44:09.231911+00:00 | 2024-05-12T02:52:46.746448+00:00 | 259 | false | Uses LINQ and C# Ranges to efficiently determine local maximums.\n\n```cs\npublic int[][] LargestLocal(int[][] grid) => \n Enumerable\n .Range(0, grid.Length - 2)\n .Select(i =>\n Enumerable\n \xA0 \xA0 \xA0 \xA0 \xA0 \xA0 \xA0 \xA0.Range(0, grid.Length - 2)\n .Select(j =>\n grid[i..(i+3)].Max(row => row[j..(j+3)].Max())\n )\n .ToArray()\n )\n .ToArray();\n```\n\n**Techniques Used:**\n\n* `Enumerable.Range(start, count)` method returns `count` numbers starting at `start`, Example `Enumerable.Range(0, 3)` returns `[0, 1, 2]`.\n* `IEnumerable<int>.Max()` extension method returns the largest value in the collection. Example `(new[] { 1, 2, 3, 4, 5 }).Max()` returns `5`.\n* `T[from..to]` range syntax returns elements between `from` (inclusive) and `to` (exclusive). Example `(new[] { 1, 2, 3, 4, 5 })[1..4]` returns `[2, 3, 4]`.\n* `IEnumerable<T>.ToArray()` extension method converts an `IEnumerable` type, that is typically used w/ LINQ syntax, to an Array.\n\nFrom there, we stitch things together in order to get the results that is being asked. In this case, we select 2 fewer rows and columns, use range syntax to select local elements, then get the maximum of the selected. There is a lot more you can do with LINQ! The newer JavaScript syntax has similar functionality.\n \nCheck out my other C# 1-liners!\n* https://leetcode.com/discuss/general-discussion/2905237/c-sharp-1-liners | 4 | 0 | [] | 2 |
largest-local-values-in-a-matrix | ✅C++ | ✅Simple solution | ✅O(n^2) | c-simple-solution-on2-by-mayanksamadhiya-0xj9 | \nclass Solution {\npublic:\n vector<vector<int>> largestLocal(vector<vector<int>>& grid) \n {\n int n = grid.size();\n vector<vector<int>> | mayanksamadhiya12345 | NORMAL | 2022-08-14T04:00:40.194057+00:00 | 2022-08-14T04:06:16.650304+00:00 | 1,015 | false | ```\nclass Solution {\npublic:\n vector<vector<int>> largestLocal(vector<vector<int>>& grid) \n {\n int n = grid.size();\n vector<vector<int>> ans(n-2,vector<int> (n-2,0));\n \n \n // find the max for each center value\n // and store it in (i-1)(j-1)\n for(int i=1;i<=n-2;i++)\n {\n for(int j=1;j<=n-2;j++)\n {\n int mx = INT_MIN;\n mx = max(mx,max(grid[i-1][j-1],max(grid[i-1][j],grid[i-1][j+1]))); // (0,0)(0,1)(0,2)\n mx = max(mx,max(grid[i][j-1],max(grid[i][j],grid[i][j+1]))); // (1,0)(1,1)(1,2)\n mx = max(mx,max(grid[i+1][j-1],max(grid[i+1][j],grid[i+1][j+1]))); // (2,0)(2,1)(2,2)\n \n ans[i-1][j-1]=mx;\n }\n }\n return ans;\n }\n};\n``` | 4 | 0 | [] | 2 |
largest-local-values-in-a-matrix | Very Easy Solution || Beat 100% ||Beginner Friendly | very-easy-solution-beat-100-beginner-fri-ud7y | Intuition\nThe problem seems to involve finding the largest local value within a grid by considering 3x3 subgrids\n\n# Approach\nThe approach seems to iterate t | Pratik_Shelke | NORMAL | 2024-05-12T14:25:14.151578+00:00 | 2024-05-12T14:25:14.151608+00:00 | 310 | false | # Intuition\nThe problem seems to involve finding the largest local value within a grid by considering 3x3 subgrids\n\n# Approach\nThe approach seems to iterate through each position (i, j) in the grid and for each position, iterate over the 3x3 subgrid starting from (i, j). Within this subgrid, find the maximum value and store it in the maxl array.\n\n# Complexity\n- Time complexity:\n $$O(n^2)$$ \n\n- Space complexity:\n $$O(n^2)$$ \n\n# Code\n```\nclass Solution {\n public int[][] largestLocal(int[][] grid) {\n int n=grid.length;\n int m=n-2;\n int [][]maxl=new int[m][m];\n int max=0;\n \n for(int i=0;i<m;i++)\n {\n for(int j=0;j<m;j++)\n {\n max=0;\n for(int k=i;k<i+3;k++)\n {\n for(int l=j;l<j+3;l++){\n max=Math.max(max,grid[k][l]);\n }\n\n }\n maxl[i][j]=max;\n }\n }\n return maxl;\n\n \n }\n}\n``` | 3 | 0 | ['Array', 'Java'] | 2 |
largest-local-values-in-a-matrix | Java Easy to Understand Solution with Explanation || 100% beats | java-easy-to-understand-solution-with-ex-h1z9 | Intuition\n> The problem is straightforward. We iterate through maxLocal and check the 3x3 matrix windows.\n Describe your first thoughts on how to solve this p | leo_messi10 | NORMAL | 2024-05-12T07:02:00.758147+00:00 | 2024-05-12T07:02:00.758180+00:00 | 184 | false | # Intuition\n> The problem is straightforward. We iterate through `maxLocal` and check the 3x3 matrix windows.\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n1. It initializes a new 2D array `maxLocal` with dimensions (size - 2) x (size - 2), where size is the size-2 of the input grid.\n2. It then iterates over the `maxLocal` grid using two nested loops(size - 2). These loops iterate over each position (i, j) in the input grid.\n3. For each position (i, j) in the input grid, it calls the `maxofMatric` method to find the maximum value in the 3x3 subgrid centered at (i, j).\n4. The maximum value found for each 3x3 subgrid is stored in the corresponding position (i, j) in the `maxLocal` array.\n5. Finally, it returns the `maxLocal` array containing the largest value in each 3x3 local region of the input grid.\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity: $$O((n-2)\xB2)$$\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity: $$O((n-2)\xB2)$$\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```\nclass Solution {\n public int[][] largestLocal(int[][] grid) {\n int size = grid.length;\n\n int[][] maxLocal = new int[size - 2][size - 2];\n\n for (int i = 0; i < size - 2; i++) {\n for (int j = 0; j < size - 2; j++) {\n maxLocal[i][j] = maxofMatric(grid, i, j);\n }\n }\n\n return maxLocal;\n }\n\n public int maxofMatric(int[][] grid, int temp1, int temp2) {\n int max = 0;\n\n for (int i = temp1; i < temp1 + 3; i++) {\n for (int j = temp2; j < temp2 + 3; j++) {\n max = Math.max(max, grid[i][j]);\n }\n }\n\n return max;\n }\n}\n```\n\n\n | 3 | 0 | ['Java'] | 0 |
largest-local-values-in-a-matrix | Easy question ? done with Easy solution ! runs at 3ms beats 99.54 % users in C++ | easy-question-done-with-easy-solution-ru-qbz7 | Intuition\n Describe your first thoughts on how to solve this problem. \nThe intuition behind the largestLocal method is to find the local maximum within a 2D g | deleted_user | NORMAL | 2024-05-12T03:39:48.806735+00:00 | 2024-05-12T03:39:48.806754+00:00 | 147 | false | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nThe intuition behind the largestLocal method is to find the local maximum within a 2D grid. It does this by first computing the maximums in the horizontal direction and then finding the maximums in the vertical direction based on the previously computed horizontal maximums.\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\nHorizontal Maximums Calculation: Iterate through each row of the grid. For each element, calculate the maximum among its left, center, and right neighbors. Store these maximums in a separate 2D vector.\n\nVertical Maximums Calculation: Iterate through the horizontal maximums vector. For each element, calculate the maximum among its top, center, and bottom neighbors. Store these maximums in the final result vector.\n# Complexity\n- Time complexity:\n\n\n\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\nHorizontal Maximums Calculation: O(m^2), where \'m\' is the number of rows in the grid. This is because we iterate through each element in the grid and perform constant time operations.\nVertical Maximums Calculation: O(m^2), where \'m\' is the number of rows in the grid. Similar to horizontal maximums calculation, we iterate through each element in the 2D vector maximums and perform constant time operations.\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\nThe space complexity of this approach is O(m^2), where \'m\' is the number of rows in the grid. This space is used to store the horizontal maximums (maximums) and the final result (ret), both of which are 2D vectors.\n\n\n\n# Code\n```\nclass Solution {\npublic:\n vector<vector<int>> largestLocal(vector<vector<int>>& grid) {\n int m = grid.size(); \n vector<vector<int>> maximums;\n for (int i = 0; i < m; i++) {\n vector<int> maxim;\n for (int j = 1; j < m - 1; j++) {\n maxim.push_back(maxo(grid[i][j-1], grid[i][j], grid[i][j + 1]));\n }\n maximums.push_back(maxim);\n }\n vector<vector<int>> ret;\n for (int i = 1; i < m - 1; i++) {\n vector<int> temp;\n for (int j = 0; j < m - 2; j++) {\n temp.push_back(maxo(maximums[i - 1][j], maximums[i][j], maximums[i + 1][j]));\n }\n ret.push_back(temp);\n }\n return ret;\n }\n\nprivate:\n int maxo(int a, int b, int c) {\n return max(max(a, b), c);\n }\n};\n\n```\n\n | 3 | 0 | ['Array', 'Matrix', 'C++'] | 0 |
largest-local-values-in-a-matrix | Beginner-friendly Solution Using C++ || Clear | beginner-friendly-solution-using-c-clear-9rof | \n# Complexity\n- Time complexity: O(n^2)\n Add your time complexity here, e.g. O(n) \n\n- Space complexity: O(1)\n Add your space complexity here, e.g. O(n) \n | truongtamthanh2004 | NORMAL | 2024-05-12T00:35:44.339578+00:00 | 2024-05-12T00:35:44.339607+00:00 | 701 | false | \n# Complexity\n- Time complexity: O(n^2)\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity: O(1)\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```\nclass Solution {\npublic:\n int largestValue(vector<vector<int>>& grid, int r, int c)\n {\n int maxValue = 0;\n for (int i = r; i < r + 3; i++)\n {\n for (int j = c; j < c + 3; j++)\n {\n maxValue = max(maxValue, grid[i][j]);\n }\n }\n\n return maxValue;\n }\n vector<vector<int>> largestLocal(vector<vector<int>>& grid) {\n int n = grid.size();\n vector<vector<int>> ans(n - 2, vector<int>(n - 2));\n\n for (int i = 0; i < n - 2; i++) {\n for (int j = 0; j < n - 2; j++)\n {\n ans[i][j] = largestValue(grid, i, j);\n }\n }\n\n return ans;\n }\n};\n``` | 3 | 0 | ['C++'] | 1 |
largest-local-values-in-a-matrix | 🔥🔥Brute force for beginners (very easy solution) C++🔥🔥 | brute-force-for-beginners-very-easy-solu-ph3u | Intuition\n Describe your first thoughts on how to solve this problem. \n\n# Approach\n Describe your approach to solving the problem. By using nested for loops | Harshitshukla_02 | NORMAL | 2023-07-16T10:03:25.628252+00:00 | 2023-07-16T10:03:25.628276+00:00 | 208 | false | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->By using nested for loops\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```\nclass Solution {\npublic:\n vector<vector<int>> largestLocal(vector<vector<int>>& grid) {\n \n int n = grid.size();\n\n vector<vector<int>> v(n-2 , vector<int>(n-2));\n\n for(int i = 0 ; i < n-2 ; i++)\n {\n for(int j = 0 ; j < n-2 ; j++)\n {\n int ans = INT_MIN;\n for(int x = i ; x < i+3 ; x++)\n {\n for(int y = j ; y < j+3 ; y++)\n {\n ans = max(grid[x][y] , ans);\n }\n }\n v[i][j] = ans;\n }\n }\n return v;\n }\n};\n``` | 3 | 0 | ['C++'] | 1 |
largest-local-values-in-a-matrix | ✅ C++ |✅ Simple and Easy Solution|Beginner friendly | c-simple-and-easy-solutionbeginner-frien-nftp | \nclass Solution {\npublic:\n vector<vector<int>> largestLocal(vector<vector<int>>& grid) {\n // Calculate the size of the modified grid\n int k | 40metersdeep | NORMAL | 2023-05-24T11:39:46.942333+00:00 | 2023-05-24T11:39:46.942376+00:00 | 219 | false | ```\nclass Solution {\npublic:\n vector<vector<int>> largestLocal(vector<vector<int>>& grid) {\n // Calculate the size of the modified grid\n int k = grid.size() - 2;\n \n // Initialize a new 2D vector \'arr\' with dimensions \'k\' x \'k\' and all elements set to 0\n vector<vector<int>> arr(k, vector<int>(k, 0));\n \n // Iterate over the elements of \'arr\'\n for(int i = 0; i < arr.size(); i++) {\n for(int j = 0; j < arr.size(); j++) {\n \n // Find the maximum element within a 3x3 subgrid of \'grid\' starting at position (i, j)\n int maxm = INT_MIN;\n for(int p = i; p < i + 3; p++) {\n for(int q = j; q < j + 3; q++) {\n maxm = max(maxm, grid[p][q]);\n }\n }\n \n // Assign the maximum value to the corresponding position in \'arr\'\n arr[i][j] = maxm;\n }\n }\n \n // Return the modified grid\n return arr;\n }\n};\n```\n\n**Time Complexity:**\n\nThe outer two nested loops iterate arr.size() times, which is \'k\'. So, the time complexity of these loops is O(k^2).\nThe inner two nested loops iterate a constant number of times (3x3), so their time complexity is constant.\nOverall, the time complexity of the code is O(k^2) * O(1) = O(k^2).\n\n**Space Complexity:**\n\nThe space complexity is determined by the size of the arr vector, which is \'k\' x \'k\'.\nTherefore, the space complexity is O(k^2).\n | 3 | 0 | ['C', 'Matrix'] | 1 |
largest-local-values-in-a-matrix | Very Simple Approach ✔ Easy To Understand✔ | very-simple-approach-easy-to-understand-erd4g | \n# Code\n\nclass Solution {\npublic:\n int findMax(int x,int y,vector<vector<int>>&grid){\n int max=INT_MIN;\n for(int i=x;i<x+3;i++){\n | divas-sagta | NORMAL | 2023-05-08T08:20:17.338093+00:00 | 2023-05-08T08:20:17.338133+00:00 | 509 | false | \n# Code\n```\nclass Solution {\npublic:\n int findMax(int x,int y,vector<vector<int>>&grid){\n int max=INT_MIN;\n for(int i=x;i<x+3;i++){\n for(int j=y;j<y+3;j++){\n if(max<grid[i][j])\n max=grid[i][j];\n }\n }\n return max;\n }\n vector<vector<int>> largestLocal(vector<vector<int>>& grid) {\n int n=grid.size();\n vector<vector<int>>ans(n-2,vector<int>(n-2));\n for(int i=0;i<n-2;i++){\n for(int j=0;j<n-2;j++){\n ans[i][j]=findMax(i,j,grid);\n }\n }\n return ans;\n }\n};\n``` | 3 | 0 | ['C++'] | 2 |
largest-local-values-in-a-matrix | Java easy solution, 100% faster (2 ms) | java-easy-solution-100-faster-2-ms-by-mo-kava | Intuition\n Describe your first thoughts on how to solve this problem. \n\n# Approach\n Describe your approach to solving the problem. \n\n# Complexity\n- Time | mojtaba2422 | NORMAL | 2023-01-07T09:23:28.637032+00:00 | 2023-01-07T09:23:28.637067+00:00 | 1,069 | false | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity: O(n^2)\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity: O(1)\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```\nclass Solution {\n public int[][] largestLocal(int[][] grid) {\n int n = grid.length;\n int[][] ans = new int[n - 2][n - 2];\n for (int i = 0; i < n - 2; i++) {\n for (int j = 0; j < n - 2; j++) {\n ans[i][j] = getMax(grid, i+1, j+1);\n }\n }\n return ans;\n }\n\n private int getMax(int[][] grid, int i, int j) {\n int max = grid[i][j];\n max = Math.max(max, grid[i][j - 1]);\n max = Math.max(max, grid[i - 1][j - 1]);\n max = Math.max(max, grid[i - 1][j]);\n max = Math.max(max, grid[i - 1][j + 1]);\n max = Math.max(max, grid[i][j + 1]);\n max = Math.max(max, grid[i + 1][j + 1]);\n max = Math.max(max, grid[i + 1][j]);\n max = Math.max(max, grid[i + 1][j - 1]);\n return max;\n }\n}\n``` | 3 | 0 | ['Java'] | 2 |
largest-local-values-in-a-matrix | c++ | easy | short | c-easy-short-by-venomhighs7-xfdb | Intuition\n Describe your first thoughts on how to solve this problem. \n\n# Approach\n Describe your approach to solving the problem. \n\n# Complexity\n- Time | venomhighs7 | NORMAL | 2022-10-24T05:26:47.586512+00:00 | 2022-10-24T05:26:47.586537+00:00 | 621 | false | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```\nclass Solution {\npublic:\n vector<vector<int>> largestLocal(vector<vector<int>>& grid) {\n \n int n = grid.size();\n \n vector<vector<int>> ans(n-2,vector<int>(n-2));\n \n \n for(int i=0;i<n-2;i++){\n for(int j=0;j<n-2;j++)\n ans[i][j] = maxIn3x3(grid, i, j);\n } \n \n return ans;\n }\n \n \n int maxIn3x3(vector<vector<int>> &arr, int i, int j)\n\t{\n int maxVal = INT_MIN;\n \n for(int x = i ; x < i+3 ; x++){\n for(int y = j; y < j+3 ; y++)\n maxVal = max(arr[x][y], maxVal);\n }\n\n return maxVal;\n }\n \n};\n\n``` | 3 | 0 | ['C++'] | 1 |
largest-local-values-in-a-matrix | Easy Solution ✅ | easy-solution-by-anishkumar127-hzjr | \nclass Solution {\n public int[][] largestLocal(int[][] grid) {\n int n = grid.length;\n int maxLocal[][] = new int[n-2][n-2];\n for(in | anishkumar127 | NORMAL | 2022-10-21T12:44:37.453364+00:00 | 2022-10-21T12:44:37.453408+00:00 | 766 | false | ```\nclass Solution {\n public int[][] largestLocal(int[][] grid) {\n int n = grid.length;\n int maxLocal[][] = new int[n-2][n-2];\n for(int i=0; i<n-2;i++){\n for(int j=0; j<n-2; j++){\n maxLocal[i][j] = maxFind(grid,i,j);\n }\n }\n return maxLocal;\n }\n private int maxFind(int arr[][], int i_Start, int j_Start){\n int max = Integer.MIN_VALUE;\n for(int i=i_Start; i<i_Start+3; i++){\n for(int j=j_Start; j<j_Start+3; j++){\n max = Math.max(max,arr[i][j]);\n }\n }\n return max;\n }\n}\n/*\n[[9,9,8,1],\n [5,6,2,6],\n [8,2,6,4],\n [6,2,2,2]]\n \n \n so in that we need do see only 3 * 3 at one time.\n \n 9 9 8 = 3\n 5 6 2\n 8 2 6 = 3 \n its 3*3 ok so. in that. 9 is big.\n \n now col +1; \n 9 8 1 = 3\n 6 2 6 \n 2 6 4 = 3.\n its also 3 * 3 in that 9 is max.\n \n now row + 1;\n 5 6 2\n 8 2 6\n 6 2 2 \n in that 8 is max.\n now col +1;\n 6 2 6\n 2 6 4\n 2 2 2 \n so in that 6 is max.\n \n so now u can see. matrix is n-2 length was 4 , its 2 length; \n 9 9\n 8 6.\n\n``` | 3 | 0 | ['Java'] | 2 |
largest-local-values-in-a-matrix | Java || Simple Solution | java-simple-solution-by-mrlittle113-j9x1 | java\nclass Solution {\n \n int[][] grid;\n \n public int[][] largestLocal(int[][] grid) {\n this.grid = grid;\n \n\t\t// limit the ro | mrlittle113 | NORMAL | 2022-09-13T14:11:51.525349+00:00 | 2022-09-13T14:11:51.525385+00:00 | 1,841 | false | ```java\nclass Solution {\n \n int[][] grid;\n \n public int[][] largestLocal(int[][] grid) {\n this.grid = grid;\n \n\t\t// limit the rows and cols that can be checked\n int rows = grid.length - 2;\n int cols = grid[0].length - 2;\n \n int[][] local = new int[rows][cols];\n \n for (int i = 0; i < rows; i++) {\n for (int j = 0; j < cols; j++){\n local[i][j] = findMax(i, j);\n }\n }\n \n return local;\n }\n \n\t// find the max in 3x3 grid\n public int findMax(int x, int y) {\n int max = 0;\n int rows = x + 3;\n int cols = y + 3;\n \n for (int i = x; i < rows; i++) {\n for (int j = y; j < cols; j++) {\n if (grid[i][j] > max) max = grid[i][j];\n }\n }\n \n return max;\n }\n}\n``` | 3 | 0 | ['Java'] | 3 |
largest-local-values-in-a-matrix | Nested For Loops JavaScript Solution | nested-for-loops-javascript-solution-by-8h1wo | Found this solution helpful? Consider showing support by upvoting this post.\nHave a question? Kindly leave a comment below.\nThank you and happy hacking!\n\nva | sronin | NORMAL | 2022-08-18T16:39:11.048040+00:00 | 2022-08-26T14:51:52.936398+00:00 | 425 | false | Found this solution helpful? Consider showing support by upvoting this post.\nHave a question? Kindly leave a comment below.\nThank you and happy hacking!\n```\nvar largestLocal = function (grid) {\n let result = []\n\n for (let i = 1; i < grid.length - 1; i++) {\n let resultRow = []\n for (let j = 1; j < grid[i].length - 1; j++) {\n\t\t//Finds the largest interger of the nine elements within each 3 x 3 sub-grid\n let max = Math.max(\n grid[i - 1][j - 1],\n grid[i - 1][j],\n grid[i - 1][j + 1],\n\n grid[i][j - 1],\n grid[i][j],\n grid[i][j + 1],\n\n grid[i + 1][j - 1],\n grid[i + 1][j],\n grid[i + 1][j + 1]\n )\n resultRow.push(max)\n }\n result.push(resultRow)\n }\n\t\n return result\n};\n``` | 3 | 0 | ['JavaScript'] | 1 |
largest-local-values-in-a-matrix | C++||Easy||Different Approach | ceasydifferent-approach-by-rohitsharma8-tq0g | To be honest i dont know how this approach came to my mind.\nBut it serves the purpose.\n\n\n vector<vector<int>> largestLocal(vector<vector<int>>& grid) {\n | rohitsharma8 | NORMAL | 2022-08-17T18:55:37.253393+00:00 | 2022-08-17T18:57:24.858693+00:00 | 462 | false | To be honest i dont know how this approach came to my mind.\nBut it serves the purpose.\n\n```\n vector<vector<int>> largestLocal(vector<vector<int>>& grid) {\n int n=grid.size();\n int it=0;\n while(it<2){\n for(int i=n-1;i>it;i--){\n for(int j=n-1;j>it;j--){\n grid[i][j]=max(grid[i][j],max(grid[i][j-1],max(grid[i-1][j],grid[i-1][j-1])));\n \n \n }\n }\n it++;\n }\n \n \n vector<vector<int>> ans( n-2 , vector<int> (n-2)); \n for(int i=2;i<n;i++){\n for(int j=2;j<n;j++){\n ans[i-2][j-2]=grid[i][j];\n }\n }\n return ans;\n }\n``` | 3 | 0 | ['C'] | 1 |
largest-local-values-in-a-matrix | Python || Priority Queue || Follow up || From LC239 | python-priority-queue-follow-up-from-lc2-5fmn | My intuition is this is a complexed 2-dim sliding window maximum extended from LC239. When I find the brute force solutions in the discuss, I feel like I am a i | xvv | NORMAL | 2022-08-14T21:24:43.361283+00:00 | 2022-08-14T21:47:02.720550+00:00 | 393 | false | My intuition is this is a complexed 2-dim sliding window maximum extended from LC239. When I find the brute force solutions in the discuss, I feel like I am a idiot... But I still want to post my answer since this can be used when k(the range) is not fixed. Welcome your comments.\nThe main idea is using a piority queue to firstly figure the maximum for every row/col, and then use the function again for the existing result col/row. I calculated the columns first in avoid of the need of switching rows&cols if the rows are calculated first.\n\n\n\n\nTC & SC: O(n^2). Please correct me if I am wrong:)\n\n```\nclass Solution:\n def largestLocal(self, grid: List[List[int]]) -> List[List[int]]:\n \n \n def largest(nums, k):\n res = []\n queue = []\n for i in range(k-1):\n heapq.heappush(queue, (-nums[i], i))\n \n for i in range(k-1, len(nums)):\n heapq.heappush(queue, (-nums[i], i))\n while queue[0][1] < i-k+1:\n heapq.heappop(queue)\n res.append(-queue[0][0])\n \n return res\n \n n = len(grid)\n res = []\n for j in range(n):\n col = [grid[i][j] for i in range(n)]\n res.append(largest(col, 3))\n \n ans = []\n for j in range(len(res[0])):\n row = [res[i][j] for i in range(n)]\n ans.append(largest(row, 3))\n \n return ans\n \n``` | 3 | 1 | ['Heap (Priority Queue)', 'Python'] | 2 |
largest-local-values-in-a-matrix | C++ || Optimised Solution || Sliding Window solution | c-optimised-solution-sliding-window-solu-v5fj | \nclass Solution {\npublic:\n vector<vector<int>> largestLocal(vector<vector<int>>& grid) {\n \n int n= grid.size();\n vector<vector<in | vineetkrtyagi | NORMAL | 2022-08-14T06:25:39.425799+00:00 | 2022-08-14T06:25:39.425831+00:00 | 242 | false | ```\nclass Solution {\npublic:\n vector<vector<int>> largestLocal(vector<vector<int>>& grid) {\n \n int n= grid.size();\n vector<vector<int>>v(n-2, vector<int>(n-2));\n for (int i=0; i<n-2; i++)\n {\n int temp1=0;\n int temp2=0;\n int temp3=0;\n \n for (int k=i; k<i+3; k++ )\n {\n temp1=max(temp1, grid[k][0]);\n temp2=max(temp2, grid[k][1]);\n temp3=max(temp3, grid[k][2]);\n }\n \n v[i][0]= max(temp1, max(temp2, temp3));\n for (int j=1; j<n-2; j++)\n {\n temp1= temp2;\n temp2= temp3;\n temp3= max(grid[i][j+2], max(grid[i+1][j+2],grid[i+2][j+2]));\n v[i][j]= max(temp1, max(temp2, temp3));\n }\n }\n return v;\n }\n};\n``` | 3 | 0 | ['C', 'Sliding Window'] | 0 |
largest-local-values-in-a-matrix | C++ || Optimized Solution || | c-optimized-solution-by-shailesh0302-d9xa | We know one row of our answer matrix would be formed using 3X3 Matrix of grid.\nSo, We traverse using a 3X3 matrix in the grid and fill matrix in Map with frequ | Shailesh0302 | NORMAL | 2022-08-14T06:02:58.488244+00:00 | 2022-08-14T07:51:27.855128+00:00 | 201 | false | We know one row of our answer matrix would be formed using 3X3 Matrix of grid.\nSo, We traverse using a 3X3 matrix in the grid and fill matrix in Map with frequencies and find maximum\n\nWe know map is sorted in ascending order of keys, So for finding maximum we access the last element of map.\n\nIn Traversing on vertically, \n 1. We first Fill the Map for 3X3 Matrix\n 2. Create Level (row for answer matrix)\n 3. Traverse Horizonatlly \n 1.1. Find maximum and push in level\n 1.2. Delete first column and add new column\n 1.3. Repeat above 2 steps till matrix is traversed horizontally \n 4. Push level in answer matrix\n 5. clear map\n 6. Repeat till n-2 rows are traversed \n""" \n \n\t\t class Solution {\n public:\n \t \n\t\t vector<vector<int>> largestLocal(vector<vector<int>>& grid) {\n \n\t\t //Using Map \t\t\n\t\t map<int,int> mp;\n \n int n=grid.size();\n\n vector<vector<int>> ans;\n \n for(int i=0;i<n-2;i++){\n\n //Filling up of Map with 3X3 Matrix \n for(int k=i;k<i+3;k++){\n for(int l=0;l<3;l++){\n mp[grid[k][l]]++;\n }\n }\n \n vector<int>level;\n\n //Traversing Using the Matrix \n for(int j=0;j<n-2;j++){\n map<int,int>::iterator it=mp.end();\n\n //Maximum Element of matrix \n if(mp.size()!=0){\n it--;\n level.push_back(it->first);\n }\n \n //Deleting First column of 3X3 Matrix \n mp[grid[i][j]]--;\n if(mp[grid[i][j]]==0) mp.erase(grid[i][j]);\n \n mp[grid[i+1][j]]--;\n if(mp[grid[i+1][j]]==0) mp.erase(grid[i+1][j]);\n \n mp[grid[i+2][j]]--;\n if(mp[grid[i+2][j]]==0) mp.erase(grid[i+2][j]);\n \n //Adding column for next 3X3 Matrix \n if(j<=n-4){\n mp[grid[i][j+3]]++;\n mp[grid[i+1][j+3]]++;\n mp[grid[i+2][j+3]]++;\n }\n }\n ans.push_back(level);\n //Clearing Map \n mp.clear();\n } \n return ans;\n \n }\n};\n""" | 3 | 0 | ['C'] | 0 |
largest-local-values-in-a-matrix | [Python] Very simple CLEAN code solution | Beats 100% | python-very-simple-clean-code-solution-b-b8pn | \n\n\n\nclass Solution:\n def largestLocal(self, grid):\n n = len(grid)\n local_values = []\n\n for row in range(n - 2):\n lo | kakinblu | NORMAL | 2022-08-14T04:24:38.634569+00:00 | 2022-08-14T04:25:05.920068+00:00 | 316 | false | \n\n\n```\nclass Solution:\n def largestLocal(self, grid):\n n = len(grid)\n local_values = []\n\n for row in range(n - 2):\n local_values.append([])\n for col in range(n - 2):\n row_one = max(grid[row][col : col + 3])\n row_two = max(grid[row + 1][col : col + 3])\n row_three = max(grid[row + 2][col : col + 3])\n\n max_local = max(row_one, row_two, row_three)\n local_values[row].append(max_local)\n\n return local_values\n``` | 3 | 0 | ['Python'] | 1 |
largest-local-values-in-a-matrix | Short & Concise | C++ | short-concise-c-by-tusharbhart-le9t | \nclass Solution {\npublic:\n vector<vector<int>> largestLocal(vector<vector<int>>& grid) {\n int n = grid.size();\n vector<vector<int>> ans(n | TusharBhart | NORMAL | 2022-08-14T04:01:34.228488+00:00 | 2022-08-14T04:01:34.228523+00:00 | 363 | false | ```\nclass Solution {\npublic:\n vector<vector<int>> largestLocal(vector<vector<int>>& grid) {\n int n = grid.size();\n vector<vector<int>> ans(n - 2, vector<int>(n - 2, 0));\n \n for(int i=0; i<n-2; i++) {\n for(int j=0; j<n-2; j++) {\n \n for(int r=i; r<i + 3; r++) {\n for(int c=j; c<j + 3; c++) ans[i][j] = max(ans[i][j], grid[r][c]);\n }\n \n }\n }\n return ans;\n }\n};\n``` | 3 | 0 | ['C'] | 0 |
largest-local-values-in-a-matrix | Easy to Understand | Clean code | easy-to-understand-clean-code-by-pulkit1-9n23 | \nclass Solution {\n public int[][] largestLocal(int[][] grid) {\n int n=grid.length;\n int ans[][]=new int[n-2][n-2];\n for(int i=0;i<n | pulkit161001 | NORMAL | 2022-08-14T04:01:27.772207+00:00 | 2022-08-14T04:04:49.804761+00:00 | 317 | false | ```\nclass Solution {\n public int[][] largestLocal(int[][] grid) {\n int n=grid.length;\n int ans[][]=new int[n-2][n-2];\n for(int i=0;i<n-2;i++){\n for(int j=0;j<n-2;j++){\n int max=0;\n for(int ii=i;ii<=i+2;ii++){\n for(int jj=j;jj<=j+2;jj++){\n // System.out.println(ii+" "+jj);\n max=Math.max(grid[ii][jj],max);\n }\n }\n ans[i][j]=max;\n }\n }\n return ans;\n }\n}\n``` | 3 | 0 | ['Java'] | 1 |
largest-local-values-in-a-matrix | Sliding window solution | sliding-window-solution-by-drgavrikov-bogc | Approach\n Describe your approach to solving the problem. \nTo calculate the maximum 3x3 matrix for each row, you can use a deque structure and store three maxi | drgavrikov | NORMAL | 2024-05-12T21:38:05.841248+00:00 | 2024-06-02T07:35:56.785613+00:00 | 33 | false | # Approach\n<!-- Describe your approach to solving the problem. -->\nTo calculate the maximum 3x3 matrix for each row, you can use a deque structure and store three maximum values for each column in it. This way, for the next column, you can avoid recalculating already calculated maximums. It\'s sufficient to remove the left element from the deque and add the maximum for the column that hasn\'t been considered yet.\n\nThis solution is slightly better than the trivial idea of calculating the maximum from the 3x3 matrix for each cell.\n\n\n# Complexity\n- Time complexity: $O(N^2)$\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity: $O(N^2)$ additional memory for responce.\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```\nclass Solution {\npublic:\n vector<vector<int>> largestLocal(vector<vector<int>> &grid) {\n auto size = grid.size();\n auto result = vector(size - 2, vector<int>(size - 2));\n for (size_t row = 0; row < size - 2; ++row) {\n std::deque<int> deque;\n deque.push_back(std::max(grid[row][0], std::max(grid[row + 1][0], grid[row + 2][0])));\n deque.push_back(std::max(grid[row][1], std::max(grid[row + 1][1], grid[row + 2][1])));\n deque.push_back(std::max(grid[row][2], std::max(grid[row + 1][2], grid[row + 2][2])));\n\n result[row][0] = *std::max_element(deque.begin(), deque.end());\n\n for (size_t col = 1; col < size - 2; ++col) {\n deque.pop_front();\n deque.push_back(std::max(grid[row][col + 2], std::max(grid[row + 1][col + 2], grid[row + 2][col + 2])));\n\n result[row][col] = *std::max_element(deque.begin(), deque.end());\n }\n }\n return result;\n }\n};\n\n```\n\nMost of my solutions are in [C++](https://github.com/drgavrikov/leetcode-cpp) and [Kotlin](https://github.com/drgavrikov/leetcode-jvm) on my [Github](https://github.com/drgavrikov). | 2 | 0 | ['Sliding Window', 'Matrix', 'C++'] | 0 |
largest-local-values-in-a-matrix | C# Solution for Largest Local Values In a Matrix Problem | c-solution-for-largest-local-values-in-a-qi3e | Intuition\n Describe your first thoughts on how to solve this problem. \nThe intuition behind this C# solution remains the same as the Java solution: iterate ov | Aman_Raj_Sinha | NORMAL | 2024-05-12T19:01:21.235166+00:00 | 2024-05-12T19:03:34.824727+00:00 | 211 | false | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nThe intuition behind this C# solution remains the same as the Java solution: iterate over every possible 3x3 submatrix in the given grid and find the maximum value within each submatrix.\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\u2022\tThe LargestLocal method iterates over each possible starting position of a 3x3 submatrix in the grid and uses the FindMaxInSubmatrix method to find the maximum value within each submatrix.\n\u2022\tThe FindMaxInSubmatrix method iterates over the 3x3 submatrix starting at the given row and column indices and finds the maximum value.\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\u2022\tLet \u2018n\u2019 be the size of the grid.\n\u2022\tThe time complexity of the LargestLocal method is approximately O(n^2) for scanning the grid and O(9) for each call to FindMaxInSubmatrix, resulting in overall O(n^2).\n\u2022\tThe time complexity of the FindMaxInSubmatrix method is O(9), as it iterates over a 3x3 submatrix.\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\nThe space complexity is O((n-2) * (n-2)) for storing the output matrix maxLocal, as it\u2019s a matrix of size (n-2) x (n-2), where \u2018n\u2019 is the size of the input grid. Additionally, space is required for the variables used in the methods, which is constant.\n\n# Code\n```\npublic class Solution {\n public int[][] LargestLocal(int[][] grid) {\n int n = grid.Length;\n int[][] maxLocal = new int[n - 2][];\n \n for (int i = 0; i < n - 2; i++) {\n maxLocal[i] = new int[n - 2];\n for (int j = 0; j < n - 2; j++) {\n maxLocal[i][j] = FindMaxInSubmatrix(grid, i, j);\n }\n }\n \n return maxLocal;\n }\n\n private int FindMaxInSubmatrix(int[][] grid, int row, int col) {\n int max = int.MinValue;\n for (int i = row; i < row + 3; i++) {\n for (int j = col; j < col + 3; j++) {\n max = Math.Max(max, grid[i][j]);\n }\n }\n return max;\n }\n}\n``` | 2 | 0 | ['C#'] | 0 |
largest-local-values-in-a-matrix | Java Solution for Largest Local Values In a Matrix Problem | java-solution-for-largest-local-values-i-nve9 | Intuition\n Describe your first thoughts on how to solve this problem. \nThe intuition behind the solution is to iterate over every possible 3x3 submatrix in th | Aman_Raj_Sinha | NORMAL | 2024-05-12T18:58:20.782308+00:00 | 2024-05-12T18:58:20.782331+00:00 | 25 | false | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nThe intuition behind the solution is to iterate over every possible 3x3 submatrix in the given grid and find the maximum value within each submatrix. This maximum value is then stored in the corresponding position in the output mat\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\nThe approach involves nested loops to iterate over each possible starting position of a 3x3 submatrix, then nested loops within that to find the maximum value within each submatrix.\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\u2022\tLet \u2018n\u2019 be the size of the grid.\n\u2022\tSince we iterate over each cell of the grid and for each cell, we iterate over a 3x3 submatrix, the time complexity is approximately O(n^2) for scanning the grid and O(9) for finding the maximum value within each submatrix, resulting in overall O(n^2).\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\nThe space complexity is O((n-2) * (n-2)) for storing the output matrix maxLocal, as it\u2019s a matrix of size (n-2) x (n-2), where \u2018n\u2019 is the size of the input grid.\n\n# Code\n```\nclass Solution {\n public int[][] largestLocal(int[][] grid) {\n int n = grid.length;\n int[][] maxLocal = new int[n - 2][n - 2];\n \n for (int i = 0; i < n - 2; i++) {\n for (int j = 0; j < n - 2; j++) {\n int max = Integer.MIN_VALUE;\n for (int k = i; k < i + 3; k++) {\n for (int l = j; l < j + 3; l++) {\n max = Math.max(max, grid[k][l]);\n }\n }\n maxLocal[i][j] = max;\n }\n }\n \n return maxLocal;\n }\n}\n``` | 2 | 0 | ['Java'] | 0 |
largest-local-values-in-a-matrix | 🔥Brute Force - Beginner Friendly | Clean Code | C++ | | brute-force-beginner-friendly-clean-code-11rb | Code\n\nclass Solution {\npublic:\n vector<vector<int>> largestLocal(vector<vector<int>>& grid) {\n int n = grid.size();\n vector<vector<int>> | Antim_Sankalp | NORMAL | 2024-05-12T14:22:06.606352+00:00 | 2024-05-12T14:22:06.606385+00:00 | 11 | false | # Code\n```\nclass Solution {\npublic:\n vector<vector<int>> largestLocal(vector<vector<int>>& grid) {\n int n = grid.size();\n vector<vector<int>> res(n - 2, vector<int>(n - 2));\n\n for (int i = 0; i < n - 2; i++)\n {\n for (int j = 0; j < n - 2; j++)\n {\n int curr = 0;\n \n for (int k = 0; k < 3; k++)\n {\n for (int l = 0; l < 3; l++)\n {\n curr = max(curr, grid[i + k][j + l]);\n }\n }\n\n res[i][j] = curr;\n }\n }\n\n return res;\n }\n};\n``` | 2 | 0 | ['C++'] | 0 |
largest-local-values-in-a-matrix | ✅Easiest Solution🔥||Beat 100%🔥||Beginner Friendly✅🔥 | easiest-solutionbeat-100beginner-friendl-laxo | Intuition\nThe code aims to find the largest local value within a 3x3 grid for each position (excluding the border) in the input grid.\nIt iterates through each | siddhesh11p | NORMAL | 2024-05-12T14:21:30.757377+00:00 | 2024-05-12T14:21:30.757409+00:00 | 455 | false | # Intuition\nThe code aims to find the largest local value within a 3x3 grid for each position (excluding the border) in the input grid.\nIt iterates through each position in the input grid, calculates the maximum value within the corresponding 3x3 subgrid, and stores it in the output array maxl.\n\n# Complexity\n- Time complexity:\n$$O(n^2)$$\n\n- Space complexity:\n$$O(n^2)$$\n\n# Code\n```\nclass Solution {\n public int[][] largestLocal(int[][] grid) {\n\n int n = grid.length;\n int m = n-2;\n\n int [][]maxl = new int [m][m];\n int max = 0;\n\n for(int i = 0; i<m;i++)\n {\n for(int j = 0 ;j<m;j++)\n {\n\n max = 0;\n for(int k = i;k<i+3;k++)\n {\n for(int l = j;l<j+3;l++)\n {\n max = Math.max(max,grid[k][l]);\n }\n }\n maxl[i][j] = max;\n\n }\n }\n \n return maxl;\n }\n}\n``` | 2 | 0 | ['Array', 'Matrix', 'Java'] | 2 |
largest-local-values-in-a-matrix | Beats 100% || Easy to understand || Clean & concise | beats-100-easy-to-understand-clean-conci-5t3t | # Intuition \n\n\n\n\n\n\n# Complexity\n- Time complexity: O(n^2)\n\n\n- Space complexity: O(n^2)\n\n\n# Code\n\nclass Solution {\n public int[][] largestL | psolanki034 | NORMAL | 2024-05-12T12:55:07.406749+00:00 | 2024-05-12T12:55:07.406776+00:00 | 468 | false | <!-- # Intuition -->\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n<!-- # Approach -->\n<!-- Describe your approach to solving the problem. -->\n\n\n# Complexity\n- Time complexity: O(n^2)\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity: O(n^2)\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```\nclass Solution {\n public int[][] largestLocal(int[][] grid) {\n int [][]result=new int [grid.length-2][grid.length-2];\n for(int row=0;row<grid.length-2;row++)\n {\n for(int col=0;col<grid.length-2;col++)\n {\n result[row][col]=maxvalue(grid,row,col);\n }\n }\n return result;\n \n }\n public int maxvalue(int [][]grid,int row,int col)\n {\n int max=Integer.MIN_VALUE;\n for(int i=row;i<=row+2;i++)\n {\n for(int j=col;j<=col+2;j++)\n {\n max=Math.max(max,grid[i][j]);\n }\n }\n return max;\n }\n}\n\n```\n\n\n``` | 2 | 0 | ['C', 'Python', 'C++', 'Java', 'JavaScript'] | 2 |
largest-local-values-in-a-matrix | Simple C solution | simple-c-solution-by-maxis_c-sklv | Complexity\n- Time complexity: O(n^2)\n\n- Space complexity: O(1) (not considering maxLocal)\n\n# Code\n\n/**\n * Return an array of arrays of size *returnSize. | Maxis_C | NORMAL | 2024-05-12T09:53:40.398093+00:00 | 2024-05-12T09:53:40.398123+00:00 | 122 | false | # Complexity\n- Time complexity: O(n^2)\n\n- Space complexity: O(1) (not considering maxLocal)\n\n# Code\n```\n/**\n * Return an array of arrays of size *returnSize.\n * The sizes of the arrays are returned as *returnColumnSizes array.\n * Note: Both returned array and *columnSizes array must be malloced, assume caller calls free().\n */\n\nint biggestNum3x3(int**mat, int cI, int cJ) {\n int max = INT_MIN;\n\n for(int i = 0; i < 3; i++) {\n for(int j = 0; j < 3; j++) {\n max = max < mat[i + cI][j + cJ] ? mat[i + cI][j + cJ] : max;\n }\n }\n\n return max;\n \n}\n\nint** largestLocal(int** grid, int gridSize, int* gridColSize, int* returnSize, int** returnColumnSizes) {\n int n = gridSize;\n int m = n - 2;\n *returnSize = m;\n *returnColumnSizes = (int *)malloc(m * sizeof(int));\n \n int **maxLocal = (int **)malloc(m * sizeof(int *));\n for(int i = 0; i < m; i++) {\n maxLocal[i] = (int *)malloc(m * sizeof(int));\n (*returnColumnSizes)[i] = m;\n }\n\n for(int i = 0; i < m; i++) {\n for(int j = 0; j < m; j++) {\n maxLocal[i][j] = biggestNum3x3(grid,i,j);\n }\n }\n\n return maxLocal;\n}\n``` | 2 | 0 | ['C'] | 0 |
largest-local-values-in-a-matrix | Neighborhood Maxima Finder | neighborhood-maxima-finder-by-mehul11-what | Intuition\n Describe your first thoughts on how to solve this problem. \nBy scanning through the grid and zooming into small regions, it pinpoints the peak valu | Mehul11 | NORMAL | 2024-05-12T09:22:30.167959+00:00 | 2024-05-12T09:22:30.167980+00:00 | 13 | false | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nBy scanning through the grid and zooming into small regions, it pinpoints the peak value within each local area. This approach is handy for tasks like identifying prominent features or hotspots within a dataset, enabling efficient analysis of spatial patterns in applications such as image processing or geographical mapping.\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\nTo solve the problem, we\'ll traverse the grid, examining each 3x3 neighborhood. At each position, we\'ll find the maximum value within the local area and store it in a result matrix. This way, we ensure that we capture the highest value within each neighborhood, providing insights into localized peaks within the grid. Handling edge cases where the neighborhood extends beyond the grid boundaries is essential for completeness. Overall, this approach efficiently identifies local maxima, making it suitable for tasks requiring neighborhood analysis.\n\n# Complexity\n- Time complexity:\n$$O(n^2)$$\n\n- Space complexity:\n$$O(n^2)$$\n\n# Code\n```\nclass Solution:\n def largestLocal(self, grid: List[List[int]]) -> List[List[int]]:\n def solve(temp):\n maxi = temp[0][0]\n for row in temp:\n for el in row:\n if el > maxi:\n maxi = el\n return maxi\n res=[]\n for i in range(len(grid)-2):\n te=[]\n for j in range(len(grid[0])-2):\n temp = [row[j:j+3] for row in grid[i:i+3]]\n x=solve(temp)\n te.append(x)\n res.append(te)\n return res\n``` | 2 | 0 | ['Array', 'Math', 'Divide and Conquer', 'Recursion', 'Python', 'Python3', 'Pandas'] | 0 |
largest-local-values-in-a-matrix | Faster, Cheaper, Elegant Sliding Window Solution - Python✔️ | faster-cheaper-elegant-sliding-window-so-i59d | Intuition\nMy initial thoughts were to avoid using nested loops by employing a sliding window approach to find the maximum value for each 3x3 submatrix horizont | Sacha_924 | NORMAL | 2024-05-12T08:51:01.934197+00:00 | 2024-05-12T08:51:01.934225+00:00 | 251 | false | # Intuition\nMy initial thoughts were to avoid using nested loops by employing a sliding window approach to find the maximum value for each 3x3 submatrix horizontally. Then, I planned to transpose the resulting matrix to apply the same sliding window vertically. Rather than implementing another sliding window for each column, I chose to transpose the matrix, perform the computation, and then transpose it back.\n\n\n\n# Complexity\n- Time complexity:$$O(n\xB2)$$\n\n# Code\n```python\nclass Solution:\n def largestLocal(self, grid: List[List[int]]) -> List[List[int]]:\n size, windowSize, max_horiz_matrix, resT = len(grid), 3, [], []\n\n for row in grid:\n max_horiz_matrix.append(self.max_sliding_window(row, windowSize))\n \n for row in self.transpose(max_horiz_matrix):\n resT.append(self.max_sliding_window(row, windowSize))\n\n return self.transpose(resT)\n \n def transpose(self, matrix):\n return [[matrix[j][i] for j in range(len(matrix))] for i in range(len(matrix[0]))]\n\n def max_sliding_window(self,nums, k):\n result = []\n window = deque()\n\n for i in range(k):\n while window and nums[i] >= nums[window[-1]]:\n window.pop()\n window.append(i)\n\n for i in range(k, len(nums)):\n result.append(nums[window[0]])\n while window and window[0] <= i - k:\n window.popleft()\n while window and nums[i] >= nums[window[-1]]:\n window.pop()\n window.append(i)\n\n result.append(nums[window[0]])\n\n return result\n``` | 2 | 0 | ['Python3'] | 2 |
largest-local-values-in-a-matrix | Python Super Simple O(1), O(N^2) solution with a sprinkle of DP | python-super-simple-o1-on2-solution-with-ojxb | Intuition\nI recommend to check the code while reading the explanaition. This is a problem where the code explains itself better than comments.\n\n Describe you | merab_m_ | NORMAL | 2024-05-12T08:03:27.236484+00:00 | 2024-05-12T08:03:27.236538+00:00 | 106 | false | # Intuition\nI recommend to check the code while reading the explanaition. This is a problem where the code explains itself better than comments.\n\n<!-- Describe your first thoughts on how to solve this problem. -->\n**Using a little of Dynamic Programming:** \nBrute force would be to compute 3x3 matrix every time. However we don\'t need to do it every time. Instead compute 3x1 columns and remember last 3 columns. \n\nThis way you will beat 95% of python users.\n\n**Saving result in grid:** \nSay current indices are `i, j`. Then, once maximum of 3 columns is computed it can be stored at `grid[i][j] ` because we will never need `grid[i][j]` in future.\n\nWhat is left is to pop 2 elements in the end of each row of `grid[i]`. And to pop 2 rows in the end of `grid`.\n\n# Complexity\n- Time complexity: O(N^2)\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity: O(1)\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```\nclass Solution:\n def largestLocal(self, grid: List[List[int]]) -> List[List[int]]:\n\n N = len(grid)\n for i in range(N - 2):\n for j in range(N - 2):\n if j == 0:\n maxFirst = max(grid[i][j], grid[i + 1][j], grid[i + 2][j])\n maxSecond = max(grid[i][j + 1], grid[i + 1][j + 1], grid[i + 2][j + 1])\n maxThird = max(grid[i][j + 2], grid[i + 1][j + 2], grid[i + 2][j + 2])\n else:\n maxNext = max(grid[i][j + 2], grid[i + 1][j + 2], grid[i + 2][j + 2])\n maxFirst, maxSecond, maxThird = maxSecond, maxThird, maxNext\n\n grid[i][j] = max(maxFirst, maxSecond, maxThird)\n \n grid[i].pop()\n grid[i].pop()\n\n grid.pop()\n grid.pop()\n \n\n return grid\n \n``` | 2 | 0 | ['Python3'] | 2 |
largest-local-values-in-a-matrix | Very Intuitive || Easy 2 For loop Solution ⭐✨ | very-intuitive-easy-2-for-loop-solution-w82qt | Problem Description\n\nGiven a 2D grid of integers, find the largest element in each 3x3 local region within the grid.\n\n## Intuition\nThe problem seems to be | _Rishabh_96 | NORMAL | 2024-05-12T07:07:24.400874+00:00 | 2024-05-12T07:07:24.400902+00:00 | 254 | false | # Problem Description\n\nGiven a 2D grid of integers, find the largest element in each 3x3 local region within the grid.\n\n## Intuition\nThe problem seems to be asking for finding the largest element in each 3x3 local region within the given grid. We can iterate over each cell in the grid and find the maximum value within its 3x3 neighborhood.\n\n## Approach\n1. Iterate over each cell in the grid, excluding the boundary cells (i.e., the outermost layer of cells).\n2. For each cell, find the maximum value among the cell itself and its eight neighboring cells.\n3. Store the maximum value in the corresponding position in the result grid.\n\n## Complexity\n- Time complexity: O(n^2), where n is the size of the grid. We iterate over each cell in the grid once.\n- Space complexity: O(n^2) for storing the result grid, where n is the size of the input grid.\n\n# Code\n```cpp\nclass Solution{\npublic: \n\n vector<vector<int>> largestLocal(vector<vector<int>>& grid) {\n \n int n = grid.size();\n vector<vector<int>> res(n - 2, vector<int>(n - 2));\n\n for(int i = 1; i <= n - 2; i++) {\n for(int j = 1; j <= n - 2; j++) {\n int maxi = 0;\n maxi = max({maxi, grid[i-1][j-1], grid[i-1][j], grid[i-1][j+1], grid[i][j-1], grid[i][j], grid[i][j+1], grid[i+1][j-1], grid[i+1][j], grid[i+1][j+1]});\n \n res[i-1][j-1] = maxi;\n }\n }\n return res;\n }\n};\n | 2 | 0 | ['Array', 'Matrix', 'C++'] | 0 |
largest-local-values-in-a-matrix | Simple Java Solution || Beats 100% | simple-java-solution-beats-100-by-saad_h-bo2n | Intuition\n Describe your first thoughts on how to solve this problem. \n\n# Approach\n Describe your approach to solving the problem. \n\n# Complexity\n- Time | saad_hussain_ | NORMAL | 2024-05-12T06:42:57.998495+00:00 | 2024-05-12T06:42:57.998534+00:00 | 9 | false | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```\nclass Solution {\n public int[][] largestLocal(int[][] grid) {\n int n = grid.length;\n int[][] res = new int[n-2][n-2]; \n for(int i=0;i<n-2;i++){\n for(int j=0;j<n-2;j++){\n res[i][j]=find_max(grid,i,j);\n }\n }\n return res;\n }\n\n private int find_max(int[][] grid,int r,int c){\n int value=Integer.MIN_VALUE;\n for(int i=r;i<r+3;i++){\n for(int j=c;j<c+3;j++){\n value=Math.max(value,grid[i][j]);\n }\n }\n return value;\n }\n}\n``` | 2 | 0 | ['Java'] | 0 |
largest-local-values-in-a-matrix | Largest Local Values in a Matrix (Easy Solution with Detailed Explanation) | largest-local-values-in-a-matrix-easy-so-2aaj | Intuition\n Describe your first thoughts on how to solve this problem. \nCreate a submatrix for each position, calculate the max element and store it in result | MohitBhatt-16 | NORMAL | 2024-05-12T05:12:15.768435+00:00 | 2024-05-12T05:12:15.768481+00:00 | 33 | false | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nCreate a submatrix for each position, calculate the max element and store it in result matrix\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n1. Initialize n with length of grid.\n2. Initialize result matrix res of n-2 size and with all zero value.\n3. Run a for loop from i = 0 to n-2 this is for the row number of result matrix : \n 1. Run a for loop from j = 0 to n-2 this is for the column number of result matrix : \n 1. Now for each i,j position in result matrix we will form a 3*3 submatrix from the original grid matrix and check for the max value for that position\n 2. Run a for loop from r = i to i+3 for rows in the submatirx :\n 1. Run a for loop from c = j to j+3 for columns in the submatrix: \n 1. Find the max value and store it in i,j position in res\n4. Return res\n\n# Complexity\n- Time complexity : O(n^2) becuase the last two loops will always run for 9 times\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity : O(n^2) for result matrix\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```\nclass Solution(object):\n def largestLocal(self, grid):\n """\n :type grid: List[List[int]]\n :rtype: List[List[int]]\n """\n n = len(grid)\n res = [[0]*(n-2) for _ in range(n-2)]\n for i in range(n-2):\n for j in range(n-2):\n for r in range(i,i+3):\n for c in range(j,j+3):\n res[i][j] = max(res[i][j],grid[r][c])\n return res\n``` | 2 | 0 | ['Python'] | 0 |
largest-local-values-in-a-matrix | largest-local-values-in-a-matrix || Simple and Intuitive Solution | largest-local-values-in-a-matrix-simple-cluux | Implementation based Question + Logic\n\n# Code\n\nclass Solution {\npublic:\n vector<vector<int>> largestLocal(vector<vector<int>>& grid) {\n int n = | Ashish_Ujjwal | NORMAL | 2024-05-12T04:49:30.767838+00:00 | 2024-05-12T04:49:30.767863+00:00 | 2 | false | # Implementation based Question + Logic\n\n# Code\n```\nclass Solution {\npublic:\n vector<vector<int>> largestLocal(vector<vector<int>>& grid) {\n int n = grid.size();\n vector<vector<int>>res(n-2, vector<int>(n-2));\n for(int i=1; i<=n-2; i++){\n for(int j=1; j<=n-2; j++){\n int maxi = 0;\n maxi = max({maxi, grid[i-1][j-1], grid[i-1][j], grid[i-1][j+1]});\n maxi = max({maxi, grid[i][j-1], grid[i][j], grid[i][j+1]});\n maxi = max({maxi, grid[i+1][j-1], grid[i+1][j], grid[i+1][j+1]});\n\n res[i-1][j-1] = maxi;\n }\n }\n return res;\n }\n};\n```\n\n | 2 | 0 | ['C++'] | 0 |
largest-local-values-in-a-matrix | Java Clean Solution | Daily Challanges | java-clean-solution-daily-challanges-by-v5hyz | Complexity\n- Time complexity:O(n^3)\n Add your time complexity here, e.g. O(n) \n\n- Space complexity:O(r*c)\n Add your space complexity here, e.g. O(n) \n\n# | Shree_Govind_Jee | NORMAL | 2024-05-12T04:21:08.753912+00:00 | 2024-05-12T04:21:08.753937+00:00 | 766 | false | # Complexity\n- Time complexity:$$O(n^3)$$\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:$$O(r*c)$$\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```\nclass Solution {\n private int helper(int[][] grid, int i, int j) {\n int maxi = Integer.MIN_VALUE;\n int r = i + 3, c = j + 3;\n for (int p = i; p < r; p++) {\n for (int q = j; q < c; q++) {\n maxi = Math.max(maxi, grid[p][q]);\n }\n }\n return maxi;\n }\n\n public int[][] largestLocal(int[][] grid) {\n int r = grid.length - 2;\n int c = grid[0].length - 2;\n\n int[][] res = new int[r][c];\n for (int i = 0; i < r; i++) {\n for (int j = 0; j < c; j++) {\n res[i][j] = helper(grid, i, j);\n }\n }\n return res;\n }\n}\n``` | 2 | 0 | ['Array', 'Matrix', 'Java'] | 0 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.