class Solution {
public int maxSubarraySumCircular(int[] nums) {
int max = nums[0], pre = 0;
for (int num : nums) {
pre += num;
if (pre > max) {
max = pre;
}
if (pre < 0) {
pre = 0;
}
}
if (max <= 0) {
return max;
}
pre = 0;
int min = nums[0], sum = 0;
for (int num : nums) {
sum += num;
pre += num;
if (pre < min) {
min = pre;
}
if (pre > 0) {
pre = 0;
}
}
return Math.max(sum - min, max);
}
}
918. 环形子数组的最大和
最新推荐文章于 2024-10-14 09:08:48 发布