Skip to content

Commit 042f9e2

Browse files
committed
leetcode
1 parent a009b6f commit 042f9e2

File tree

4 files changed

+222
-0
lines changed

4 files changed

+222
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
/*
2+
3+
4+
-* 918. Maximum Sum Circular SubArray *-
5+
6+
Given a circular integer array nums of length n, return the maximum possible sum of a non-empty sub-array of nums.
7+
8+
A circular array means the end of the array connects to the beginning of the array. Formally, the next element of nums[i] is nums[(i + 1) % n] and the previous element of nums[i] is nums[(i - 1 + n) % n].
9+
10+
A sub-array may only include each element of the fixed buffer nums at most once. Formally, for a sub-array nums[i], nums[i + 1], ..., nums[j], there does not exist i <= k1, k2 <= j with k1 % n == k2 % n.
11+
12+
13+
14+
Example 1:
15+
16+
Input: nums = [1,-2,3,-2]
17+
Output: 3
18+
Explanation: Sub-array [3] has maximum sum 3.
19+
Example 2:
20+
21+
Input: nums = [5,-3,5]
22+
Output: 10
23+
Explanation: Sub-array [5,5] has maximum sum 5 + 5 = 10.
24+
Example 3:
25+
26+
Input: nums = [-3,-2,-3]
27+
Output: -2
28+
Explanation: Sub-array [-2] has maximum sum -2.
29+
30+
31+
Constraints:
32+
33+
n == nums.length
34+
1 <= n <= 3 * 104
35+
-3 * 104 <= nums[i] <= 3 * 104
36+
37+
*/
38+
39+
import 'dart:math';
40+
41+
class A {
42+
int maxSubarraySumCircular(List<int> nums) {
43+
// max = int_min
44+
int totalSum = 0, maxSum = -30012, curMax = 0, minSum = 30012, curMin = 0;
45+
for (int x in nums) {
46+
curMax = max(x, curMax + x); //update the current max sub-array sum
47+
maxSum = max(maxSum, curMax); //update the overall max sub-array sum
48+
curMin = min(x, curMin + x); //update the current min sub-array sum
49+
minSum = min(minSum, curMin); //update the overall min sub-array sum
50+
totalSum += x;
51+
}
52+
return maxSum > 0 ? max(maxSum, totalSum - minSum) : maxSum;
53+
}
54+
}
55+
56+
class B {
57+
int kadane(List<int> nums) {
58+
int local = nums[0];
59+
int global = nums[0];
60+
for (int i = 1; i < nums.length; i++) {
61+
local = max(nums[i], local + nums[i]);
62+
global = max(global, local);
63+
}
64+
return global;
65+
}
66+
// case 1: max subarray sum in [0 .. n - 1]
67+
// i.e. kadane's algo
68+
// case 2. circular subarray in [0 .. | n - 1 .. | .. 2 * n - 1]
69+
// i.e. total sum - min subarray sum in [0 .. n - 1]
70+
71+
int maxSubarraySumCircular(List<int> nums) {
72+
int n = nums.length;
73+
// use kadane's algo to find out max sub array sum (case 1)
74+
int maxSubArraySum = kadane(nums);
75+
// handle cases like [-3,-2,-3]
76+
if (maxSubArraySum < 0) return maxSubArraySum;
77+
// calculate the total sum
78+
int totalSum = 0;
79+
// in order to use the same kadane function, we flip the sign
80+
for (int i = 0; i < n; i++) {
81+
totalSum += nums[i];
82+
nums[i] = -nums[i];
83+
}
84+
// use kadane's algo to find out min sub array sum
85+
int minSubArraySum = kadane(nums);
86+
// compare case 1 & case 2, take the max
87+
return max(maxSubArraySum, totalSum + minSubArraySum);
88+
}
89+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
package main
2+
3+
func maxSubarraySumCircular(nums []int) int {
4+
var totalSum int = 0
5+
var maxSum int = -30012
6+
var curMax int = 0
7+
var minSum int = 3012
8+
var curMin = 0
9+
for _, x := range nums {
10+
curMax = max(x, curMax+x)
11+
maxSum = max(maxSum, curMax)
12+
curMin = min(x, curMin+x)
13+
minSum = min(minSum, curMin)
14+
totalSum += x
15+
}
16+
if maxSum > 0 {
17+
return max(maxSum, totalSum-minSum)
18+
} else {
19+
return maxSum
20+
}
21+
}
22+
23+
func max(a int, b int) int {
24+
if a > b {
25+
return a
26+
} else {
27+
return b
28+
}
29+
}
30+
func min(a int, b int) int {
31+
if a < b {
32+
return a
33+
} else {
34+
return b
35+
}
36+
}
37+
38+
/*
39+
40+
41+
42+
43+
int totalSum = 0, maxSum = -30012, curMax = 0, minSum = 30012, curMin = 0;
44+
for (int x in nums) {
45+
curMax = max(x, curMax + x); //update the current max sub-array sum
46+
maxSum = max(maxSum, curMax); //update the overall max sub-array sum
47+
curMin = min(x, curMin + x); //update the current min sub-array sum
48+
minSum = min(minSum, curMin); //update the overall min sub-array sum
49+
totalSum += x;
50+
}
51+
return maxSum > 0 ? max(maxSum, totalSum - minSum) : maxSum;
52+
53+
54+
55+
*/
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
# 🔥 100% FAST KADANE 🔥 || Simple Fast and Easy || with Explanation
2+
3+
## Approach
4+
5+
This problem is asking for the maximum possible sum of a non-empty sub-array of a given circular integer array.
6+
A circular array means that the end of the array connects to the beginning of the array, so the next element of nums[i] is nums[(i + 1) % n] and the previous element of nums[i] is nums[(i - 1 + n) % n].
7+
A sub-array may only include each element of the fixed buffer nums at most once.
8+
9+
One approach to solving this problem is to first find the maximum sub-array sum using the standard Kadane's algorithm, and then check if there's a better solution by finding the minimum sub-array sum and subtracting it from the total sum of the array.
10+
This approach considers the case where the maximum sub-array sum is obtained from the sub-array that "wraps around" the circular array.
11+
12+
### Complexity
13+
14+
#### Time complexity:: O(n)
15+
16+
Because we are iterating through the array once and performing constant time operations inside the loop
17+
18+
#### Space complexity:: O(1)
19+
20+
Because we are using a constant amount of additional space to store the curMax, maxSum, curMin, minSum and totalSum variables.
21+
22+
## Code
23+
24+
```dart
25+
class Solution {
26+
int maxSubarraySumCircular(List<int> nums) {
27+
int totalSum = 0, maxSum = -30012, curMax = 0, minSum = 30012, curMin = 0;
28+
for (int x in nums) {
29+
curMax = max(x, curMax + x); //update the current max sub-array sum
30+
maxSum = max(maxSum, curMax); //update the overall max sub-array sum
31+
curMin = min(x, curMin + x); //update the current min sub-array sum
32+
minSum = min(minSum, curMin); //update the overall min sub-array sum
33+
totalSum += x;
34+
}
35+
return maxSum > 0 ? max(maxSum, totalSum - minSum) : maxSum;
36+
}
37+
}
38+
```
39+
40+
## Code - 2
41+
42+
```dart
43+
class Solution {
44+
int kadane(List<int> nums) {
45+
int local = nums[0];
46+
int global = nums[0];
47+
for (int i = 1; i < nums.length; i++) {
48+
local = max(nums[i], local + nums[i]);
49+
global = max(global, local);
50+
}
51+
return global;
52+
}
53+
// case 1: max subarray sum in [0 .. n - 1]
54+
// i.e. kadane's algo
55+
// case 2. circular subarray in [0 .. | n - 1 .. | .. 2 * n - 1]
56+
// i.e. total sum - min subarray sum in [0 .. n - 1]
57+
58+
int maxSubarraySumCircular(List<int> nums) {
59+
int n = nums.length;
60+
// use kadane's algo to find out max sub array sum (case 1)
61+
int maxSubArraySum = kadane(nums);
62+
// handle cases like [-3,-2,-3]
63+
if (maxSubArraySum < 0) return maxSubArraySum;
64+
// calculate the total sum
65+
int totalSum = 0;
66+
// in order to use the same kadane function, we flip the sign
67+
for (int i = 0; i < n; i++) {
68+
totalSum += nums[i];
69+
nums[i] = -nums[i];
70+
}
71+
// use kadane's algo to find out min sub array sum
72+
int minSubArraySum = kadane(nums);
73+
// compare case 1 & case 2, take the max
74+
return max(maxSubArraySum, totalSum + minSubArraySum);
75+
}
76+
}
77+
```

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,7 @@ This repo contain leetcode solution using DART and GO programming language. Most
187187
- [**2421.** Number of Good Paths](NumberOfGoodPaths/number_of_good_paths.dart)
188188
- [**57.** Insert Interval](InsertInterval/insert_interval.dart)
189189
- [**926.** Flip String to Monotone Increasing](FlipStringToMonotoneIncreasing/flip_string_to_monotone_increasing.dart)
190+
- [**918.** Maximum Sum Circular Sub-Array](MaximumSumCircularSubarray/maximum_sum_circular_subarray.dart)
190191

191192
## Reach me via
192193

0 commit comments

Comments
 (0)