Advertisement
Guest User

Untitled

a guest
Feb 20th, 2018
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.47 KB | None | 0 0
  1. import java.util.Arrays;
  2.  
  3. /**
  4.  * Created by lidiakulikova on 20/02/2018.
  5.  */
  6. public class Discs {
  7.  
  8.     public static int solution(int[] A) {
  9.         // 0 <= A.length <= 100,000
  10.         // 0 <= A[i] <= 2147483647
  11.         int [] leftEdge = new int[A.length];
  12.         int [] rightEdge = new int[A.length];
  13.  
  14.         int maxLength = 100000;
  15.         // maxLength is used to prevent integers > 2147483647
  16.         // and integers < -2147483647
  17.         for (int i = 0; i < A.length; i++) {
  18.             leftEdge[i] = i - A[i];
  19.             rightEdge[i] = i + A[i];
  20.         }
  21.         Arrays.sort(leftEdge);
  22.         Arrays.sort(rightEdge);
  23.  
  24.         int sum = mergeAndCountOverlaps(leftEdge,rightEdge, maxLength);
  25.         return sum;
  26.     }
  27.  
  28.     private static int mergeAndCountOverlaps(int[] leftEdge, int[] rightEdge, int maxLength) {
  29.         int leftIndex = 0;
  30.         int rightIndex = 0;
  31.         int sum = 0;
  32.         int total = 0;
  33.         while ((leftIndex < leftEdge.length) || (rightIndex < rightEdge.length)) {
  34.             if ((leftIndex < leftEdge.length) && (rightIndex < rightEdge.length)) {
  35.                 boolean compareLeftEdgeandRightEdge;
  36.                 if (leftEdge[leftIndex] < -2147483647 + maxLength) {
  37.                     compareLeftEdgeandRightEdge = leftEdge[leftIndex] <= rightEdge[rightIndex];
  38.                 } else {
  39.                     compareLeftEdgeandRightEdge = leftEdge[leftIndex] <= rightEdge[rightIndex];
  40.                 }
  41.                 if (compareLeftEdgeandRightEdge) {
  42.                     // a new left edge
  43.                     sum += total;
  44.                     if (sum > 10000000) {
  45.                         return -1;
  46.                     }
  47.                     total++;
  48.                     leftIndex++;
  49.                 } else {
  50.                     // a new right edge
  51.                     total--;
  52.                     rightIndex++;
  53.                 }
  54.             } else if (leftIndex < leftEdge.length) {
  55.                 // a new left edge
  56.                 sum += total;
  57.                 if (sum > 10000000) {
  58.                     return -1;
  59.                 }
  60.                 total++;
  61.                 leftIndex++;
  62.             } else if (rightIndex < rightEdge.length) {
  63.                 // a new right edge
  64.                 total--;
  65.                 rightIndex++;
  66.             }
  67.         }
  68.         return sum;
  69.     }
  70.  
  71.  
  72.     public static void main (String[] args){
  73.         System.out.println(solution(new int[]{1, 5, 2, 1, 4, 0}));
  74.     }
  75. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement