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. .NET基础拾遗

    原帖地址: http://www.cnblogs.com/edisonchou/p/4787775.html

  2. python 和为S的连续正数序列

    题目描述: 小明很喜欢数学,有一天他在做数学作业时,要求计算出9~16的和,他马上就写出了正确答案是100.但是他并不满足于此,他在想究竟有多少种连续的正数序列的和为100(至少包括两个数).没多久, ...

  3. 左右RAC CRS 自己主动启动

    左右CRS自己主动重新启动实验 一.检验ASM [root@rac1 ~]# /etc/init.d/oracleasm status Checking if ASM is loaded: yes C ...

  4. 在WPF中制作正圆形公章

    原文:在WPF中制作正圆形公章 之前,我利用C#与GDI+程序制作过正圆形公章(利用C#制作公章 ,C#制作公章[续])并将它集成到一个小软件中(个性印章及公章的画法及实现),今天我们来探讨一下WPF ...

  5. WPF中利用RadialGradient模拟放大镜效果

    原文:WPF中利用RadialGradient模拟放大镜效果 --------------------------------------------------------------------- ...

  6. WPF,Silverlight与XAML读书笔记第三十九 - 可视化效果之3D图形

    原文:WPF,Silverlight与XAML读书笔记第三十九 - 可视化效果之3D图形 说明:本系列基本上是<WPF揭秘>的读书笔记.在结构安排与文章内容上参照<WPF揭秘> ...

  7. windows 的使用 —— 注册表(软件的安装和卸载)

    win + r(run):输入 regedit(register edit)进入: 1. 网络连接 比如一些 vpn 安装之后,会对网络连接进行一定的修改,这样在 vpn 工具删除之后,仍然无法消除修 ...

  8. 通通玩blend美工(7)——简约而不简单的块

    原文:通通玩blend美工(7)--简约而不简单的块 最近在研发一个WPF快速开发框架,满脑子都是各种逻辑各种模式,写一篇比较休闲娱乐的博客,宣泄下我对美工的热爱. 我一直以来有意无意在手机应用或者各 ...

  9. Java Socket 爬虫

    # 地址 https://github.com/mofadeyunduo/crawler # 前言 1.代码不断优化更新. 2.有建议请留言. # 介绍 1.多线程,基于 ExcutorServcie ...

  10. MVC WebApi

    两种web服务 •SOAP风格:基于方法,产品是WebService •REST风格:基于资源,产品是WebAPI 对于数据的增.删.改.查,提供相对的资源操作,按照请求的类型进行相应处理,主要包括G ...