Advertisement
Guest User

Untitled

a guest
Feb 23rd, 2019
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.30 KB | None | 0 0
  1. class Solution {
  2. public int[] gridIllumination(int N, int[][] lamps, int[][] queries) {
  3. Map<Integer,Integer> rowMap = new HashMap<>();
  4. Map<Integer,Integer> colMap = new HashMap<>();
  5. Map<Integer,Integer> diagnalMap = new HashMap<>();
  6. Map<Integer,Integer> antiDiagonalMap = new HashMap<>();
  7. Set<String> lampLocation = new HashSet<>();
  8. for(int[] lamp : lamps)
  9. {
  10. int x = lamp[0];
  11. int y = lamp[1];
  12. lampLocation.add(x+"->"+y);
  13. addToMap(rowMap,x);
  14. addToMap(colMap,y);
  15. addToMap(diagnalMap,x+y);
  16. addToMap(antiDiagonalMap,x-y);
  17. }
  18. int[][] dicts = {{-1,-1},{-1,0},{1,1},{0,-1},{0,1},{1,-1},{1,0},{1,1}};
  19. int[] result = new int[queries.length];
  20. for(int i = 0 ; i < queries.length ; i++)
  21. {
  22. int x = queries[i][0];
  23. int y = queries[i][1];
  24. if(rowMap.containsKey(x)
  25. || colMap.containsKey(y)
  26. || diagnalMap.containsKey(x+y)
  27. || antiDiagonalMap.containsKey(x-y))
  28. result[i] = 1;
  29. for(int[] dict : dicts)
  30. {
  31. int newX = x + dict[0];
  32. int newY = y + dict[1];
  33. String location = newX + "->" + newY;
  34. if(valid(N,newX,newY) && lampLocation.contains(location))
  35. {
  36. lampLocation.remove(location);
  37. substractMap(rowMap,newX);
  38. substractMap(colMap,newY);
  39. substractMap(diagnalMap,newX+newY);
  40. substractMap(antiDiagonalMap,newX - newY);
  41. }
  42. }
  43. }
  44. return result;
  45. }
  46. public boolean valid(int N,int x,int y)
  47. {
  48. return x>=0 && y >= 0 && x < N && y < N;
  49. }
  50. public void substractMap(Map<Integer,Integer> map,int val)
  51. {
  52. if(!map.containsKey(val))
  53. return;
  54. map.put(val,map.get(val)-1);
  55. if(map.get(val) ==0)
  56. map.remove(val);
  57. }
  58.  
  59. public void addToMap(Map<Integer,Integer> map, int val)
  60. {
  61. map.compute(val,(k,v)->{
  62. if(v == null)
  63. return 1;
  64. return v + 1;
  65. });
  66. }
  67. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement