Codility Lesson 4 NumberOfDiscIntersections 解答
来源:互联网 发布:电力猫 知乎 编辑:程序博客网 时间:2024/06/02 17:00
Thinking:
1. Look every circle as a range, that will help us think in a simpler way. So the first step is translate circles into ranges(from "center&radius" to range):
A[0]=1 >> [-1,1]
A[1]=5 >> [-4,6]
A[2]=2 >> [0,4]
A[3]=1 >> [2,4]
A[4]=4 >> [0,8]
A[5]=0 >> [5,5]
2. Now, let's draw these ranges in a horizontal axis, so that every intersection is clear.
3. After understanding this theory, I believe you've fully understand the question. The next step is to find a way to calculate the intersections distinctively.
4. There is an idea that we can count the intersections along the index of array A[N], but the problem is recounting. If the intersection of two ranges has been counted in the previous index, and they still have intersection in the following index(s), it's easy to get a wrong result.
5. So, we need a variable to record the total ranges existing according to the current index. And 2 arrays to record when the starts and ends of the ranges, so that we know how many ranges is new when we move to the next index, which is really need to be counted the intersections with each other(See line 22). After these new ranges count their own intersections, they also need to count intersections with the existing ranges(See line 20).
6. In general, the most important thought has been shown. While there are still some traps should be valued, which will affect on the score.
- Take care the (A[i] + i) exceeds the value of MAX int<span style="font-family: Arial, Helvetica, sans-serif;">, which will become minus int.
- Return -1, when the result exceeds 10,000,000.
7. Hope it's not difficult to understand, if not, feel free to leave a message, we can talk about your question.
Solution in Java:
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; }}
Task description:
Given an array A of N integers, we draw N discs in a 2D plane such that the I-th disc is centered on (0,I) and has a radius of A[I]. We say that the J-th disc and K-th disc intersect if J ≠ K and J-th and K-th discs have at least one common point.
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 pairs of intersecting discs. For example, given N=6 and:
A[0] = 1 A[1] = 5 A[2] = 2A[3] = 1 A[4] = 4 A[5] = 0
intersecting discs appear in eleven pairs of elements:
- 0 and 1,
- 0 and 2,
- 0 and 4,
- 1 and 2,
- 1 and 3,
- 1 and 4,
- 1 and 5,
- 2 and 3,
- 2 and 4,
- 3 and 4,
- 4 and 5.
so the function should return 11.
The function should return −1 if the number of intersecting pairs exceeds 10,000,000.
Assume that:
- N is an integer within the range [0..100,000];
- each element of array A is an integer within the range [0..2147483647].
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.
Reference: http://www.rationalplanet.com/php-related/numberofdiscintersections-beta2010-demo-task-at-codility-com.html
- Codility Lesson 4 NumberOfDiscIntersections 解答
- Codility Lesson 3 MinAvgTwoSlice解答
- Codility Lesson 5 Brackets 解答
- Solution of NumberOfDiscIntersections by Codility
- 4.4 NumberOfDiscIntersections
- NumberOfDiscIntersections--重点
- codility test 4
- codility
- codility
- Lesson 4
- lesson 4
- codility上的练习 (4)
- LESSON 1 -part 4
- Ajax Lesson 4
- 新概念英语 lesson 4
- 孙鑫MFC:Lesson 4
- Java SE Lesson 4
- Lesson 4 按键
- UML基本架构建模--关联的术语和概念(一)
- UML基本架构建模--关联的术语和概念(二)
- UML基本架构建模--关联的术语和概念(三)
- 32位汇编语言学习笔记(41)--fgets等函数的使用
- 【博弈】【bzoj 1457】: 棋盘游戏
- Codility Lesson 4 NumberOfDiscIntersections 解答
- 职业院校英语教学的有效措施
- Linux System Log
- [转载]lambda函数
- Dojo Dijit.Tree 懒加载
- 年终总结之搜房网面试题(三)
- css的引用
- 传统的HMTL的缺陷
- 标记(tag)的概念