【Leetcode】Best Time to Buy and Sell Stock
来源:互联网 发布:js 汉字长度 编辑:程序博客网 时间:2024/06/05 11:14
题目链接:https://leetcode.com/problems/best-time-to-buy-and-sell-stock/
题目:
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 int maxProfit(int[] prices) { int maxProfit = 0; int minEle = Integer.MAX_VALUE; for (int i = 0; i < prices.length; i++) { maxProfit = Math.max(maxProfit, prices[i]-minEle); minEle = Math.min(minEle, prices[i]); } return maxProfit; }
1 0
- 【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
- leetcode 16: Best Time to Buy and Sell Stock III
- LeetCode Best Time to Buy and Sell Stock
- TCP管理的4种定时器
- 运动目标检测--光流法
- python Django错误解决:A server error occurred. Please contact the administrator.
- 【Leetcode】Count and Say
- linenoise源码解析
- 【Leetcode】Best Time to Buy and Sell Stock
- nova boot代码流程分析(五):VM启动从neutron-dhcp-agent获取IP与MAC
- linux安全防护中迅速排查的一些命令
- HashTable二次探测
- 准备自己写代码读取tif高程和影像
- 【Leetcode】House Robber
- 触发器
- Android activity任务栈和四中启动模式
- 【Leetcode】Kth Largest Element in an Array