[June training camp] day28 dynamic programming

2310. Sum of integers whose unit digit is K

Title description:

Given you two integers num and k, consider the multiset of positive integers with the following properties:

Each integer digit is k .
The sum of all integers is num.
Returns the minimum size of this multiset, or -1 if no such multiset exists.

Notice:

A multiset is similar to a set, but a multiset can contain more than one of the same integer, and the sum of an empty multiset is 0.
The ones digit is the rightmost digit of the number.

Ideas:

1. Initialize all numbers whose unit digit is k, a total of 300, (num up to 3000)
2. Then each number is the capacity of an item, and the consumption of the item is 1.
3. The total capacity of the backpack is num, so It turns into a complete knapsack problem, find the minimum consumption of the complete knapsack with a capacity of num

class Solution {
    int pack[310],packsize;
public:
    int minimumNumbers(int num, int k) {
        packsize = 0;
        int dp[3010];
        //将所有个位数字为k的数初始化出来,总共300个
        for (int i = 0; i <= num; ++i) {
            if (i % 10 == k) {
                pack[++packsize] = i; 
            }
        }

        for (int j = 0; j <= num; ++j) {
            dp[j] = 100000000;
        }
        dp[0] = 0;
        for (int i = 1; i <= packsize; ++i) {
            for (int j = pack[i]; j <= num; ++j) {
                //dp[j - pack[i]] + 1 表示前i个数组合出j,所需要的数字的最小个数
                if (dp[j - pack[i]] + 1 < dp[j]) {
                    dp[j] = dp[j - pack[i]] + 1;
                }
            }
        }
        return dp[num] >= 100000000 ? -1:dp[num];

        
    }
};

1594. Maximum Nonnegative Product of Matrices

Title description:

You are given a matrix grid of size rows x cols. Initially, you are located in the upper left corner (0, 0), and with each step, you can move right or down in the matrix.

Among all the paths starting at the upper left corner (0, 0) and ending at the lower right corner (rows - 1, cols - 1), find the path with the largest nonnegative product. The product of a path is the product of all the integers in the cells visited along the path.

Returns the modulo 109 + 7 of the largest nonnegative product. Returns -1 if the maximum product is negative.

Note that the remainder is performed after the maximum product is obtained

Ideas:

1. User dpmin[i][j] records the minimum product from (0,0) to (i,j), and uses dpmax[i][j] to record from (0,0) to (i,j) ) points, the maximum value and the minimum value need to be calculated every time, because it may be a negative number, and the maximum value can be obtained at the end.
2. Each state transition can come from the left or from the top, so after the m*n state transition, the value of dpmax[m-1][n-1] is the final answer, if it is less than 0 , then output -1, don't forget to take the modulus

class Solution {
    #define ll long long
    ll dpmin[16][16], dpmax[16][16];
    ll getmin(int now, int i, int j) {
        return min(dpmin[i][j]*now, dpmax[i][j]*now);
    }
    ll getmax(int now, int i, int j) {
        return max(dpmin[i][j]*now, dpmax[i][j]*now);
    }
public:
    int maxProductPath(vector<vector<int>>& grid) {
        int m = grid.size();
        int n = grid[0].size();

        for (int i = 0; i < m; ++i) {
            for (int j = 0; j < n; ++j) {
                if (!i & !j) { //i=j=0,初始化dpmax和dpmin
                    dpmax[i][j] = dpmin[i][j] = grid[i][j];
                } else if(!i) { //i=0时只能从上面下来
                    dpmax[i][j] = getmax(grid[i][j], i, j-1);
                    dpmin[i][j] = getmin(grid[i][j], i, j-1);
                } else if(!j) {//j=0时只能从左边下来
                    dpmax[i][j] = getmax(grid[i][j], i-1, j);
                    dpmin[i][j] = getmin(grid[i][j], i-1, j);
                } else  {
                    dpmax[i][j] = getmax(grid[i][j], i, j-1);
                    dpmax[i][j] = max(dpmax[i][j], getmax(grid[i][j], i-1, j));
                    dpmin[i][j] = getmin(grid[i][j], i, j-1);
                    dpmin[i][j] = min(dpmin[i][j], getmin(grid[i][j], i-1, j));
                }
            }
        }

        if (dpmax[m-1][n-1] < 0) return -1;
        else return dpmax[m-1][n-1]%1000000007;
    }
    
};

Guess you like

Origin blog.csdn.net/ilovejujube/article/details/125500161