FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
algorithm-examples/cpp-algorithm/src/dp/rod_cutting.h at main · codejsha/algorithm-examples · GitHub
codejsha
/
algorithm-examples
Public
Notifications
You must be signed in to change notification settings
Fork
0
Star
2
Code
Issues
0
Pull requests
0
Discussions
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Issues
Pull requests
Discussions
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
algorithm-examples
/
cpp-algorithm
/
src
/
dp
/
rod_cutting.h
Copy path
More file actions
More file actions
Latest commit
History
History
History
175 lines (151 loc) · 4.63 KB
Breadcrumbs
algorithm-examples
/
cpp-algorithm
/
src
/
dp
/
rod_cutting.h
Copy path
File metadata and controls
175 lines (151 loc) · 4.63 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
#
ifndef
CPP_ALGORITHM_ROD_CUTTING_H
#
define
CPP_ALGORITHM_ROD_CUTTING_H
#
include
<
algorithm
>
#
include
<
limits
>
#
include
<
map
>
#
include
<
vector
>
namespace
RodCutting
{
/*
*
* \brief Rod cutting algorithm to call recursively.
* \param price prices for rod length
* \param length length of a rod
* \return the maximum revenue
*/
int
CutRod
(
const
std::map<
int
,
int
>& price,
int
length);
/*
*
* \brief Rod cutting algorithm with memoization that use top-down approach.
* \param price prices for rod length
* \param length length of a rod
* \return the maximum revenue
*/
int
MemoizedCutRod
(
const
std::map<
int
,
int
>& price,
int
length);
/*
*
* \brief Sub procedure for rod cutting algorithm with memoization.
* This use auxiliary array for memoization.
* \param price prices for rod length
* \param length length of a rod
* \param memo memoized results
* \return the maximum revenue
*/
int
MemoizedCutRodAux
(
const
std::map<
int
,
int
>& price,
int
length,
std::vector<
int
>& memo);
/*
*
* \brief Rod cutting algorithm that use bottom-up approach.
* \param price prices for rod length
* \param length length of a rod
* \return the maximum revenue
*/
int
BottomUpCutRod
(
const
std::map<
int
,
int
>& price,
int
length);
/*
*
* \brief Rod cutting algorithm that computes the maximum revenue and the optimal size of the first piece for given
* rod length.
* \param price prices for rod length
* \param length length of a rod
* \return the maximum revenue and the optimal size of the first piece
*/
std::tuple<
int
,
int
>
ExtendedBottomUpCutRod
(
const
std::map<
int
,
int
>& price,
int
length);
}
//
----------------------------------------------------------------------------
inline
int
RodCutting::CutRod
(
const
std::map<
int
,
int
>& price,
const
int
length)
{
if
(length ==
0
)
{
return
0
;
}
int
max_revenue = std::numeric_limits<
int
>::
min
();
for
(
int
i =
1
; i <= length; ++i)
{
max_revenue =
std::max
(max_revenue, price.
at
(i) +
CutRod
(price, length - i));
}
return
max_revenue;
}
//
----------------------------------------------------------------------------
inline
int
RodCutting::MemoizedCutRod
(
const
std::map<
int
,
int
>& price,
const
int
length)
{
std::vector<
int
>
memo
(
static_cast
<
int
>(price.
size
()) +
1
, -
1
);
return
MemoizedCutRodAux
(price, length, memo);
}
//
----------------------------------------------------------------------------
inline
int
RodCutting::MemoizedCutRodAux
(
const
std::map<
int
,
int
>& price,
const
int
length,
std::vector<
int
>& memo)
{
int
max_revenue = std::numeric_limits<
int
>::
min
();
if
(memo[length] >=
0
)
{
return
memo[length];
}
if
(length ==
0
)
{
max_revenue =
0
;
}
else
{
for
(
int
i =
1
; i <= length; ++i)
{
max_revenue =
std::max
(max_revenue, price.
at
(i) +
MemoizedCutRodAux
(price, length - i, memo));
}
}
memo[length] = max_revenue;
return
max_revenue;
}
//
----------------------------------------------------------------------------
inline
int
RodCutting::BottomUpCutRod
(
const
std::map<
int
,
int
>& price,
const
int
length)
{
std::vector<
int
>
memo
(
static_cast
<
int
>(price.
size
()) +
1
, -
1
);
memo[
0
] =
0
;
for
(
int
i =
1
; i <= length; ++i)
{
int
max_revenue = std::numeric_limits<
int
>::
min
();
for
(
int
j =
1
; j <= i; ++j)
{
max_revenue =
std::max
(max_revenue, price.
at
(j) + memo[i - j]);
}
memo[i] = max_revenue;
}
return
memo[length];
}
//
----------------------------------------------------------------------------
inline
std::tuple<
int
,
int
>
RodCutting::ExtendedBottomUpCutRod
(
const
std::map<
int
,
int
>& price,
const
int
length)
{
//
the memoization of the max revenue
std::vector<
int
>
memo
(
static_cast
<
int
>(price.
size
()) +
1
, -
1
);
memo[
0
] =
0
;
//
the optimal size of the first piece to cut off
std::vector<
int
>
optimal_first_piece
(
static_cast
<
int
>(price.
size
()) +
1
, -
1
);
for
(
int
i =
1
; i <= length; ++i)
{
int
max_revenue = std::numeric_limits<
int
>::
min
();
for
(
int
j =
1
; j <= i; ++j)
{
if
(max_revenue < price.
at
(j) + memo[i - j])
{
max_revenue = price.
at
(j) + memo[i - j];
optimal_first_piece[i] = j;
}
}
memo[i] = max_revenue;
}
return
std::make_tuple
(memo[length], optimal_first_piece[length]);
}
#
endif
Back
|
FazBrowse Home
|
New Git URL