ProgrammingHard
A grid DP that counts right/down paths for 1000 x 1000 cells should use what memory layout to stay small?
- A. Two rolling rows of ints, O(columns) memory, updated row by row
- B. A full 1000 x 1000 int table that is never freed, about 4 MB
- C. A table indexed by path length, about 10^9 entries
- D. A recursive function with no memo, using the stack only
Correct answer: A. Two rolling rows of ints, O(columns) memory, updated row by row
Each row depends only on the previous one, so two rows suffice. A full table is only about 4 MB, but the rolling form is cleaner, and the recursive form is exponential.
More C Programming questions
- Which approach finds the maximum sum rectangle in an n x n int matrix with n = 500 within a second in C?
- int a[2][3]; with two rows and three columns in a variable-length array, what is sizeof(a) on a platform with 4-byte int?
- char names[3][6] = {"ab", "cde"}; What is sizeof names[1]?
- Which statement about memset(a, 0, sizeof a) for a 2D int array a[4][5] is correct?
- int a[3][4]; int p = &a[0][0]; Which expression refers to a[2][1]?
- On a machine with 4-byte int, what does sizeof(r) give when int (r)[3] is a pointer to a row of three ints?