Codility--- NumberOfDiscIntersections


Task description

We draw N discs on a plane. The discs are numbered from 0 to N − 1. A zero-indexed array A of N non-negative integers, specifying the radiuses of the discs, is given. The J-th disc is drawn with its center at (J, 0) and radius A[J].

We say that the J-th disc and K-th disc intersect if J ≠ K and the J-th and K-th discs have at least one common point (assuming that the discs contain their borders).

The figure below shows discs drawn for N = 6 and A as follows:

A[0] = 1 A[1] = 5 A[2] = 2 A[3] = 1 A[4] = 4 A[5] = 0

There are eleven (unordered) pairs of discs that intersect, namely:

  • discs 1 and 4 intersect, and both intersect with all the other discs;
  • disc 2 also intersects with discs 0 and 3.

Write a function:

class Solution { public int solution(int[] A); }

that, given an array A describing N discs as explained above, returns the number of (unordered) pairs of intersecting discs. The function should return −1 if the number of intersecting pairs exceeds 10,000,000.

Given array A shown above, the function should return 11, as explained above.

Assume that:

  • N is an integer within the range [0..100,000];
  • each element of array A is an integer within the range [0..2,147,483,647].

Complexity:

  • expected worst-case time complexity is O(N*log(N));
  • expected worst-case space complexity is O(N), beyond input storage (not counting the storage required for input arguments).

Elements of input arrays can be modified.

Solution

Programming language used: Java

Total time used: 4 minutes

Code: 15:38:12 UTC, java, final, score:  100

show code in pop-up

12345678910111213141516171819202122232425262728293031323334353637383940414243444546

// you can also use imports, for example:
// import java.util.*;

// you can write to stdout for debugging purposes, e.g.
// System.out.println("this is a debug message");

class Solution {
    public int solution(int[] A) {
        // write your code in Java SE 8
        int l = A.length;
        int[] arrayIn = new int[l];
        int[] arrayOut = new int[l];
        int inNumContext = 0;
        int result = 0;  

        for(int i = 0; i < l; i ++) {
            int in = (i - A[i]) < 0 ? 0 : (i - A[i]);
            // take care the (A[i] + i) exceeds the value of MAX int
            // which will become minus int
            int out = (A[i] + i > l - 1 || A[i] + i < 0) ? (l - 1) : (A[i] + i);
            arrayIn[in] ++;
            arrayOut[out] ++;
        }  

        for(int i = 0; i < l; i ++) {
            if(arrayIn[i] != 0) {
                // previous circles times new coming circles
                result += inNumContext * arrayIn[i];
                // new coming circles group with each other
                result += arrayIn[i] * (arrayIn[i] - 1) / 2;  

                if (result > 10000000) {
                    return -1;
                }  

                // add coming circles to inNumContext
                inNumContext += arrayIn[i];
            }
            // minus leaving circles from inNumContext
            inNumContext -= arrayOut[i];
        }   

        return result;
    }
}

https://codility.com/demo/results/training5N9W8K-3M3/

时间: 2024-10-03 22:37:36

Codility--- NumberOfDiscIntersections的相关文章

Solution of NumberOfDiscIntersections by Codility

question:https://codility.com/programmers/lessons/4 trap: int overflow code: #include <algorithm> int solution(vector<int> &A) { // write your code in C++11 int size = A.size(); if (size <2) return 0; vector<long> begin; vector<

codility problem - NumberOfDiscIntersections

就是求 区间覆盖的问题. [x, y] 按x排序, 对y,二分找刚好大于它的x. package solution // you can also use imports, for example: import "fmt" import "sort" // you can write to stdout for debugging purposes, e.g. // fmt.Println("this is a debug message")

Solution of Codility

Solution of Codility codility.com is another great place to improve your programming skill. Train myself , and record here. Lesson 1: Time Complexity Lesson 2: Counting Elements Lesson 3: Prefix Sums Lesson 4: Sorting MaxProductOfThree: * Distinct: *

Solution to Triangle by Codility

question: https://codility.com/programmers/lessons/4 we need two parts to prove our solution. on one hand, there is no false triangular. Given the array has been sorted, if A[i]+A[i+1]>A[i+2], we can prove the existence of the triangle. for array A i

the solution of CountNonDivisible by Codility

question:https://codility.com/programmers/lessons/9 To solve this question , I get each element's divsors which appearing in input Array A using Sieve of Eratosthenes method. Time complexity is O(nlogn); Then  we iterate array A to get the ith non-di

GenomicRangeQuery /codility/ preFix sums

首先上题目: A DNA sequence can be represented as a string consisting of the letters A, C, G and T, which correspond to the types of successive nucleotides in the sequence. Each nucleotide has an impact factor, which is an integer. Nucleotides of types A,

codility上的问题(34) Fluorum 2014

好久没写codility的题了,一来没时间,二来有的题目不太好分析.这个题比较有意思,我还没有给出非常严格的证明. 给定一棵树(无向无环图),从一个节点出发,每次选择一个节点,从起点到目的节点的路径上没经过的节点尽可能多,直到遍历完所有的节点.如果起点到两个目的节点的路径中没经过的节点同样多,则选择标号较小的节点作为目的节点.如此继续,直到遍历所有的节点,求按顺序选择了哪些目的节点? 例如从2 开始,第一次选择0,然后1,0变为经历过的节点. 然后从0开始,第二次选择6, 然后4,6变为经历过的

codility flags solution

How to solve this HARD issue 1. Problem: A non-empty zero-indexed array A consisting of N integers is given. A peak is an array element which is larger than its neighbours. More precisely, it is an index P such that 0 < P < N − 1 and A[P − 1] < A

*[codility]Country network

https://codility.com/programmers/challenges/fluorum2014 http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1273 http://blog.csdn.net/caopengcs/article/details/36872627 http://www.quora.com/How-do-I-determine-the-order-of-visiting-all-leave

Codility上的问题(35) Neon 2014

也是比较有意思的题,越来越数学了--不善于做这种题. 如图一个码头有N个木桩,用于拴住船,码头长度是M,可以理解未0到M的线段.有N调船,每条船的一半长度为X,所以船长是2 * X.每个船的中心必须拴在一个木桩上.并且每个木桩只能拴一条船.拴船的绳子长度是船的中心与木桩位置的距离.当然,木桩的位置不能移动,但是船可以自由左右移动.要求船头船尾必须都在码头上(0..M的线段),船也可以看作长度为2 * X的线段,请给每条船指定一个位置让拴船的最长绳子长度最短,求这个最短的绳子长度.如果无法容纳下所