Sword Finger Offer-47: The greatest value of gifts _ dynamic programming

Source: Internet
Author: User
Tags array definition
Topic:

Each of the squares of a m*n board has a gift, each of which has a certain value (worth more than 0). You can start with the present in the box from the upper left corner of the chessboard and move one square at a time to the left or down, until you reach the lower right corner of the chessboard. Given a chessboard and the gift above it, calculate the most valuable gift you can take. Link:

Sword refers to offer (2nd edition): P233 Thinking Tags: algorithm: Recursive, dynamic Programming Solution: 1. Using the dynamic programming of the loop, the auxiliary two-dimensional array definition F (i,j) is used to represent the maximum value of the sum of gifts that can be obtained when a lattice arrives at coordinates (I,J). There are two ways to reach (I,J): (i-1,j) or (i,j-1); F (i,j) = Max (f (i-1,j), F (i , j-1)) + gift[i,j]; use loops to compute to avoid duplicate child problems.

 class Solution {public:int getmaxvalue_solution (const int* values, int rows, int cols) {if (values = = nullptr | | Rows <= 0 | |

        cols <= 0) return 0;
        int** maxvalues = 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 left = 0;
                int up = 0;
                if (i > 0) up = maxvalues[i-1][j];

                if (J > 0) left = maxvalues[i][j-1];
            MAXVALUES[I][J] = Std::max (left, up) + Values[i*cols + j];

        an int maxValue = maxvalues[rows-1][cols-1];
        for (int i = 0; i < rows; ++i) delete[] maxvalues[i];

        Delete[] maxvalues;
    return maxValue; }
};
2. Optimization of space complexity, using one-dimensional array topics, it is known that the maximum gift value of coordinates (I,J) depends solely on coordinates (I-1,J) and (i,j-1) two lattices, so the maximum value above the i-2 line is not necessarily preserved. Use one-dimensional array to save: (0,0) ... (0,j-1) The preservation of (i,0) ... (i,j-1) The maximum value of (0,J) ... (0,cols-1) The preservation of (I-1,J) ... (I-1,cols-1) The greatest value. Each time a new (I,J) is computed, use the maximum value of the array subscript j-1 and J to add the current gift value.
Class Solution {public
:
    int getmaxvalue_solution (const int* values, int rows, int cols) {
        if (values = = NULLP TR | | Rows <= 0 | | cols <= 0) return
            0;

        int* maxvalues = new Int[cols];
        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];
                if (J > 0) Left
                    = maxvalues[j-1];

                MAXVALUES[J] = Std::max (left, up) + Values[i*cols + j];
            }

        int maxValue = maxvalues[cols-1];

        Delete[] maxvalues;

        return maxValue;
    }
;

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.