(As the direct problem statement from LeetCode isn’t available, we can infer a reasonable problem based on typical grid-related conditions.)
Check if Grid Satisfies Conditions
Given an m x n
grid, determine if the grid satisfies certain conditions provided below. You have to write a function to check if the grid follows:
Function Signature:
def checkGridConditions(grid: List[List[int]]) -> bool:
Example:
grid = [[2, 4], [4, 2]]
Output: True
grid = [[1, 2], [3, 4]]
False
Constraints:
from typing import List
def checkGridConditions(grid: List[List[int]]) -> bool:
m, n = len(grid), len(grid[0])
# Check Rows
for i in range(m):
seen = set()
for j in range(n):
if grid[i][j] % 2 != 0 or grid[i][j] in seen:
return False
seen.add(grid[i][j])
# Check Columns
for j in range(n):
seen = set()
for i in range(m):
if grid[i][j] % 2 != 0 or grid[i][j] in seen:
return False
seen.add(grid[i][j])
return True
# Time Complexity:
# - O(m * n): It involves iterating through each cell of the m x n grid once for rows check and once for columns check.
# Example Usage:
if __name__ == "__main__":
grid1 = [[2, 4], [4, 2]]
print(checkGridConditions(grid1)) # Output: True
grid2 = [[1, 2], [3, 4]]
print(checkGridConditions(grid2)) # Output: False
This approach ensures every number is checked twice (once row-wise and once column-wise), but it avoids unnecessary nested loops and thus maintains efficiency. Each element is checked once per row and per column, rendering an overall O(m * n)
time complexity.
Got blindsided by a question you didn’t expect?
Spend too much time studying?
Or simply don’t have the time to go over all 3000 questions?