Advertisement
Guest User

Untitled

a guest
Jan 22nd, 2019
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.56 KB | None | 0 0
  1. class Solution {
  2.     public int[] prisonAfterNDays(int[] cells, int N) {
  3.        
  4.         int index = 0;
  5.         HashMap<String,Integer> map = new HashMap<>();
  6.         String currentVariation = "";
  7.        
  8.         while (true)
  9.         {
  10.             if (index == N) return cells;
  11.             int prev = cells[0];
  12.             currentVariation = "" + 0;
  13.             for (int j = 1; j < cells.length - 1; j++)
  14.             {
  15.                 if (prev + cells[j+1] != 1)
  16.                 {
  17.                     prev = cells[j];
  18.                     cells[j] = 1;
  19.                 }
  20.                 else
  21.                 {
  22.                     prev = cells[j];
  23.                     cells[j] = 0;
  24.                 }
  25.                 currentVariation += cells[j];
  26.             }
  27.            
  28.             cells[0] = 0;
  29.             cells[cells.length-1] = 0;
  30.             currentVariation += 0;
  31.            
  32.             if (!map.containsKey(currentVariation)) map.put(currentVariation,index+1);
  33.             else break;
  34.            
  35.             index++;
  36.         }    
  37.        
  38.         Integer variationToGet = N % index == 0 ? index : N % index;
  39.         String ret = "";
  40.        
  41.         for(Map.Entry entry: map.entrySet()){
  42.             if(variationToGet.equals(entry.getValue())){
  43.                 ret = entry.getKey().toString();
  44.                 break;
  45.             }
  46.         }
  47.        
  48.         for (int i = 1; i < ret.length() - 1; i++)
  49.         {
  50.             cells[i] = Character.getNumericValue(ret.charAt(i));
  51.         }
  52.                
  53.         return cells;
  54.     }
  55. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement