best-time-to-buy-and-sell-stock Java code
2017-10-24 13:15
281 查看
Say you have an array for which the i th 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.
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.
import java.util.*; public class Solution { public int maxProfit(int[] prices) { if(prices.length<2){ return 0; } int maxProfit=0; int temp = prices[0]; for(int i=1; i<prices.length; i++){ temp = Math.min(temp, prices[i]); maxProfit = Math.max(maxProfit, prices[i]-temp); } return maxProfit; } }
相关文章推荐
- best-time-to-buy-and-sell-stock Java code
- best-time-to-buy-and-sell-stock Java code
- Best Time to Buy and Sell Stock III Java
- leecode 解题总结:122. Best Time to Buy and Sell Stock II
- LeetCode 121 -Best Time to Buy and Sell Stock ( JAVA )
- LeetCode 122 -Best Time to Buy and Sell Stock II ( JAVA )
- 【leetcode】121. Best Time to Buy and Sell Stock【java】
- Best Time to Buy and Sell Stock III(JAVA)-动态规划
- Best Time to Buy and Sell Stock (Java)
- [Leetcode][JAVA] Best Time to Buy and Sell Stock I, II, III
- 【小熊刷题】Best Time to Buy and Sell Stock I <leetcode 121 Java>
- leecode 解题总结:188. Best Time to Buy and Sell Stock IV
- 【LeetCode-面试算法经典-Java实现】【121-Best Time to Buy and Sell Stock(最佳买卖股票的时间)】
- [LeetCode][Java] Best Time to Buy and Sell Stock IV
- LeetCode(121) Best Time to Buy and Sell Stock (Java)
- (java)Best Time to Buy and Sell Stock系列
- Best Time to Buy and Sell Stock Java
- Best Time to Buy and Sell Stock II Java
- 【leetcode】123. Best Time to Buy and Sell Stock III【java】
- (java)Best Time to Buy and Sell Stock