Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 15 additions & 5 deletions chapter07.tex
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,7 @@ \subsubsection{Using memoization}
for (int x = 1; x <= n; x++) {
value[x] = INF;
for (auto c : coins) {
if (x-c >= 0) {
if (x >= c) {
value[x] = min(value[x], value[x-c]+1);
}
}
Expand Down Expand Up @@ -283,7 +283,7 @@ \subsubsection{Constructing a solution}
for (int x = 1; x <= n; x++) {
value[x] = INF;
for (auto c : coins) {
if (x-c >= 0 && value[x-c]+1 < value[x]) {
if ((x >= c) && value[x-c]+1 < value[x]) {
value[x] = value[x-c]+1;
first[x] = c;
}
Expand Down Expand Up @@ -358,7 +358,7 @@ \subsubsection{Counting the number of solutions}
count[0] = 1;
for (int x = 1; x <= n; x++) {
for (auto c : coins) {
if (x-c >= 0) {
if (x >= c) {
count[x] += count[x-c];
}
}
Expand Down Expand Up @@ -634,12 +634,22 @@ \section{Paths in a grid}
\end{lstlisting}
and calculate the sums as follows:
\begin{lstlisting}
for (int y = 1; y <= n; y++) {
for (int x = 1; x <= n; x++) {
for (int y = 1; y < N; y++) {
for (int x = 1; x < N; x++) {
sum[y][x] = max(sum[y][x-1],sum[y-1][x])+value[y][x];
}
}
\end{lstlisting}
However, below is a better solution that only uses one dimensional
array to keep track of the maximum sums
\begin{lstlisting}
for (int y = 1; y < N; ++y) {
sum[0] += value[y][0];
for (int x = 1; x < N; ++x) {
sum[x] = value[y][x] + max(sum[x-1], sum[x]);
}
}
\end{lstlisting}
The time complexity of the algorithm is $O(n^2)$.

\section{Knapsack problems}
Expand Down