开始刷leetcode day12:Best Time to Buy and Sell Stock
来源:互联网 发布:演唱会市场怎样 知乎 编辑:程序博客网 时间:2024/06/07 03:37
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.
Java
public class Solution {
public int maxProfit(int[] prices) {
if(prices.length == 0) return 0;
int maxProfit = 0;
int min = prices[0];
for(int i=0; i<prices.length; i++)
{
if(prices[i]>min && (prices[i] - min > maxProfit))
{
maxProfit = prices[i] - min;
}
else if(prices[i] < min) min = prices[i];
}
return maxProfit;
}
}
0 0
- 开始刷leetcode day12:Best Time to Buy and Sell Stock
- 开始刷leetcode day12:Best Time to Buy and Sell Stock II
- 【LeetCode】Best Time to Buy and Sell Stock & Best Time to Buy and Sell Stock
- [LeetCode] [LeetCode]Best Time to Buy and Sell Stock
- 【LeetCode with Python】 Best Time to Buy and Sell Stock
- [LeetCode]Best Time to Buy and Sell Stock
- [LeetCode]Best Time to Buy and Sell Stock II
- LeetCode Best Time to Buy and Sell Stock
- LeetCode Best Time to Buy and Sell Stock II
- LeetCode Best Time to Buy and Sell Stock III
- Leetcode: Best time to buy and sell a stock I
- Leetcode: Best Time to Buy and Sell Stock II
- LeetCode Best Time to Buy and Sell Stock III
- [Leetcode] Best Time to Buy and Sell Stock
- [Leetcode] Best Time to Buy and Sell Stock II
- [Leetcode] Best Time to Buy and Sell Stock III
- LeetCode : Best Time to Buy and Sell Stock II
- [leetcode] Best Time to Buy and Sell Stock
- td标签不换行CSS写法
- sql 顺序产生大小字符
- weblogic各种线程数的关系的看法
- 批处理批量更改文件名称
- 直接拿来用!十大Material Design开源项目
- 开始刷leetcode day12:Best Time to Buy and Sell Stock
- 详解android Activity之间数据传递 Parcelable&Serializable
- Android中POST请求中的UTF-8编码问题
- [leetcode]Course Schedule II
- Gallery图片浏览
- spring学习笔记(25)——spring基本思路
- android intent-infliter 过滤验证
- leetcode笔记:2Sum
- python