Skip to content

Commit 11d22da

Browse files
fixed standard template issues
1 parent a945ad4 commit 11d22da

1 file changed

Lines changed: 19 additions & 6 deletions

File tree

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,19 @@
11
#include<iostream>
22
#include<vector>
33
#include<climits>
4-
5-
using namespace std;
4+
#include<algorithm>
65

76
class Solution {
87
public:
9-
int maxSubArray(vector<int>& nums) {
8+
int maxSubArray(std::vector<int>& nums) {
9+
if (nums.empty()) {
10+
return 0;
11+
}
1012
int maxSum = INT_MIN; // Stores the maximum sum found so far
1113
int currSum = 0; // Stores the sum of the current subarray
12-
for(int i = 0; i < nums.size(); i++){
14+
for(size_t i = 0; i < nums.size(); i++){
1315
currSum += nums[i];
14-
maxSum = max(currSum, maxSum);
16+
maxSum = std::max(currSum, maxSum);
1517

1618
// If the current sum is negative, it won't contribute to a larger sum,
1719
// so we reset it to 0 to start a new subarray from the next element.
@@ -22,4 +24,15 @@ class Solution {
2224

2325
return maxSum;
2426
}
25-
};
27+
};
28+
29+
int main() {
30+
Solution sol;
31+
std::vector<int> nums = {-2, 1, -3, 4, -1, 2, 1, -5, 4};
32+
33+
int result = sol.maxSubArray(nums);
34+
35+
std::cout << "The maximum subarray sum is: " << result << std::endl;
36+
37+
return 0;
38+
}

0 commit comments

Comments
 (0)