LeetCode - Best Time to Buy and Sell Stock
来源:互联网 发布:淘宝怎么推广 编辑:程序博客网 时间:2024/04/30 06:42
public class Solution { public int maxProfit(int[] prices) { // Start typing your Java solution below // DO NOT write main() function if (prices.length < 1) return 0; int[] lowest = new int[prices.length]; int[] highest = new int[prices.length]; int min = prices[0]; for (int i=0; i<prices.length; i++) { min = Math.min(min, prices[i]); lowest[i] = min; } int max = prices[prices.length-1]; for (int i=prices.length-1; i>=0; i--) { max = Math.max(max, prices[i]); highest[i] = max; } max = 0; for (int i=0; i<lowest.length; i++) max = Math.max(max, highest[i]-lowest[i]); return max; }}
第一题很简单,时间复杂度为 O(N)
- 【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
- 苹果Xcode 证书生成、设置、应用完整图文教程
- OpenGL ES系列之0 – 建立Xcode项目
- 求两个链表的第一个节点
- Matlab常用命令(二)
- Java 文件合并
- LeetCode - Best Time to Buy and Sell Stock
- 关于qt移植arm的环境变量的一些小理解
- static作用:静态变量的生存周期和作用域
- 关于XML字符串和XML Document之间的转换《转》
- Python核心编程2-11习题——求和以及求平均数
- Log4j的进阶使用-自定义类和+自定义输出日志类型+输出文件
- Android4.2中简单实现checkbox全选、全不选、反选
- 用乌龟(TortoiseSvn) 创建分支(Branch)和标签(Tag)
- 比较字符串的大小