-
Notifications
You must be signed in to change notification settings - Fork 1.6k
/
Copy pathmaximize-subarray-sum-after-removing-all-occurrences-of-one-element.cpp
163 lines (150 loc) · 4.69 KB
/
maximize-subarray-sum-after-removing-all-occurrences-of-one-element.cpp
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
// Time: O(n)
// Space: O(n)
// hash table, greedy, kadane's algorithm
class Solution {
public:
long long maxSubarraySum(vector<int>& nums) {
int64_t result = numeric_limits<int64_t>::min();
int64_t curr = 0, mn = 0, mn0 = 0;
unordered_map<int, int64_t> mn1;
for (const auto& x: nums) {
curr += x;
result = max(result, curr - mn);
mn1[x] = min(mn1[x], mn0) + x;
mn0 = min(mn0, curr);
mn = min({mn, mn1[x], mn0});
}
return result;
}
};
// Time: O(n)
// Space: O(n)
// hash table, greedy, kadane's algorithm
class Solution2 {
public:
long long maxSubarraySum(vector<int>& nums) {
int64_t result = numeric_limits<int64_t>::min();
int64_t curr = 0, mn = 0, mn0 = 0;
unordered_map<int, int64_t> mn1;
for (const auto& x: nums) {
curr += x;
result = max(result, curr - mn);
if (x < 0) {
mn1[x] = min(mn1[x], mn0) + x;
mn = min(mn, mn1[x]);
}
mn0 = min(mn0, curr);
mn = min(mn, mn0);
}
return result;
}
};
// Time: O(nlogn)
// Space: O(n)
// segment tree
class Solution_TLE {
public:
long long maxSubarraySum(vector<int>& nums) {
enum {MAX, TOTAL, PREFIX, SUFFIX};
const auto& build = [&](int i) {
return vector<int64_t>(4, nums[i]);
};
const auto& update = [] (const auto& c) {
return c;
};
const auto& query = [&] (const auto& x, const auto& y) {
if (empty(x)) {
return y;
}
if (empty(y)) {
return x;
}
return vector<int64_t>{
max({x[MAX], y[MAX], x[SUFFIX] + y[PREFIX]}),
x[TOTAL] + y[TOTAL],
max(x[PREFIX], x[TOTAL] + y[PREFIX]),
max(y[SUFFIX], x[SUFFIX] + y[TOTAL])
};
};
const int mx = ranges::max(nums);
if (mx < 0) {
return mx;
}
const int mn = ranges::min(nums);
if (mn >= 0) {
return accumulate(cbegin(nums), cend(nums), 0ll);
}
unordered_map<int, vector<int>> groups;
for (int i = 0; i < size(nums); ++i) {
groups[nums[i]].emplace_back(i);
}
SegmentTree<vector<int64_t>> st(size(nums), build, update, query);
int64_t result = st.tree[1][0]; // st.query(0, size(nums) - 1)[0]
for (const auto& [k, v] : groups) {
vector<int64_t> arr;
for (const auto& i : v) {
st.update(i, arr);
}
result = max(result, st.tree[1][0]); // st.query(0, size(nums) - 1)[0]
arr.assign(4, k);
for (const auto& i : v) {
st.update(i, arr);
}
}
return result;
}
private:
// Template:
// https://github.com/kamyu104/LeetCode-Solutions/blob/master/C++/booking-concert-tickets-in-groups.cpp
template <typename T>
class SegmentTree {
public:
explicit SegmentTree(
int N,
const function<T(const int&)>& build_fn,
const function<T(const T&)>& update_fn,
const function<T(const T&, const T&)>& query_fn)
: tree(N > 1 ? 1 << (__lg(N - 1) + 2) : 2),
base(N > 1 ? 1 << (__lg(N - 1) + 1) : 1),
build_fn_(build_fn),
query_fn_(query_fn),
update_fn_(update_fn) {
for (int i = base; i < base + N; ++i) {
tree[i] = build_fn_(i - base);
}
for (int i = base - 1; i >= 1; --i) {
tree[i] = query_fn_(tree[i << 1], tree[(i << 1) + 1]);
}
}
void update(int i, const T& h) {
int x = base + i;
tree[x] = update_fn_(h);
while (x > 1) {
x >>= 1;
tree[x] = query_fn_(tree[x << 1], tree[(x << 1) + 1]);
}
}
T query(int L, int R) {
L += base;
R += base;
T left, right;
for (; L <= R; L >>= 1, R >>= 1) {
if (L & 1) {
left = query_fn_(left, tree[L]);
++L;
}
if ((R & 1) == 0) {
right = query_fn_(tree[R], right);
--R;
}
}
return query_fn_(left, right);
}
vector<T> tree;
int base;
private:
const function<T(const int&)> build_fn_;
const function<T(const T&)> update_fn_;
const function<T(const T&, const T&)> query_fn_;
};
};