Advertisement
Guest User

Untitled

a guest
Jan 24th, 2020
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.68 KB | None | 0 0
  1. class Solution {
  2.     public int orangesRotting(int[][] grid) {
  3.         HashSet<Point> rotten = new HashSet<Point>();
  4.         HashSet<Point> fresh = new HashSet<Point>();
  5.         for(int r = 0; r < grid.length; r++) {
  6.             for(int c = 0; c < grid[r].length; c++) {
  7.                 if (grid[r][c] == 2) {
  8.                     rotten.add(new Point(r,c));
  9.                     System.out.println("Rotten r: " + r + " c: " + c);
  10.                 }
  11.                 if ( grid[r][c] == 1) {
  12.                     fresh.add(new Point(r,c));
  13.                     System.out.println("Fresh r: " + r + " c: " + c);
  14.                 }
  15.             }
  16.         }
  17.         if ( fresh.isEmpty()) {
  18.             return 0;
  19.         }
  20.         int count = 0;
  21.         HashSet<Point> newRotten = new HashSet<Point>();
  22.         while (!fresh.isEmpty() && !rotten.isEmpty()) {
  23.             for(Point p: rotten) {
  24.                 ArrayList<Point> neighbors = getNeighbors(p, fresh);
  25.                 for(Point n: neighbors) {
  26.                     newRotten.add(n);
  27.                 }
  28.             }
  29.             if(newRotten.isEmpty() && !fresh.isEmpty()) {
  30.                 return -1;
  31.             }
  32.             rotten.clear();
  33.             for(Point r: newRotten) {
  34.                 rotten.add(r);
  35.             }
  36.             System.out.println(count);
  37.             newRotten.clear();
  38.             count++;
  39.         }
  40.         return count;
  41.     }
  42.    
  43.     private static ArrayList<Point> getNeighbors(Point p, HashSet<Point> fresh) {
  44.         int r = p.r;
  45.         int c = p.c;
  46.         Point top = new Point(r - 1, c);
  47.         Point bot = new Point(r + 1, c);
  48.         Point left = new Point(r, c + 1);
  49.         Point right = new Point(r, c - 1);
  50.         ArrayList<Point> points = new ArrayList<Point>();
  51.         if (fresh.contains(top)) {
  52.             points.add(top);
  53.             fresh.remove(top);
  54.         }
  55.         if(fresh.contains(left)){
  56.             points.add(left);
  57.             fresh.remove(left);
  58.         }
  59.         if(fresh.contains(right)){
  60.             points.add(right);
  61.             fresh.remove(right);
  62.         }
  63.         if(fresh.contains(bot)) {
  64.             points.add(bot);
  65.             fresh.remove(bot);
  66.         }
  67.         System.out.println(points);
  68.         return points;  
  69.     }
  70. }
  71.  
  72. class Point {
  73.     public final int r;
  74.     public final int c;
  75.    
  76.     public Point (int r, int c ) {
  77.         this.r = r;
  78.         this.c = c;
  79.     }
  80.    
  81.     public boolean equals(Object b) {
  82.         if ( b == this ) {
  83.             return true;
  84.         }
  85.         Point comp = (Point) b;
  86.         if (this.r == comp.r && this.c == comp.c) {
  87.             return true;
  88.         }
  89.         return false;
  90.     }
  91. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement