test sum 2 5 6 3 2 2 6 15 int findMin(vector<int> result){ int min = -1; if(result.size()==0){ min = -1; }else{ for(int i = 0;i<result.size();i++){ if(result[i]<min) min = result[i]; } } return min; } int getMinLength(vecotr<int> &Array, int n){ if(Array.size()==0) return -1; int sum = Array[0]; int begin = 0; int end = 0; int temp = 0; vector<int> result; for(int i = 1;i<Array.size();i++){ if(sum < n){ sum += Array[i]; end = i; }else{ // here // 2 5 6 3 2 2 6 temp = end - begin; result.push_back(temp); // bu if(Array[i]<= Array[begin]){ if(sum + Array[i]-Array[begin]>n){ end = i; begin++; temp = end - begin; result.push_back(temp); } // else end = i; }else { sum = sum + Array[i]; while(sum - Array[begin]>n){ sum = sum - Array[begin]; begin++; } end = i; temp = end - begin; result.push_back(temp); } } } temp = findMin(result); return temp; }
时间: 2024-12-13 15:22:39