LeetCode 121 Best Time to Buy and Sell Stock
2015-03-25 06:59
435 查看
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.
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.
# @param {Integer[]} prices # @return {Integer} def max_profit(prices) return 0 if prices.empty? || prices.length == 1 l, m = prices[0], 0 prices.each do |x| if x < l l = x else m = x - l > m ? x-l : m end end m end
相关文章推荐
- LeetCode: Best Time to Buy and Sell Stock [121]
- leetcode[121]Best Time to Buy and Sell Stock
- leetcode || 121、Best Time to Buy and Sell Stock
- leetcode-121 Best Time to Buy and Sell Stock
- 【LeetCode-面试算法经典-Java实现】【121-Best Time to Buy and Sell Stock(最佳买卖股票的时间)】
- leetcode 121: Best Time to Buy and Sell Stock
- LeetCode 121 Best Time to Buy and Sell Stock
- [Leetcode 37] 121 Best Time to Buy and Sell Stock
- leetcode 121 —— Best Time to Buy and Sell Stock
- LeetCode(121) Best Time to Buy and Sell Stock (Java)
- LeetCode 121: Best Time to Buy and Sell Stock
- Best Time to Buy and Sell Stock - LeetCode 121
- leetcode解题报告:121 Best Time to Buy and Sell Stock
- Leetcode #121 Best Time to Buy and Sell Stock
- LeetCode(121)Best Time to Buy and Sell Stock
- Leetcode_121_Best Time to Buy and Sell Stock
- [leetcode 121] Best Time to Buy and Sell Stock
- [LeetCode 121] - 买入与卖出股票的最佳时机(Best Time to Buy and Sell Stock)
- Leetcode 121 Best Time to Buy and Sell Stock 单次买卖股票最大收益
- Java for LeetCode 121 Best Time to Buy and Sell Stock