- 921. Minimum Add to Make Parentheses Valid 使括号有效的最少添加
- 915. Partition Array into Disjoint Intervals 分割数组
- 932. Beautiful Array 漂亮数组
- 940. Distinct Subsequences II 不同的子序列 II
题目地址:https://leetcode.com/problems/best-time-to-buy-and-sell-stock/open in new window
Total Accepted: 98941 Total Submissions: 274449 Difficulty: Easy
Sayyou have an array for which the ith element is the price of a given stock on day i.
Ifyou were only permitted to complete at most one transaction (i.e., buy one and sell one share of the stock), design an algorithm to find the maximum profit.
Note that you cannot sell a stock before you buy one.
Example 1:
Input: [7,1,5,3,6,4]
Output: 5
Explanation: Buy on day 2 (price = 1) and sell on day 5 (price = 6), profit = 6-1 = 5.
Not 7-1 = 6, as selling price needs to be larger than buying price.
Example 2:
Input: [7,6,4,3,1]
Output: 0
Explanation: In this case, no transaction is done, i.e. max profit = 0.
可以在某一天买股票,在之后卖股票,求最大收益。
动态规划求解。
因为买股票再卖掉是要求有先后顺序的。肯定是找到当前项的值与之前最小值的差值的最大值。
动态规划问题,可以用数组去写。我偷看别人的,没用数组。
1、 保存在见到这个数值时,之前的所有值的最小值2.记录当前值与这个最小值的差3.找到在所有里边差值的最大值;
不得不说下面这个方法还是很巧的。因为没有笨的像我一样在每个值时都去遍历查找之前所有值的最小值。而是采用保存下来的方法。
public class Solution {
public int maxProfit(int[] prices) {
if(prices.length<2) return 0;
int maxProfit=0;
int min=prices[0];
int cur=0;
for(int i=1;i<prices.length;i++){
cur=prices[i];
min=Math.min(min,cur);
maxProfit=Math.max(maxProfit,cur-min);
}
return maxProfit;
}
}
1 2 3 4 5 6 7 8 9 10 11 12 13 14
AC:3ms
四刷,使用数组分别保存每个数字左边和右边的最小值与最大值,然后我们找到每个位置它右边的最大值-左边的最小值即可。
class Solution:
def maxProfit(self, prices):
"""
:type prices: List[int]
:rtype: int
"""
if not prices: return 0
N = len(prices)
mins = [0] * N
maxs = [0] * N
mins[0] = prices[0]
for i in range(1, N):
mins[i] = min(mins[i - 1], prices[i])
maxs[N - 1] = prices[N - 1]
for j in range(N - 2, -1, -1):
maxs[j] = max(maxs[j + 1], prices[j])
return max(maxs[i] - mins[i] for i in range(N))
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
二刷,python。只使用了两个变量,保存目前的最小值和当前最大的收益。
class Solution(object):
def maxProfit(self, prices):
"""
:type prices: List[int]
:rtype: int
"""
if not prices: return 0
minPrice = float('inf')
profit = 0
for price in prices:
minPrice = min(minPrice, price)
profit = max(profit, price - minPrice)
return profit
1 2 3 4 5 6 7 8 9 10 11 12 13
三刷,保存最新的最小值和最大值。这样只用遍历一次,如果遇到更小的值,那么把最小值和最大值同时更新;如果遇到更大的值,那么只更新最大值;最后的结果是最大值和最小值的差。
class Solution(object):
def maxProfit(self, prices):
"""
:type prices: List[int]
:rtype: int
"""
N = len(prices)
res = 0
minP, maxP = float("inf"), 0
for i in range(N):
if minP > prices[i]:
minP = prices[i]
maxP = 0
if maxP < prices[i]:
maxP = prices[i]
res = max(res, maxP - minP)
return res
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
五刷,我们在遍历的过程中,始终保持目前最好的收益,那么遍历到最后的结果就是我们要求的。这个思路,在很多题目里面都有运用。
这个题而言,使用变量minP保存当前遇到了的最小值,那么,我们遇到每一个值的时候,都减去当前的最小值就是收益,所有收益最大值就是最大收益。
class Solution {
public:
int maxProfit(vector<int>& prices) {
int minP = INT_MAX;
int res = 0;
for (int p : prices) {
if (p < minP)
minP = p;
res = max(res, p - minP);
}
return res;
}
};
1 2 3 4 5 6 7 8 9 10 11 12 13
DDKK.COM 弟弟快看-教程,程序员编程资料站,版权归原作者所有
本文经作者:负雪明烛 授权发布,任何组织或个人未经作者授权不得转发
上下文 我有一个日间 TUMBLINGWINDOW(类似于下面所示的) SELECT DATEADD(day, -1, System.Timestamp()) AS WindowStart
因此,我正在开发一个非常小的 Web 组件,以作为更大的设计系统的一部分。 我对网络组件的使用有点陌生,但我知道这个特定的网络组件可以在一个布局中使用很多很多次。 此 Web 组件控制在传递给它的任何
我刚刚开发了一个 Android 应用程序(minSdkVersion 23/targetSdkVersion 29),可以连接到 BluetoothLE 设备以定期获取数据。 现在,在 MainAc
就目前而言,这个问题不适合我们的问答形式。我们希望答案得到事实、引用或专业知识的支持,但这个问题可能会引起辩论、争论、投票或扩展讨论。如果您觉得这个问题可以改进并可能重新打开,visit the he
关闭。这个问题是opinion-based .它目前不接受答案。 想要改进这个问题? 更新问题,以便 editing this post 可以用事实和引用来回答它. 关闭 9 年前。 Improve
我一直在阅读有关 Airplay SDK 和 Dragonfire SDK 的内容。这些是在没有 Mac 的情况下进行 iphone 开发的合法选择吗?大家有更好的解决办法吗? 最佳答案 由于签名等原
我正在考虑使用 DTO 而不是传递我的域对象。我在这里和其他地方都读过几篇文章,我知道有几种方法可以完成这项工作。 如果我总共只有大约 10 个域类,并且考虑到我想在我的 View (WPF 前端)中
我的问题是,如果我有一个列表 list = [1,2,3,4,5,6,7] 如果我想知道这个列表中元素的数量,是len(list) 或 list.count 更好的选择?还是 .count 仅适用于来
我想知道 Django Rest Framework 的最佳实践。我一直通过每个用户使用不同的序列化程序(员工、帐户所有者、其他人)和 HTTP 方法来限制更改帐户上某些属性的访问权限,但我觉得这太不
我有数百个用于测试组件的脚本。这些脚本中的每一个都包含一组下标和单独的记录。 下标可以在多个 TC_Level 脚本中使用,甚至可以在其他下标中使用。 每个脚本都有一个唯一的名称。 例子: TC_1
我正在建立一个以两种语言提供产品信息的网站:英文和中文。 每个产品必须有英文名称,也可以有中文名称。 每次请求产品页面时,request检查对象以确定产品名称应该以英文还是中文显示。在后一种情况下,如
场景:应用程序具有可以启用某些优化的算法。默认情况下,有些是打开的,有些是关闭的。在 Java 中是否有一种方便的方法来实现某种全局属性系统? 要求: 必须很容易两次使用不同的设置运行程序的多个实例
我从事嵌入式工作,我有一些管理硬件的软件模块。这个模块有状态,状态转换很复杂:根据事件,模块可以从状态 A 到状态 B 或者可能到 C .但是,当它退出某个状态时,它应该对硬件执行一些操作,以使其也保
我有一个免费网站,可以流式传输实时股票期权数据。我想让用户制作并保存他们自己的 JavaScript 可调用工具来解释选项数据。用户可以调用这些自定义工具来帮助他们自己做出有关期权的买卖决定。 但对于
关闭。这个问题是opinion-based .它目前不接受答案。 想要改进这个问题? 更新问题,以便 editing this post 可以用事实和引用来回答它. 关闭 2 年前。 社区在 6 个
我不是 DBA,我不知道什么是最好的解决方案。我有两个表, Custumers Table CustomerId (primary key, identity) ... 和 Suppliers Tab
关闭。这个问题是opinion-based .它目前不接受答案。 想改善这个问题吗?更新问题,以便可以通过 editing this post 用事实和引文回答问题. 7年前关闭。 Improve t
我一直在阅读关于版本控制主题的所有问题,但我认为我没有找到一个看起来像我自己的场景。 场景是: 我们有一个中型/大型 Web 应用程序,它有(至少应该有)一个部署到所有客户端的核心。当我们向客户演示应
我希望根据继承来组织一些项目,目的是确定哪些项目是连接最密集的父项,并且还只是看到形成的连接。 通常这将通过拓扑排序来完成,但我的图有循环。是否有类似“尽力而为”拓扑排序的东西,它可以尝试根据连接数或
以下在 JQuery 中附加事件处理程序的方法有什么区别? (function () { var $body = $("body"); $('button').click(functi
我是一名优秀的程序员,十分优秀!