Prison Cells After N Days

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.
  • Otherwise, it becomes vacant.

(Note that because the prison is a row, the first and the last cells in the row can't have two adjacent neighbors.)

We describe the current state of the prison in the following way: cells[i] == 1 if the i-th cell is occupied, else cells[i] == 0.

Given the initial state of the prison, return the state of the prison after N days (and N such changes described above.)

Example 1:

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:
Day 0: [0, 1, 0, 1, 1, 0, 0, 1]
Day 1: [0, 1, 1, 0, 0, 0, 0, 0]
Day 2: [0, 0, 0, 0, 1, 1, 1, 0]
Day 3: [0, 1, 1, 0, 0, 1, 0, 0]
Day 4: [0, 0, 0, 0, 0, 1, 0, 0]
Day 5: [0, 1, 1, 1, 0, 1, 0, 0]
Day 6: [0, 0, 1, 0, 1, 1, 0, 0]
Day 7: [0, 0, 1, 1, 0, 0, 0, 0]

Example 2:

Input: cells = [1,0,0,1,0,0,1,0], N = 1000000000
Output: [0,0,1,1,1,1,1,0]

Note:

  1. cells.length == 8
  2. cells[i] is in {0, 1}
  3. 1 <= N <= 10^9
 class Solution {
    public int[] prisonAfterNDays(int[] cells, int N) {
        int[] result = new int[cells.length];
        N= N%14 == 0?14:N%14;
        int i, length = cells.length-1;
        while(N > 0)
        {
            for(i=1; i<length; i++)
            {
                if(cells[i-1]==cells[i+1])
                    result[i]=1;
                else
                    result[i]=0;
            }
            N--;
            cells=Arrays.copyOf(result, length+1);
        }
        return result;
    }
}


Here, we are going to solve using normal easiest approach that we can come up easily by seeing example explanation.

1) If both neighbors of any cell is same, then current cell has to be updated by 1 else 0 (Note that because the prison is a row, the first and the last cells in the row can't have two adjacent neighbors.)
2) While repeating above steps for N times, make sure to update result array using Arrays.copyOf()

NOTE:
Only way to reduce N iteration is for each multiples of 14 iterations we will get same availability of cells. Thus by using N%14 == 0?14:N%14 we can reduce N iterations.

Related Post:

Click here for April month challenges with solution and explanation

Click Follow button to receive updates from us instantly.

Please let us know about your views in comment section.

Help Others, Please Share..!!!!

                                                     😍..Happy Coding...😍


Comments

  1. why 14 from where this come from

    ReplyDelete
    Replies
    1. Thanks for asking here.I get this by taking some small prison cells and do n iterations(using pen, paper) to find any similarities between them..from that I come up with this solution. If you want to know mathematical reason behind this approach check out here https://math.stackexchange.com/questions/3311568/why-does-this-pattern-repeat-after-14-cycles-instead-of-256-can-you-give-a-proo Hope it clears.!! Happy coding..!!

      Delete

Post a Comment

Popular posts from this blog

Balanced Binary Tree

First Unique Character in a String

Majority Element

Smallest Range II