【LeetCode】HOT 100(10)

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:

Topic: 56. Merging Intervals - Leetcode

The interface of the topic:

Problem-solving ideas:

code:

It's over! ! ! !

Title: 62. Different paths - Leetcode

The interface of the topic:

Problem-solving ideas:

code:

It's over! ! ! !

Write at the end:


Title: 56. Merge Intervals - Leetcode

The interface of the topic:

class Solution {
public:
    vector<vector<int>> merge(vector<vector<int>>& intervals) {

    }
};

Problem-solving ideas:

This question is not difficult, it is a simple update boundary,

I just follow the meaning of the question directly,

If the left boundary is small, update the left boundary,

If the right border is large, update the right border,

If the right boundary of the previous number < the left boundary of the current number, it proves that the interval is over.

Did you write it like this at the beginning?

It is found that the numbers given in the question are out of order, just sort them in the front, this rule is only suitable for ascending order

In addition, when I write it this way, the last group of intervals will be missed, and it will be fine to deal with it at the end.

code show as below:

code:

class Solution {
public:
    vector<vector<int>> merge(vector<vector<int>>& intervals) {
        vector<vector<int>> vv;
        if(intervals.empty()) return vv; 
        sort(intervals.begin(), intervals.end()); //排序
        int left = INT_MAX, right = -1; //确保第一次进入逻辑的时候能更新left和right
        for(int i = 0; i < intervals.size(); i++) { //遍历
            if(right != -1 && right < intervals[i][0]) { //第一次不进来,如果上一个数的右边界 < 这个数的左边界,证明证明区间结束了
                vector<int> v{left, right}; //插入区间
                vv.push_back(v);
                left = intervals[i][0]; //开始计算新的区间
                right = intervals[i][1];
            }
            if(intervals[i][0] < left) left = intervals[i][0]; //如果左边界小就更新左边界
            if(intervals[i][1] > right) right = intervals[i][1]; //如果右边界大就更新右边界
        }
        vector<int> v{left, right}; //处理最后一组区间
        vv.push_back(v);
        return vv;
    }
};

It's over! ! ! !

Title: 62. Different Paths - Leetcode

The interface of the topic:

class Solution {
public:
    int uniquePaths(int m, int n) {

    }
};

Problem-solving ideas:

I saw this question at a glance, and I felt that it could be done by searching.

However, it is obvious that the time complexity of the topic does not allow it.

It can only be done using dynamic programming, but this question is a simple dynamic programming, I will try it too,

According to the topic, we can know that the number of paths of a grid is determined by the grid above and to the left of it.

So we can actually write the transfer equation of dynamic programming:

dp[ i ][ j ] = dp[ i - 1 ][ j ] + dp[ i ][ j - 1 ];

But make sure that the first row and first column start from 1.

code show as below:

code:

class Solution {
public:
    int uniquePaths(int m, int n) {
        vector<vector<int>> dp(m, vector<int>(n, 1)); //初始化成1,满足条件
        for(int i = 1; i < m; i++) {
            for(int j = 1; j < n; j++) {
                dp[i][j] = dp[i - 1][j] + dp[i][j - 1]; //推出的状态转移方程
            }
        }
        return dp[m - 1][n - 1];
    }
};

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/131063197