-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path0918-MaximumSumCircularSubarray.cs
49 lines (42 loc) · 1.38 KB
/
0918-MaximumSumCircularSubarray.cs
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
37
38
39
40
41
42
43
44
45
46
47
48
49
//-----------------------------------------------------------------------------
// Runtime: 152ms
// Memory Usage: 38.8 MB
// Link: https://leetcode.com/submissions/detail/340014935/
//-----------------------------------------------------------------------------
using System;
using System.Linq;
namespace LeetCode
{
public class _0918_MaximumSumCircularSubarray
{
public int MaxSubarraySumCircular(int[] A)
{
int n = A.Length;
if (n == 1) return A[0];
int curr = int.MinValue, result1 = int.MinValue;
for (int i = 0; i < n; i++)
{
curr = A[i] + Math.Max(curr, 0);
result1 = Math.Max(result1, curr);
}
int total = A.Sum();
curr = int.MaxValue;
int result2 = int.MaxValue;
for (int i = 1; i < n; i++)
{
curr = A[i] + Math.Min(curr, 0);
result2 = Math.Min(result2, curr);
}
result2 = total - result2;
curr = int.MaxValue;
int result3 = int.MaxValue;
for (int i = 0; i < n - 1; i++)
{
curr = A[i] + Math.Min(curr, 0);
result3 = Math.Min(result3, curr);
}
result3 = total - result3;
return Math.Max(result1, Math.Max(result2, result3));
}
}
}