// face questions 47: the maximum value of the gift // Title: each cell are in a m × n discharge board has a gift, each gift has a certain value // (value greater than 0). You can begin to get a gift from the upper left corner of the grid the board, and each time left or // moves down until it reaches the bottom right corner of a checkerboard grid. Given a chessboard and above gift, please count // count the maximum number you can get the value of a gift? #include <algorithm> #include <the iostream> int getMaxValue_solution1(const int* values, int rows, int cols) { if (values == nullptr || rows <= 0 || cols <= 0) return 0; // create a two-dimensional array holds f (i, j) the maximum value of a gift int ** maxValues = new new int * [rows]; for ( int i = 0 ; i <rows; ++ i) maxValues[i] = new int[cols]; for (int i = 0; i < rows; ++i) { for (int j = 0; j < cols; ++j) { int up = 0 ; // from above f (i - 1, j) the maximum int left = 0 ; // from the left f (i, j - 1) maximum if (i > 0) up = maxValues[i - 1][j]; if (j > 0) left = maxValues[i][j - 1]; maxValues[i][j] = std::max(up, left) + values[i * cols + j]; } } int max = maxValues[rows - 1][cols - 1]; for (int i = 0; i < rows; ++i) delete[] maxValues[i]; delete[] maxValues; return max; } int getMaxValue_solution2(const int* values, int rows, int cols) { if (values == nullptr || rows <= 0 || cols <= 0) return 0; int * = maxValues new new int [cols]; // space optimization for ( int I = 0 ; I <rows; ++ I) { for (int j = 0; j < cols; ++j) { int left = 0; int up = 0; if (i > 0) up = maxValues [j]; // j-th bit is just f (i - 1, j) the maximum value of if (j > 0) left = maxValues [J - . 1 ]; // first j - 1 bit is just f (i, j - 1) the maximum maxValues [J] = STD :: max (left, up) + values [I * cols + J]; // update bits value J } } int maxValue = maxValues[cols - 1]; delete[] maxValues; return maxValue; }
// ==================== test code ==================== void the Test ( const char * TestName, const int * values, int rows, int cols, int expected) { if (getMaxValue_solution1(values, rows, cols) == expected) std::cout << testName << ": solution1 passed." << std::endl; else std::cout << testName << ": solution1 FAILED." << std::endl; if (getMaxValue_solution2(values, rows, cols) == expected) std::cout << testName << ": solution2 passed." << std::endl; else std::cout << testName << ": solution2 FAILED." << std::endl; } void test1() { // three lines of three int values [] [ . 3 ] = { { 1, 2, 3 }, { 4, 5, 6 }, { 7, 8, 9 } }; int expected = 29; test("test1", (const int*)values, 3, 3, expected); } void test2() { // four rows four int values [] [ . 4 ] = { { 1, 10, 3, 8 }, { 12, 2, 9, 6 }, { 5, 7, 4, 11 }, { 3, 7, 16, 5 } }; int expected = 53; test("test2", (const int*)values, 4, 4, expected); } void test3() { // row four int values [] [ . 4 ] = { { 1, 10, 3, 8 } }; int expected = 22; test("test3", (const int*)values, 1, 4, expected); } void test4() { int values[4][1] = { { 1 }, { 12 }, { 5 }, { 3 } }; int expected = 21; test("test4", (const int*)values, 4, 1, expected); } void test5() { // row an int values [] [ . 1 ] = { { 3 } }; int expected = 3; test("test5", (const int*)values, 1, 1, expected); } void test6() { // null pointer int expected = 0 ; test("test6", nullptr, 0, 0, expected); } int main(int argc, char* argv[]) { test1(); test2 (); test3 (); test4 (); test5(); return 0; }
Analysis: The second space optimization idea is very clever, careful analysis of the flow path in order to get out.