【发布时间】:2020-07-02 04:21:12
【问题描述】:
问题-假设您有一个数组,其中 ith 元素是给定股票在 dayi 的价格。
如果您最多只能完成一次交易(即买入并卖出一股股票),请设计一种算法来找到最大利润。
请注意,您不能在买入股票之前先卖出股票。
示例 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.
示例 2:
Input: [7,6,4,3,1]
Output: 0
Explanation: In this case, no transaction is done, i.e. max profit = 0.
我相信这个问题可以使用动态编程来解决,在继续简单地解决这个问题之前,我尝试使用我自己的方法来解决这个问题。 我确实检查了蛮力算法并意识到我的方法与蛮力不相似
public class Solution {
public int maxProfit(int prices[]) {
int maxprofit = 0;
for (int i = 0; i < prices.length - 1; i++) {
for (int j = i + 1; j < prices.length; j++) {
int profit = prices[j] - prices[i];
if (profit > maxprofit)
maxprofit = profit;
}
}
return maxprofit;
}
}
这是我的方法
class Solution:
def maxProfit(self, prices: List[int]) -> int:
res=0
if not prices:
return 0
idx=prices.index(min(prices))
value=min(prices)
try:
for i in range (idx+1,len(prices)):
res=max(res,prices[i]-value)
except IndexError :
return 0
return res
我的代码通过了示例测试用例和 143/200 用例,但这次失败了。
Input: [2,4,1]
Output: 0
Expected: 2
如何改进我的代码?我怎样才能使这种方法起作用?或者如果这种方法完全错误,请详细说明。
我相信我的方法的时间复杂度比蛮力要好,因此,努力使这段代码工作;稍后检查动态编程方法
【问题讨论】:
-
该示例的问题是最低价格在索引 2 中,因此您的 for 循环什么也不做,因为它只查看列表中最低价格之后的价格。
-
每个 Stack Overflow 问题都应该是关于一个狭隘的、具体的问题,并考虑到对该问题而言并非必不可少的所有问题。我们不允许在这里提出一般改进的请求。请参阅codereview.meta.stackexchange.com/questions/5777/…,了解我们的规则与姊妹网站 Code Review 的规则之间的差异(尽管他们要求问题是关于工作代码的,所以现在这在那儿是不可接受的)。
标签: python algorithm time-complexity dynamic-programming