leetcode 122. The best time to buy and sell stocks II
Topic Link: 122. The Best Time to Buy and Sell Stocks II - LeetCode
topic overview
You are given an array of integers prices
, which prices[i]
represents the price of a certain stock on the first i
day.
On each day, you can decide whether to buy and/or sell stocks. You can hold no more than one share of stock at any one time . You can also buy first and then sell on the same day .
Return the maximum profit you can make .
Example 1:
Input: prices = [7,1,5,3,6,4] Output: 7 Explanation: Buy on day 2 (stock price = 1), sell on day 3 (stock price = 5) Out, this transaction can make profit = 5 - 1 = 4. Then, buy on the 4th day (stock price = 3) and sell on the 5th day (stock price = 6), this transaction can make profit = 6 - 3 = 3. The total profit is 4 + 3 = 7.
Example 2:
Input: prices = [1,2,3,4,5] Output: 4 Explanation: buy on day 1 (stock price = 1), sell on day 5 (stock price = 5), Profit = 5 - 1 = 4 for this trade. The total profit is 4.
train of thought
An ingenious point of this question is that when we think, we can split the overall profit and only collect the daily positive profit. as the picture shows:
Local Optimum: Collect daily positive profits, Global Optimum: Find the maximum profit.
Code
There is no profit on the first day, at least it must start from the second day, so i must start from subscript 1.
class Solution {
public:
int maxProfit(vector<int>& prices) {
int result = 0;
for(int i = 1;i < prices.size();i++) {
result += max(prices[i] - prices[i - 1],0);
}
return result;
}
};
leetcode 55. Jumping game
Topic Link: 55. Jumping Game - LeetCode
topic overview
Given an array of non-negative integers nums
, you are initially at the first index of the array . Each element in the array represents the maximum length you can jump at that position.
Determine whether you can reach the last subscript, if yes, return true
; otherwise, return false
.
Example 1:
Input: nums = [2,3,1,1,4] Output: true Explanation: You can jump 1 step first, from subscript 0 to subscript 1, and then jump 3 steps from subscript 1 to the last subscript.
train of thought
When I saw this question, I was thinking about how many steps to jump, where to choose to jump, how to jump, etc. But once I think about this question, it will be very complicated and difficult. So the key to this question is not to jump a few steps but to cover the range !
Let the subscript i move within the coverage range (cover), and choose the largest number within the coverage range each time to continue covering.
The local optimum is to select the maximum coverage every time, and the global optimum is to finally get the maximum coverage to see if it can reach the end.
Code
class Solution {
public:
bool canJump(vector<int>& nums) {
int cover = 0;
if (nums.size() == 1) return true;
for (int i = 0; i <= cover; i++) {
cover = max(i + nums[i], cover);
if (cover >= nums.size() - 1) return true;
}
return false;
}
};
leetcode 45. Jumping Game II
Topic Link: 45. Jumping Game II - LeetCode
topic overview
Given an array of 0-indexed integers n
of length . The initial position is .nums
nums[0]
Each element nums[i]
represents i
the maximum length to jump forward from the index. In other words, if you are at nums[i]
, you can jump to anywhere nums[i + j]
:
0 <= j <= nums[i]
i + j < n
Returns nums[n - 1]
the minimum number of hops reached. The generated test cases can be reached nums[n - 1]
.
Example 1:
Input: nums = [2,3,1,1,4] Output: 2 Explanation: The minimum number of hops to get to the last position is 2. Jump from index 0 to index 1, jump 1 step, then jump 3 steps to reach the last position of the array.
train of thought
The idea of this question is similar to that of the previous question, and they all want to cover the range, but this question needs to record the minimum number of steps, unlike the previous question, which returns true once it reaches the end, so this is the difficulty of this question.
Code
class Solution {
public:
int jump(vector<int>& nums) {
if (nums.size() == 1) return 0;
int curDistance = 0; // 当前覆盖最远距离下标
int ans = 0; // 记录走的最大步数
int nextDistance = 0; // 下一步覆盖最远距离下标
for (int i = 0; i < nums.size(); i++) {
nextDistance = max(nums[i] + i, nextDistance); // 更新下一步覆盖最远距离下标
if (i == curDistance) { // 遇到当前覆盖最远距离下标
ans++; // 需要走下一步
curDistance = nextDistance; // 更新当前覆盖最远距离下标(相当于加油了)
if (nextDistance >= nums.size() - 1) break; // 当前覆盖最远距到达集合终点,不用做ans++操作了,直接结束
}
}
return ans;
}
};