Algorithm to calculate number of intersecting discs

前端 未结 30 1367
鱼传尺愫
鱼传尺愫 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:30

    Well, I adapted Falk Hüffner's idea to c++, and made a change in the range. Opposite to what is written above, there is no need to go beyond the scope of the array (no matter how large are the values in it). On Codility this code received 100%. Thank you Falk for your great idea!

    int number_of_disc_intersections ( const vector &A ) {
        int sum=0;
        vector start(A.size(),0);
        vector end(A.size(),0);
        for (unsigned int i=0;i=A.size())   end[A.size()-1]++;
            else                    end[i+A[i]]++;
        }
        int active=0;
        for (unsigned int i=0;i10000000) return -1;
            active+=start[i]-end[i];
        }
        return sum;
    }
    

提交回复
热议问题