-
左右指针的想法用的好多啊
#include
#include
using namespace std;
int maxt(int a, int b)
{
if (a > b)
{
return a;
}
return b;
}
int mint(int a, int b)
{
if (a < b)
{
return a;
}
return b;
}
int MinimumSizeSubarraySum(vector &vec,int target)
{int right = 0, left = 0, len = vec.size();
int res = len + 1;
int sum = 0;
while (right < len)
{
while (right < len && sum < target)
{
sum += vec[right];
right++;
}
while (sum >= target)
{
res = mint(res, right - left);
sum -= vec[left];
left++;
}
}
return res==len+1?0:res;
}
int getMaxSum(vector &vec)
{
int res = 0;
int sum = 0;
for (int i = 0; i < vec.size(); i++)
{
sum =maxt(sum+vec[i],vec[i]);
res = maxt(sum, res);
}
return res;
}
int main()
{
vector vec{ -2,1,-3,4,-1,2,1,-5,4 };
vector vec1{ 2,3,1,2,4,3 };
cout << getMaxSum(vec) << endl;
cout << MinimumSizeSubarraySum(vec1, 7) << endl;
system(“pause”);
return 0;
}
[LeetCode] 209. Minimum Size Subarray Sum 最短子数组之和
最新推荐文章于 2020-09-28 21:55:41 发布