Advertisement
Guest User

Untitled

a guest
May 23rd, 2019
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.44 KB | None | 0 0
  1. public class Solution {
  2. public int solution(int[] radiuses) {
  3. int length = radiuses.length;
  4. int[] startCounts = new int[length];
  5. int[] endCounts = new int[length];
  6.  
  7. // make sure there's no overflows
  8. for (int i = 0; i < length; i++) {
  9. radiuses[i] = Math.min(length, radiuses[i]);
  10. }
  11.  
  12. int lastPos = length - 1;
  13. for (int i = 0; i < length; i++) {
  14. int radius = radiuses[i];
  15. int startPos = Math.max(i - radius, 0);
  16. int endPos = Math.min(i + radius, lastPos);
  17.  
  18. startCounts[startPos]++;
  19. endCounts[endPos]++;
  20. }
  21.  
  22. // printAll(startCounts);
  23. // printAll(endCounts);
  24.  
  25. int intersectCount = 0;
  26. int currentLayerCount = 0;
  27.  
  28. for (int i = 0; i < length; i++) {
  29. int newStarts = startCounts[i];
  30. int newEnds = endCounts[i];
  31.  
  32. intersectCount += newStarts * currentLayerCount + (newStarts * (newStarts - 1)) / 2;
  33. currentLayerCount = currentLayerCount + newStarts - newEnds;
  34.  
  35. // short circuit return
  36. if(intersectCount > 10000000) {
  37. return -1;
  38. }
  39. }
  40.  
  41. return intersectCount;
  42. }
  43.  
  44. public void printAll(int[] numbers) {
  45. int length = numbers.length;
  46. for (int i = 0; i < length; i++) {
  47. System.out.println(numbers[i]);
  48. }
  49.  
  50. System.out.println("");
  51. }
  52. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement