【LeetCode】HOT 100(9)

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: 53. Maximum Subarray Sum - Leetcode

The interface of the topic:

Problem-solving ideas:

code:

It's over! ! ! !

Title: 55. Jumping Game - Leetcode

The interface of the topic:

Problem-solving ideas:

code:

It's over! ! ! !

Write at the end:


Title: 53. Maximum Subarray Sum - Leetcode

The interface of the topic:

class Solution {
public:
    int maxSubArray(vector<int>& nums) {

    }
};

Problem-solving ideas:

This question cannot be passed violently, and dynamic programming or divide-and-conquer is required.

But divide and conquer is too complicated, and I don't know how to do dynamic programming.

So I tried greediness, and it turned out that greedy can also pass, unexpectedly,

I thought greed couldn't get over it,

greedy:

Traverse the array, calculate the interval and sum

If sum < 0 restart the calculation

code show as below:

code:

class Solution {
public:
    int maxSubArray(vector<int>& nums) {
        int ans = INT_MIN;
        int n = nums.size();
        int sum = 0;
        for(int i = 0; i < n; i++) {
            sum += nums[i];
            ans = max(ans, sum);
            if(sum < 0) {
                sum = 0;
            }
        }
        return ans;
    }
};

It's over! ! ! !

Title: 55. Jumping Game - Leetcode

The interface of the topic:

class Solution {
public:
    bool canJump(vector<int>& nums) {
        
    }
};

Problem-solving ideas:

The idea of ​​this question is very clever.

The main ideas are as follows:

Traverse the array and record the farthest distance each position can travel,

If the farthest distance >= the last subscript of the array, return true directly,

If the subscript of the traversal array < the farthest distance we can go, it proves that we can't go anywhere, and return false

code show as below:

code:

class Solution {
public:
    bool canJump(vector<int>& nums) {
        int jump = 0; //记录每个位置最远能走的距离
        for(int i = 0; i < nums.size(); i++) { //遍历整个数组
            if(i > jump) return false; //遍历数组的下标 < 我们能走的最远距离
            if(jump >= nums.size() - 1) return true; //最远距离 >= 数组最后的下标
            jump = max(jump, i + nums[i]); //i + nums[i]是我们这个位置能走的最远距离
        }
        return true;
    }
};

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