-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgetMaxValue.cpp
More file actions
53 lines (47 loc) · 1.3 KB
/
getMaxValue.cpp
File metadata and controls
53 lines (47 loc) · 1.3 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
// 47 礼物的最大价值
#include "array_util.h"
using namespace std;
/// dp
int getMaxValue_v1(vector<vector<int>> values)
{
if (values.size() == 0)
return 0;
if (values[0].size() == 0)
return 0;
int rows = values.size();
int cols = values[0].size();
vector<vector<int>> max_v(rows, vector<int>(cols));
for (size_t i = 0; i < rows; i++) {
for (size_t j = 0; j < cols; j++) {
int left = 0, up = 0;
if (i > 0)
up = max_v[i - 1][j];
if (j > 0)
left = max_v[i][j - 1];
max_v[i][j] = max(left, up) + values[i][j];
}
}
return max_v[rows - 1][cols - 1];
}
// 优化空间使用 只占用一行 即values[0].size()
int getMaxValue_v2(vector<vector<int>> values)
{
if (values.size() == 0)
return 0;
if (values[0].size() == 0)
return 0;
int rows = values.size();
int cols = values[0].size();
vector<int> max_v(cols);
for (size_t i = 0; i < rows; i++) {
for (size_t j = 0; j < cols; j++) {
int left = 0, up = 0;
if (i > 0)
up = max_v[j];
if (j > 0)
left = max_v[j - 1];
max_v[j] = max(left, up) + values[i][j];
}
}
return max_v[cols - 1];
}