-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathmaxValueProfit.cpp
More file actions
113 lines (88 loc) · 3.58 KB
/
maxValueProfit.cpp
File metadata and controls
113 lines (88 loc) · 3.58 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
#include <vector>
#include <algorithm>
#include <iostream>
// To solve "Prefix sum" can be used.
// So the question is that there is investors these are making investment
// in the predefined range in array. At the end, maximum profit is desired.
/*
1 2 10 => 1 and 2 are the coordinates of the indices. 10 is the profit
Total profit
0 0 0 0 0
1 2 10 10 10
2 4 5 10 15 5 5
3 5 12 10 10 17 17 12
At the end max profit is 17.
In brute force, there can be double for loop to check each input vector and add them to result vector.
This will lead time complexit for O(n^2)
To fix this time complexity we can use "Prefix sum"
Instead of adding each rounds lets add only investment value as a start and end.
For the first round between 1 and 2 there is 10 profit.
So, in the investment vector investment[start] += 10; and investment[end-1] -= 10;
start = 1; end = 2;
This is because instead of adding each profit in each step, we can track the positons
and at the end we can add them together (prefix sum) to find the maximum profit/investment
In simple example lets assume between 2 and 6 we need to add 5
vector should be [0 5 5 5 5 5 0 0]
instead of wrtting like this. lets use start and end points
[0 5 0 0 0 0 0 -5] (vector size should be round+1).
then lets use prefix sum to get same result
vec[1] = 0; vec[2] = vec[1] + vec[2]; vec[3] = vec[0] + vec[1] + vec[2]; .....
Result = s[0 5 5 5 5 5 5 0].
*/
long maxValue(int n, std::vector<std::vector<int>>& rounds) {
std::vector<long> investments(n + 1, 0); // Initialize the investment array with n+1 elements, all set to 0
// Loop through each round and update the investments accordingly
for (const auto& round : rounds) {
int start = round[0];
int end = round[1];
int contribution = round[2];
investments[start - 1] += contribution; // Add the contribution to the starting index
if (end < n) {
investments[end] -= contribution; // Subtract the contribution from the ending index + 1
}
}
// Calculate the prefix sum of the investments array
for (int i = 1; i < n; ++i) {
investments[i] += investments[i - 1];
}
// Find the maximum investment value
long max_investment = *std::max_element(investments.begin(), investments.end() - 1);
return max_investment;
}
class Solution
{
public:
long maxValue(int n, std::vector<std::vector<int>>& rounds) {
// Initialize vectors to store the balances of each asset
std::vector<long long> balances(n + 1);
for(auto round : rounds)
{
auto start = round[0];
auto end = round[1];
auto investment = round[2];
for(int i=start; i<=end; i++)
{
balances[i-1] += investment;
}
}
auto maxBalance = *std::max_element(balances.begin(), balances.end());
return maxBalance;
}
};
int main() {
//std::vector<std::vector<int>> rounds = {{1, 5, 100}, {2, 4, 200}, {3, 3, 50}};
std::vector<std::vector<int>> rounds =
// {{1, 3, 10},
// {2, 4, 20},
// {1, 4, 5},
// {3, 5, 15},
// {1, 5, 25}};
{{1, 3, 5},
{2, 4, 10},
{1,2,2},
{3,4,7}};
long maxInvestment = maxValue(5, rounds);
std::cout << "Maximum investment: " << maxInvestment << std::endl;
std::cout << "Maximum investment: " << Solution{}.maxValue(5, rounds) << std::endl;
return 0;
}