Algorithm to calculate number of intersecting discs

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

    count = 0
    for (int i = 0; i < N; i++) {
      for (int j = i+1; j < N; j++) {
        if (i + A[i] >= j - A[j]) count++;
      }
    }
    

    It is O(N^2) so pretty slow, but it works.

提交回复
热议问题