Flipping an Image - Problem
You're given an n x n binary matrix representing a black and white image. Your task is to flip the image horizontally, then invert it, and return the resulting image.
What does "flip horizontally" mean?
Each row of the image is reversed. For example, flipping [1,1,0] horizontally results in [0,1,1].
What does "invert" mean?
Each 0 is replaced by 1, and each 1 is replaced by 0. For example, inverting [0,1,1] results in [1,0,0].
Think of this like taking a photo negative and then flipping it like you would flip a pancake!
Input & Output
example_1.py — Basic 3x3 matrix
$
Input:
image = [[1,1,0],[1,0,1],[0,0,0]]
›
Output:
[[1,0,0],[0,1,0],[1,1,1]]
💡 Note:
First flip: [[0,1,1],[1,0,1],[0,0,0]], then invert: [[1,0,0],[0,1,0],[1,1,1]]
example_2.py — 4x4 matrix
$
Input:
image = [[1,1,0,0],[1,0,0,1],[0,1,1,1],[1,0,1,0]]
›
Output:
[[1,1,0,0],[0,1,1,0],[0,0,0,1],[1,0,1,0]]
💡 Note:
Each row is flipped horizontally then inverted: [1,1,0,0] → [0,0,1,1] → [1,1,0,0]
example_3.py — Single element
$
Input:
image = [[1]]
›
Output:
[[0]]
💡 Note:
Single element matrix: flip [1] → [1], then invert [1] → [0]
Constraints
- n == image.length
- n == image[i].length
- 1 ≤ n ≤ 20
- image[i][j] is either 0 or 1
Visualization
Tap to expand
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code