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. Objective-C 中的Runtime的使用

    Runtime的使用 一直以来,OC被大家冠以动态语言的称谓,其实是因为OC中包含的runtime机制.Runtime 又叫运行时,是一套底层的 C 语言 API,其为 iOS 内部的核心之一,我们平 ...

  2. node与webpack的process.env.NODE_ENV

    先看两篇文章 1.前端工程项目的NODE_ENV 2. Node 环境变量 process.env.NODE_ENV 之webpack应用 3.process.env.NODE_ENV 下面全部是在w ...

  3. 使用UISegementControl实现简易Tomcat程序

    // // TomViewController.m #import "TomViewController.h" #import <AVFoundation/AVFoundat ...

  4. scipy 图像处理(scipy.misc、scipy.ndimage)、matplotlib 图像处理

    from scipy.misc import imread / imsave / imshow imresize / imrotate / imfilter 1. scipy.misc 下的图像处理 ...

  5. git记不住用户名和密码

    以前我是用svn的 , 我也是最近才用的git 虽然git 有GUI界面  , 但是我觉得还是不如svn 最开始使用git的时候我们直接clone项目的时候可能会设置全局的账号和密码 , 但是我重装系 ...

  6. prototype __proto__ Function

    我们创建的每个函数都有一个prototype属性,这个属性是一个指针,指向一个对象.(注意:是函数才有prototype属性) 而__proto__属性每一个对象都有. 在js中如果A对象是由B函数构 ...

  7. 权限控制方案之——基于URL拦截

    概述: 在系统开发过程中需要考虑的一个重要的问题就是权限问题,权限问题也是安全问题的一个范畴,我们要求在用户登录系统之后,要控制用户可以访问的系统资源,使得用户只可以访问到系统事先分配好的资源:这里的 ...

  8. lua--从白开始(2)

    眼下lua最新的版本号,5.2.3. 这个例子是一个简单lua分析器,来源自<Lua游戏开发实践指南>. 测试程序的功能:解决简单lua说明,例如:print("Hello wo ...

  9. WebView 联系(要么button)至 Activity 跳跃在几个方面

    第一 ,写一个 JavaScriptinterface 分类.内实现WebView至Activity 页面跳转 public class JavaScriptinterface { Activity ...

  10. 第一泰泽(Tizen)智能手机发布在俄罗斯

    请看下图: 这是韩国三星公司在俄罗斯境内公布的第一款泰泽(Tizen)智能手机(今年6月2日).这说明,Tizen操作系统没有死去. 在泰泽官网上将泰泽操作系统定义为:"The OS of ...