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
// 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/

Codility--- NumberOfDiscIntersections的更多相关文章

  1. *[codility]Number-of-disc-intersections

    http://codility.com/demo/take-sample-test/beta2010/ 这题以前做的时候是先排序再二分,现在觉得没有必要.首先圆可以看成线段,把线段的进入作为一个事件, ...

  2. Solution of NumberOfDiscIntersections by Codility

    question:https://codility.com/programmers/lessons/4 this question is seem like line intersections qu ...

  3. Codility NumberSolitaire Solution

    1.题目: A game for one player is played on a board consisting of N consecutive squares, numbered from ...

  4. codility flags solution

    How to solve this HARD issue 1. Problem: A non-empty zero-indexed array A consisting of N integers i ...

  5. GenomicRangeQuery /codility/ preFix sums

    首先上题目: A DNA sequence can be represented as a string consisting of the letters A, C, G and T, which ...

  6. *[codility]Peaks

    https://codility.com/demo/take-sample-test/peaks http://blog.csdn.net/caopengcs/article/details/1749 ...

  7. *[codility]Country network

    https://codility.com/programmers/challenges/fluorum2014 http://www.51nod.com/onlineJudge/questionCod ...

  8. *[codility]AscendingPaths

    https://codility.com/programmers/challenges/magnesium2014 图形上的DP,先按照路径长度排序,然后依次遍历,状态是使用到当前路径为止的情况:每个 ...

  9. *[codility]MaxDoubleSliceSum

    https://codility.com/demo/take-sample-test/max_double_slice_sum 两个最大子段和相拼接,从前和从后都扫一遍.注意其中一段可以为0.还有最后 ...

  10. *[codility]Fish

    https://codility.com/demo/take-sample-test/fish 一开始习惯性使用单调栈,后来发现一个普通栈就可以了. #include <stack> us ...

随机推荐

  1. MongoDB小结

    教程 MongoDB MongoDB 是由C++语言编写的,是一个基于分布式文件存储的开源数据库系统.在高负载的情况下,添加更多的节点,可以保证服务器性能.MongoDB 旨在为WEB应用提供可扩展的 ...

  2. No_Sql总结

    NoSQL简介 NoSQL(NoSQL = Not Only SQL ),意即"不仅仅是SQL",是对不同于传统的关系型数据库的数据库管理系统的统称.在现代的计算系统上每天网络上都 ...

  3. SSH原理和使用

    ssh 是什么 在 linux 上工作,ssh 是必须要了解的技术方法.它可以建立起多台主机之间的安全的加密传输,以进行远程的访问.操控.传输数据. SSH 為 Secure Shell 的縮寫.為建 ...

  4. FZU Problem 2062 Suneast &amp; Yayamao

    http://acm.fzu.edu.cn/problem.php?pid=2062 标题效果: 给你一个数n,要求求出用多少个数字能够表示1~n的全部数. 思路: 分解为二进制. 对于一个数n.看它 ...

  5. Linux学习(1)vi编辑器的常用命令

    今天对Linux中的vi编辑器进行了学习,对其中的常用命令进行总结: 数字 0 或^:光标移到行首 $              :光标移到行尾 H             :光标移到屏幕的首行 L ...

  6. Linux下编译,要下载tar.xz,而不要下载zip,因为换行的编码不一样,对.h.cpp没有影响,但是对脚本有影响 good

    原因是 在win下编辑的时候,换行结尾是\n\r , 而在linux下 是\n,所以才会有 多出来的\r但是这个我是直接下载的官网文件解压的,没有动过啊. 破案了. linux下编译要下 .tar.x ...

  7. C++:怎样把一个int转成4个字节?

    大家都知道,一个int 或 unsigned int是由4个字节组成的,(<C/C++学习指南>,第3章,第3.2.3节:变量的内存视图) 比如, int   n  =  sizeof( ...

  8. 非参贝叶斯(Bayesian Non-parameter)初步

    0. motivations 如何确定 GMM 模型的 k,既观察到的样本由多少个高斯分布生成.由此在数据属于高维空间中时,根本就无法 visualize,更加难以建立直观,从而很难确定 k,高斯分布 ...

  9. XP双网卡不能上网的问题

    转载. 现在很多本本都是双网卡配置,让两个网卡分别负责连接内外网能够加快上网速度和连接稳定,但不少网友照做后会出现无法上网的情况,这是由于默认网关冲突所导致的.那么该如何处理让双网卡各行其是,互不干扰 ...

  10. 多线程——继承Thread类别

    详细java此前使用多线程,让我们来看看下面的问题. 什么是多线程     简单的理解成:cpu"同一时候"运行多个任务,这就是多线程. (究其本质,当涉及到进程和线程的概念.上面 ...