Best Time to Buy and Sell Stock

mac2022-06-30  97

题目:

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.

 

 

1 public class Solution { 2 public int maxProfit(int[] prices) 3 { 4 int MaxProfit=0; 5 int MinElement=Integer.MAX_VALUE; 6 for(int i=0;i<prices.length;i++) 7 { 8 MinElement=Math.min(MinElement,prices[i]); 9 MaxProfit=Math.max(MaxProfit,prices[i]-MinElement); 10 11 } 12 return MaxProfit; 13 } 14 }

 

reference: http://lifexplorer.me/leetcode-best-time-to-buy-and-sell-stock-i-ii-iii/

转载于:https://www.cnblogs.com/hygeia/p/4636342.html

相关资源:JAVA上百实例源码以及开源项目
最新回复(0)