forked from SjxSubham/Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path53. Maximum Subarray.cpp
More file actions
175 lines (153 loc) · 4.06 KB
/
53. Maximum Subarray.cpp
File metadata and controls
175 lines (153 loc) · 4.06 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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
//Brute Force
//This will give you TLE
//Time Complexity: O(n^2)
//Space Complexity: O(1)
#include <bits/stdc++.h>
using namespace std;
class Solution {
public:
int maxSubArray(vector<int>& nums) {
int n = nums.size();
int maxSum = INT_MIN;
for (int i = 0; i < n; i++) {
int currSum = 0;
for (int j = i; j < n; j++) {
currSum += nums[j];
maxSum = max(maxSum, currSum);
}
}
return maxSum;
}
};
//Prefix Sum
//This will give you TLE also
//Time Complexity: O(n^2)
//Space Complexity: O(n)
#include <bits/stdc++.h>
using namespace std;
class Solution {
public:
int maxSubArray(vector<int>& nums) {
int n = nums.size();
vector<int> prefix(n+1, 0);
for (int i = 0; i < n; i++) {
prefix[i+1] = prefix[i] + nums[i];
}
int maxSum = INT_MIN;
for (int i = 0; i < n; i++) {
for (int j = i; j < n; j++) {
int sum = prefix[j+1] - prefix[i];
maxSum = max(maxSum, sum);
}
}
return maxSum;
}
};
//Divide & Conquer
//This Works
//Time Complexity: O(n log n)
//Space Complexity: O(log n) recursion stack
#include <bits/stdc++.h>
using namespace std;
class Solution {
public:
int maxCrossingSum(vector<int>& nums, int l, int m, int r) {
int leftSum = INT_MIN, rightSum = INT_MIN, sum = 0;
for (int i = m; i >= l; i--) {
sum += nums[i];
leftSum = max(leftSum, sum);
}
sum = 0;
for (int i = m+1; i <= r; i++) {
sum += nums[i];
rightSum = max(rightSum, sum);
}
return leftSum + rightSum;
}
int maxSubArrayHelper(vector<int>& nums, int l, int r) {
if (l == r) return nums[l];
int m = (l + r) / 2;
return max({maxSubArrayHelper(nums, l, m),
maxSubArrayHelper(nums, m+1, r),
maxCrossingSum(nums, l, m, r)});
}
int maxSubArray(vector<int>& nums) {
return maxSubArrayHelper(nums, 0, nums.size()-1);
}
};
//Dynamic Programming
//This Works Too
//Time Complexity: O(n)
//Space Complexity: O(n)
#include <bits/stdc++.h>
using namespace std;
class Solution {
public:
int maxSubArray(vector<int>& nums) {
int n = nums.size();
vector<int> dp(n);
dp[0] = nums[0];
int maxSum = dp[0];
for (int i = 1; i < n; i++) {
dp[i] = max(nums[i], dp[i-1] + nums[i]);
maxSum = max(maxSum, dp[i]);
}
return maxSum;
}
};
//Kadane’s Algorithm
//This Will Works Too
//Time Complexity: O(n)
//Space Complexity: O(1)
#include <bits/stdc++.h>
using namespace std;
class Solution {
public:
int maxSubArray(vector<int>& nums) {
int maxSum = nums[0], currSum = nums[0];
for (int i = 1; i < nums.size(); i++) {
currSum = max(nums[i], currSum + nums[i]);
maxSum = max(maxSum, currSum);
}
return maxSum;
}
};
//Segment Tree
//This Works too
//Time Complexity: O(n) build + O(1) query for maxSubArray
//Space Complexity: O(n) for segment tree
#include <bits/stdc++.h>
using namespace std;
struct Node {
int sum, pref, suff, ans;
};
Node combine(Node l, Node r) {
Node res;
res.sum = l.sum + r.sum;
res.pref = max(l.pref, l.sum + r.pref);
res.suff = max(r.suff, r.sum + l.suff);
res.ans = max({l.ans, r.ans, l.suff + r.pref});
return res;
}
Node makeNode(int val) {
return {val, val, val, val};
}
class Solution {
public:
void build(vector<int>& nums, vector<Node>& tree, int v, int tl, int tr) {
if (tl == tr) {
tree[v] = makeNode(nums[tl]);
} else {
int tm = (tl + tr) / 2;
build(nums, tree, v*2, tl, tm);
build(nums, tree, v*2+1, tm+1, tr);
tree[v] = combine(tree[v*2], tree[v*2+1]);
}
}
int maxSubArray(vector<int>& nums) {
int n = nums.size();
vector<Node> tree(4*n);
build(nums, tree, 1, 0, n-1);
return tree[1].ans;
}
};