Algorithm to calculate number of intersecting discs

前端 未结 30 1554
鱼传尺愫
鱼传尺愫 2020-12-12 10:57

Given an array A of N integers we draw N discs in a 2D plane, such that i-th disc has center in (0,i) and a radius

30条回答
  •  醉酒成梦
    2020-12-12 11:46

    Java 2*100%.

    result is declared as long for a case codility doesn't test, namely 50k*50k intersections at one point.

    class Solution {
        public int solution(int[] A) {
    
            int[] westEnding = new int[A.length];
            int[] eastEnding = new int[A.length];
    
            for (int i=0; i=0) eastEnding[i-A[i]]++; else eastEnding[0]++;
                if ((long)i+A[i]10000000) return -1;
    
                easts--;
                wests-= westEnding[i];
            }
    
            return (int) result;
        }
    }
    

提交回复
热议问题