【LeetCode】HOT 100(14)

Introduction to the question list:

Selected 100 most popular questions on LeetCode, suitable for beginners who are new to algorithms and data structures and those who want to improve efficiently in a short period of time, master these 100 questions, and you already have the ability to learn in code The basic ability to pass through the world.

Table of contents

Introduction to the question list:

Title: 85. Largest Rectangle - Leetcode

The interface of the topic:

Problem-solving ideas:

code:

It's over! ! ! !

Topic: 96. Different Binary Search Trees - Leetcode

The interface of the topic:

Problem-solving ideas:

code:

It's over! ! ! !

Write at the end:


Title: 85. Largest Rectangle - Leetcode

The interface of the topic:

class Solution {
public:
    int maximalRectangle(vector<vector<char>>& matrix) {

    }
};

Problem-solving ideas:

I don't know how they came up with the solution to this problem,

It is too strong, anyway, I can't think of it.

The main ideas are as follows:

Read this array row by row,

If the number of grids read is 1, the height will be +1, if it is 0, the height will become 0,

Read each line according to this rule, you can get an array to store the height,

Bring in the code for finding the maximum rectangular area of ​​the height: (It is recommended to learn this question first)

84. Largest Rectangle in Histogram - Leetcode

Then find the largest rectangle for each updated row

code show as below:

code:

class Solution {
public:
    int maximalRectangle(vector<vector<char>>& matrix) {
        if(matrix.empty()) return 0;
        int ans = 0; 
        vector<int> line(matrix[0].size() + 2, 0);
        for(int i = 0; i < matrix.size(); i++) {
            for(int j = 0; j < matrix[0].size(); j++) {
                line[j + 1] = (matrix[i][j] == '0') ? 0 : line[j + 1] + 1;
            }
            ans = max(ans, largestRectangleArea(line));
        }
        return ans;
    }
private: //带入求最大矩形的代码
    int largestRectangleArea(vector<int>& heights) {
        vector<int> st;
        int ans = 0;
        for(int i = 0; i < heights.size(); i++) {
            while(!st.empty() && heights[st.back()] > heights[i]) {
                int high = st.back();
                st.pop_back();
                int left = st.back() + 1;
                int right = i - 1;
                ans = max(ans, (right - left + 1) * heights[high]);
            }
            st.push_back(i);
        }
        return ans;
    }
};

It's over! ! ! !

Title: 96. Different Binary Search Trees - Leetcode

The interface of the topic:

class Solution {
public:
    int numTrees(int n) {

    }
};

Problem-solving ideas:

At first, I thought about searching and counting for this question.

But complicated ones don't allow,

can only be done with dynamic programming.

I drew a picture to observe the law, but I still don't quite understand how the following state expression is obtained.

dp[i] += dp[number of left subtree nodes with j as head node] * dp[number of right subtree nodes with j as head node]

I think I have to learn about dynamic programming systematically, otherwise I will face pain every time I encounter dynamic programming.

code show as below:

code:

class Solution {
public:
    int numTrees(int n) {
        vector<int> dp(n + 1);
        dp[0] = 1;
        for(int i = 1; i <= n; i++) {
            for(int j = 1; j <= i; j++) {
                dp[i] += dp[j - 1] * dp[i - j]; 
            }
        }
        return dp[n];
    }
};

It's over! ! ! !

Write at the end:

The above is the content of this article, thank you for reading.

If you feel that you have gained something, you can give the blogger a like .

If there are omissions or mistakes in the content of the article, please private message the blogger or point it out in the comment area~

Guess you like

Origin blog.csdn.net/Locky136/article/details/131165251