LeetCode刷题 Day32
122. Best Time to Buy and Sell Stock II
You are given an integer array prices where prices[i] is the price of a given stock on the ith day.
On each day, you may decide to buy and/or sell the stock. You can only hold at most one share of the stock at any time. However, you can buy it then immediately sell it on the same day.
Find and return the maximum profit you can achieve.
Example 1:
Input: prices = [7,1,5,3,6,4]
Output: 7
Explanation: Buy on day 2 (price = 1) and sell on day 3 (price = 5), profit = 5-1 = 4.
Then buy on day 4 (price = 3) and sell on day 5 (price = 6), profit = 6-3 = 3.
Total profit is 4 + 3 = 7.
Example 2:
Input: prices = [1,2,3,4,5]
Output: 4
Explanation: Buy on day 1 (price = 1) and sell on day 5 (price = 5), profit = 5-1 = 4.
Total profit is 4.
Example 3:
Input: prices = [7,6,4,3,1]
Output: 0
Explanation: There is no way to make a positive profit, so we never buy the stock to achieve the maximum profit of 0.
思路:
- 因为可以多次交易可以将price[i] - price[i - 1] > 0的值累加起来
代码:
var maxProfit = function(prices) {
let max = 0;
for (let i = 1; i < prices.length; i++) {
const diff = prices[i] - prices[i - 1];
if (diff > 0) max += diff;
}
return max;
};
时间复杂度: O(n) 空间复杂度: O(1)
55. Jump Game
You are given an integer array nums. You are initially positioned at the array's first index, and each element in the array represents your maximum jump length at that position.
Return true if you can reach the last index, or false otherwise.
Example 1:
Input: nums = [2,3,1,1,4]
Output: true
Explanation: Jump 1 step from index 0 to 1, then 3 steps to the last index.
Example 2:
Input: nums = [3,2,1,0,4]
Output: false
Explanation: You will always arrive at index 3 no matter what. Its maximum jump length is 0, which makes it impossible to reach the last index.
思路:
- 算每一步的coverage
- for loop中最大值条件要随之更新
var canJump = function(nums) {
let currPos = 0;
for (let i = 0; i <= currPos; i++) {
currPos = Math.max(nums[i] + i, currPos);
if (currPos >= nums.length - 1) return true;
}
return false;
};
时间复杂度: O(n), 空间复杂度: O(1)
45. Jump Game II
You are given a 0-indexed array of integers nums of length n. You are initially positioned at nums[0].
Each element nums[i] represents the maximum length of a forward jump from index i. In other words, if you are at nums[i], you can jump to any nums[i + j] where:
0 <= j <= nums[i]andi + j < n
Return the minimum number of jumps to reach nums[n - 1]. The test cases are generated such that you can reach nums[n - 1].
Example 1:
Input: nums = [2,3,1,1,4]
Output: 2
Explanation: The minimum number of jumps to reach the last index is 2. Jump 1 step from index 0 to 1, then 3 steps to the last index.
Example 2:
Input: nums = [2,3,0,1,4]
Output: 2
思路:
- 非常典型的一个贪心题,需要两个range: curr, next;
- 当i达到当前的最大范围时,如果curr === length - 1, break; 否则需要走下一步: 这样step++, curr = next, 如果next >= length - 1, break;
- 返回step值
代码:
var jump = function(nums) {
if (nums.length === 1) return 0;
let steps = 0;
let currentPos = 0;
let nextPos = 0;
for (let i = 0; i < nums.length; i++) {
nextPos = Math.max(nums[i] + i, nextPos); // update the next position
if (currentPos === i) { // meet the current maximum range
if (currentPos !== nums.length - 1) {
steps++;
currentPos = nextPos;
if (nextPos >= nums.length - 1) break;
} else break;
}
}
return steps;
};
时间复杂度: O(n), 空间复杂度: O(1)