-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsolution.java
More file actions
36 lines (35 loc) · 1.03 KB
/
Copy pathsolution.java
File metadata and controls
36 lines (35 loc) · 1.03 KB
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
36
// 918. Maximum Sum Circular Subarray
// https://leetcode.com/problems/maximum-sum-circular-subarray/
// Medium | Java | Accepted 2026-08-18
// Runtime 5 ms | Memory 51.4 MB
class Solution {
public int maxSubarraySumCircular(int[] nums) {
int[] twice = new int[nums.length*2];
int total = 0;
for(int i = 0; i<twice.length; i++)
{
twice[i] = nums[i%nums.length];
if(i<nums.length)
{
total+=nums[i];
}
}
int max = twice[0];
int min = twice[0];
int currMax = twice[0];
int currMin = twice[0];
for(int i = 1; i<nums.length; i++)
{
currMax = Math.max(twice[i], currMax+twice[i]);
currMin = Math.min(twice[i], currMin+twice[i]);
max = Math.max(max, currMax);
min = Math.min(min, currMin);
}
if(min==total)
{
return max;
}
min = total - min;
return Math.max(max, min);
}
}