question:
this question is seem like line intersections question. we can use similar method to solve this question.
now i prove this solution. for each line, it has two endpoint. we call it left and right. assume that the left and right have been sorted .
if line i and line j intersection, we can conclude that left[i]<right[j] or left[j]<right[i]. ... how to say?
?
trap: int overflow
code:
#includeint solution(vector &A) { // write your code in C++11 int size = A.size(); if (size <2) return 0; vector begin; vector end; for(int i=0; i (A[i])); end.push_back(i+static_cast (A[i])); } sort(begin.begin(),begin.end()); sort(end.begin(),end.end()); int res = 0; int upper_index =0, lower_index=0; for(upper_index =0; upper_index 10000000) return -1; } return res;}