MediumNo. 0957
Prison Cells After N Days — LeetCode 957 Python Solution
- Pattern
- Bit Manipulation
- Topics
- Bit Manipulation, Array, Hash Table, Math
- Reading time
- 2 min
- Source
- leetcode.com
The problem
There are 8 prison cells in a row and each cell is either occupied or vacant. Each day, whether the cell is occupied or vacant changes according to the following rules: If a cell has two adjacent neighbors that are both occupied or both vacant, then the cell becomes occupied.
Example
- Input
- cells = [0,1,0,1,1,0,0,1], n = 7
- Output
- [0,0,1,1,0,0,0,0]
- Explanation
- The following table summarizes the state of the prison on each day:
Complexity
- Time
- O(n)
- Space · auxiliary
- O(n)
Pattern: Bit Manipulation
Use XOR, masks and the low-bit trick to replace whole data structures with an integer. LeetCode 957. Prison Cells After N Days is filed here because LeetCode tags it Bit Manipulation, which is the vocabulary this hub collects.
The bit manipulation guide has the Python template for the pattern and the 194 LeetCode problems that use it.
Related problems
| No. | Problem | Difficulty |
|---|---|---|
| 0268 | Missing Number | Easy |
| 0996 | Number of Squareful Arrays | Hard |
| 1442 | Count Triplets That Can Form Two Arrays of Equal XOR | Medium |
| 1994 | The Number of Good Subsets | Hard |
| 0952 | Largest Component Size by Common Factor | Hard |
| 0963 | Minimum Area Rectangle II | Medium |
Frequently asked questions
- How hard is LeetCode 957. Prison Cells After N Days?
LeetCode 957. Prison Cells After N Days is rated Medium on LeetCode.
- What is the time complexity of LeetCode 957. Prison Cells After N Days?
The Python solution on this page runs in O(n).
- What is the space complexity of LeetCode 957. Prison Cells After N Days?
The Python solution on this page uses O(n) auxiliary space.
- What topics does LeetCode 957. Prison Cells After N Days cover?
LeetCode 957. Prison Cells After N Days is tagged Bit Manipulation, Array, Hash Table and Math on LeetCode.