Triplets with Sum between given range
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
| int Solution::solve(vector<string> &A) { long double sum = 0; vector<long double> all; for(auto a : A) { long double now = stold(a); if(all.size() < 3) { all.push_back(now); sum += now; } else { if(1. < sum and sum < 2.) return true; for(int i = 0; i < 3; i++) { long double dsum = sum - all[i] + now; if(1. < dsum and dsum < 2.) return true; } if(sum <= 1.) { int p = min_element(begin(all), end(all)) - begin(all); if(all[p] < now) { sum = sum - all[p] + now; all[p] = now; } } else if(sum >= 2.) { int p = max_element(begin(all), end(all)) - begin(all); if(all[p] > now) { sum = sum - all[p] + now; all[p] = now; } } if(1. < sum and sum < 2.) return true; } } return 1. < sum and sum < 2.; }
|