首页 > 代码库 > Colidity--MinAvgTwoSlice

Colidity--MinAvgTwoSlice

这题挺有意思的,没想到规模大于3的slice的avg肯定要比它的子slice(小于3)的avg来得小的话,就不太好写。

 1 int solution(vector<int> &A) { 2     // write your code in C++98 3     int minStart = 0; 4     double minAvg = (A[0]+A[1])/2.0; 5     int size = A.size(); 6     for(int i=0;i<size-1;i++) { 7         double tmpAvg = (A[i]+A[i+1])/2.0; 8         if(i<size-2&&A[i+2]<tmpAvg) { 9             tmpAvg = (A[i]+A[i+1]+A[i+2])/3.0;10         }11         if(tmpAvg<minAvg) {12             minAvg = tmpAvg;13             minStart = i;14         }15     }16     return minStart;17 }

 

Colidity--MinAvgTwoSlice