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.
Constraints:
1 <= prices.length <= 3 * 104
0 <= prices[i] <= 104
class Solution:
def maxProfit(self, prices: List[int]) -> int:
count = 0
for i in range(len(prices) -1):
count = count + max(prices[i+1] - prices[i], 0)
return count
Complexity Analysis:
-
Time Complexity
:
O(n) -
Space Complexity
:
O(1)
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.
Constraints:
1 <= nums.length <= 104
0 <= nums[i] <= 105
class Solution:
def canJump(self, nums: List[int]) -> bool:
cover = 0
for index, jump in enumerate(nums):
# if max cover cannot reach index
# just return False
if cover < index:
return False
# pick up the longest cover jump
cover = max(cover, index + jump)
#never meet the case of False
return True
``
**Complexity Analysis:**
- *`Time Complexity`*:<br>
O(n)
- *`Space Complexity`*:<br>
O(1)
<br>
![Dividing Line](https://github.com/samuelusc/Algomuscle/blob/main/assets/CatDividing.png)
<br>
<h2 id = "45"><a href="https://leetcode.com/problems/jump-game-ii">45. Jump Game II</a></h2><h3>Medium</h3><p>You are given a <strong>0-indexed</strong> array of integers <code>nums</code> of length <code>n</code>. You are initially positioned at <code>nums[0]</code>.</p>
<p>Each element <code>nums[i]</code> represents the maximum length of a forward jump from index <code>i</code>. In other words, if you are at <code>nums[i]</code>, you can jump to any <code>nums[i + j]</code> where:</p>
<ul>
<li><code>0 <= j <= nums[i]</code> and</li>
<li><code>i + j < n</code></li>
</ul>
<p>Return <em>the minimum number of jumps to reach </em><code>nums[n - 1]</code>. The test cases are generated such that you can reach <code>nums[n - 1]</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [2,3,1,1,4]
<strong>Output:</strong> 2
<strong>Explanation:</strong> 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.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [2,3,0,1,4]
<strong>Output:</strong> 2
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 10<sup>4</sup></code></li>
<li><code>0 <= nums[i] <= 1000</code></li>
<li>It's guaranteed that you can reach <code>nums[n - 1]</code>.</li>
</ul>
### My Solution 1:_`greedy - longest cover`_
```python
class Solution:
def jump(self, nums: List[int]) -> int:
last, cover, count = 0, 0, 0
for i in range(len(nums) - 1):
cover = max(cover, nums[i] + i)
if i == last:
count += 1
last = cover
return count
Complexity Analysis:
-
Time Complexity
:
O(n) -
Space Complexity
:
O(1)
You are given an array of non-negative integers arr and a start index. When you are at an index i, you can move left or right by arr[i]. Your task is to figure out if you can reach value 0.
Example 1:
Input: arr = [3, 4, 2, 3, 0, 3, 1, 2, 1], start = 7
Output: true
Explanation: left -> left -> right
Example 2:
Input: arr = [3, 2, 1, 3, 0, 3, 1, 2, 1], start = 2
Output: false
from typing import List
from collections import deque
def jump_game(arr: List[int], start: int) -> bool:
# 我们使用BFS(适合寻找最短路径)
visited = set()
queue = deque([start])
while queue:
size = len(queue)
for i in range(size):
cur_index = queue.popleft()
if cur_index in visited:
continue
visited.add(cur_index)
if arr[cur_index] == 0:
return True
if cur_index + arr[cur_index] < len(arr):
queue.append(cur_index + arr[cur_index])
if cur_index - arr[cur_index] >= 0:
queue.append(cur_index - arr[cur_index])
return False
if __name__ == '__main__':
arr = [int(x) for x in input().split()]
start = int(input())
res = jump_game(arr, start)
print('true' if res else 'false')
Complexity Analysis:
-
Time Complexity
:
O(n) -
Space Complexity
:
O(n)