Say you have an array for which the ith element is the price of a given stock on day i.
If you were only permitted to complete at most one transaction (ie, buy one and sell one share of the stock), design an algorithm to find the maximum profit.
public class Solution {
public int maxProfit(int[] prices) {
// Start typing your Java solution below
// DO NOT write main() function
if(prices.length == 0)
return 0;
int profit = 0;
int minValue = prices[0];
for(int i = 0; i < prices.length; i++){
if(prices[i] - minValue > profit)
profit = prices[i] - minValue;
if(prices[i] < minValue)
minValue = prices[i];
}
return profit;
}
}

本文介绍了一种用于计算给定股票价格数组中最大可能利润的算法。该算法假设只能进行一次买卖操作,并通过迭代找到最低买入价格和最高卖出价格之间的最大差额。
427

被折叠的 条评论
为什么被折叠?



