Update the solution for the paths in a grid problem.
This commit is contained in:
		
							parent
							
								
									6a8019e2ca
								
							
						
					
					
						commit
						e4506fa092
					
				|  | @ -634,12 +634,22 @@ int sum[N][N]; | ||||||
| \end{lstlisting} | \end{lstlisting} | ||||||
| and calculate the sums as follows: | and calculate the sums as follows: | ||||||
| \begin{lstlisting} | \begin{lstlisting} | ||||||
| for (int y = 1; y <= n; y++) { | for (int y = 1; y < N; y++) { | ||||||
|     for (int x = 1; x <= n; x++) { |     for (int x = 1; x < N; x++) { | ||||||
|         sum[y][x] = max(sum[y][x-1],sum[y-1][x])+value[y][x]; |         sum[y][x] = max(sum[y][x-1],sum[y-1][x])+value[y][x]; | ||||||
|     } |     } | ||||||
| } | } | ||||||
| \end{lstlisting} | \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] + std::max(sum[x-1], sum[x]); | ||||||
|  |     } | ||||||
|  | }   | ||||||
|  | \end{lstlisting} | ||||||
| The time complexity of the algorithm is $O(n^2)$. | The time complexity of the algorithm is $O(n^2)$. | ||||||
| 
 | 
 | ||||||
| \section{Knapsack problems} | \section{Knapsack problems} | ||||||
|  |  | ||||||
		Loading…
	
		Reference in New Issue