Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save SuryaPratapK/2b6b4b1971f6532aa7f5a97aeb133d11 to your computer and use it in GitHub Desktop.

Select an option

Save SuryaPratapK/2b6b4b1971f6532aa7f5a97aeb133d11 to your computer and use it in GitHub Desktop.
class Solution {
public:
int minimumArea(vector<vector<int>>& grid) {
int m = grid.size();
int n = grid[0].size();
int low_x = INT_MAX, high_x = -1;
int low_y = INT_MAX, high_y = -1;
for(int i=0;i<m;++i){
for(int j=0;j<n;++j){
if(grid[i][j]==1){
low_x = min(low_x,i);
high_x = max(high_x,i);
low_y = min(low_y,j);
high_y = max(high_y,j);
}
}
}
return (high_x-low_x+1) * (high_y-low_y+1);
}
};
/*
//JAVA
class Solution {
public int minimumArea(int[][] grid) {
if (grid == null || grid.length == 0 || grid[0].length == 0) return 0;
int m = grid.length;
int n = grid[0].length;
int lowX = Integer.MAX_VALUE, highX = -1;
int lowY = Integer.MAX_VALUE, highY = -1;
for (int i = 0; i < m; ++i) {
for (int j = 0; j < n; ++j) {
if (grid[i][j] == 1) {
lowX = Math.min(lowX, i);
highX = Math.max(highX, i);
lowY = Math.min(lowY, j);
highY = Math.max(highY, j);
}
}
}
if (lowX == Integer.MAX_VALUE) return 0; // no 1s
return (highX - lowX + 1) * (highY - lowY + 1);
}
}
#Python
from typing import List
class Solution:
def minimumArea(self, grid: List[List[int]]) -> int:
if not grid or not grid[0]:
return 0
m, n = len(grid), len(grid[0])
low_x, high_x = float('inf'), -1
low_y, high_y = float('inf'), -1
for i in range(m):
for j in range(n):
if grid[i][j] == 1:
low_x = min(low_x, i)
high_x = max(high_x, i)
low_y = min(low_y, j)
high_y = max(high_y, j)
if low_x == float('inf'):
return 0 # no 1s
return (high_x - low_x + 1) * (high_y - low_y + 1)
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment