Skip to content

Commit 169a476

Browse files
authored
Create maximum-coin-collection.cpp
1 parent 01a4995 commit 169a476

File tree

1 file changed

+18
-0
lines changed

1 file changed

+18
-0
lines changed

C++/maximum-coin-collection.cpp

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
// Time: O(n)
2+
// Space: O(1)
3+
4+
// dp
5+
class Solution {
6+
public:
7+
long long maxCoins(vector<int>& lane1, vector<int>& lane2) {
8+
int64_t NEG_INF = numeric_limits<int64_t>::min();
9+
int64_t result = NEG_INF, dp1 = NEG_INF, dp12 = NEG_INF, dp121 = NEG_INF;
10+
for (int i = 0; i < size(lane1); ++i) {
11+
dp1 = max(dp1, static_cast<int64_t>(0)) + lane1[i];
12+
dp12 = max(max(dp12, static_cast<int64_t>(0)) + lane2[i], dp1);
13+
dp121 = max(max(dp121, static_cast<int64_t>(0)) + lane1[i], dp12);
14+
result = max({result, dp1, dp121});
15+
}
16+
return result;
17+
}
18+
};

0 commit comments

Comments
 (0)