Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- def count_negative_numbers(matrix):
- count = 0
- row = 0
- col = len(matrix[0]) - 1 # Start from the top-right corner
- while row < len(matrix) and col >= 0:
- if matrix[row][col] < 0:
- count += col + 1 # Add count of negative numbers in this column
- row += 1 # Move to the next row
- else:
- col -= 1 # Move to the previous column
- return count
- # Test the function with the provided example
- grid = [[4, 3, 2, -1], [3, 2, 1, -1], [1, 1, -1, -2], [-1, -1, -2, -3]]
- print(count_negative_numbers(grid)) # Output should be 8
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement