Write a function in Python that calculates the maximum value in a 2-dimensional array. […
- User
Write a function in Python that calculates the maximum value in a 2-dimensional array. [ [1, 4, 8], [7, 9, 6] ]
Assistant
Write a function in Python that calculates the maximum value in a 2-dimensional array. [ [1, 4, 8], [7, 9, 6] ]
Problem: Write a function in Python that calculates the maximum value in a 2-dimensional array. [ [1, 4, 8], [7, 9, 6] ]
Approach: Selection
Key considerations:
| Approach | Time Complexity | Space Complexity | Readabil
Edge Case Handling
Empty array: Return None with clear documentation
Empty sub-arrays: Skip gracefully
Type flexibility: Support any comparabl
Args: array_2d: List of lists containing comparable elements
Returns: The maximum value found, or None if array is empty
Complexity: | Space Complexity | Readability |
Let me implement this solution.
I'll implement multiple approaches to demonstrate different trade-offs:
| Approach | Time Complexity | Space Complexity | Readability |
|---|---|---|---|
Generator + built-in max() | O(n×m) | O(1) | High |
| Nested loops | O(n×m) | O(1) | Medium |
| Flatten then find max | O(n×m) | O(n×m) | High |
Winner: Generator expression with max() — optimal in all categories.
None with clear documentationFlattening creates an intermediate list, wasting O(n×m) memory. A generator expression yields values lazily, using constant space.
def find_max_2d(array_2d):
"""
Find the maximum value in a 2-dimensional array.
Args:
array_2d: List of lists containing comparable elements
Returns:
The maximum value found, or None if array is empty
Examples:
>>> find_max_2d([[1, 4, 8], [7, 9, 6]])
9
>>> find_max_2d([[-5, -2], [-10, -3]])
-2
>>> find_max_2d([])
None
"""
if not array_2d:
return None
# Generator expression: yields each element from each row
# Avoids creating intermediate flattened list
return max(item for row in array_2d for item in row)
# Alternative: Explicit nested loops (more control for edge cases)
def find_max_2d_verbose(array_2d):
"""
Find maximum using explicit loops with granular edge case handling.
"""
if not array_2d:
return None
max_value = None
found_any = False
for row in array_2d:
if not row: # Skip empty rows
continue
for item in row:
if not found_any or item > max_value:
max_value = item
found_any = True
return max_value
# Demonstration
if __name__ == "__main__":
test_array = [
[1, 4, 8],
[7, 9, 6]
]
result = find_max_2d(test_array)
print(f"Maximum value: {result}") # Output: Maximum value: 9
# Additional test cases
print(f"Empty array: {find_max_2d([])}") # Output: None
print(f"Negative values: {find_max_2d([[-5, -2], [-10, -3]])}") # Output: -2
print(f"Single element: {find_max_2d([[42]])}") # Output: 42
Maximum value: 9
Empty array: None
Negative values: -2
Single element: 42
| Decision | Rationale |
|---|---|
| Generator expression | Memory-efficient lazy evaluation |
Return None for empty | Explicit handling, avoids exceptions |
| Single-pass algorithm | O(n×m) time, no nested max() calls per row |
| Documented docstrings | Clear API contract for users |