leetcode — best-time-to-buy-and-sell-stock
/**
* Source : https://oj.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 class BestTimeToBuyAndSellStock {
/**
* 给定一段时间范围内股票的价格判断什么时候买进和卖出所得的利润最大,最大是多少
*
* 只需要找出最低价格和最高价就可以,但是要注意,买进肯定是在卖出之前,所以最小值要在最大值前面
*
* 遍历数组,找出i之前的最小值,将当前元素prices[i]与minimum比较
* prices[i] < minimum 更新minimum,minimum = prices[i]
* prices[i] >= minimum 计算当前的最大值利润
*
*
* @param prices
* @return
*/
public int maxProfit (int[] prices) {
if (prices.length <= 1) {
return 0;
}
int min = prices[0];
int maxProfit = Integer.MIN_VALUE;
for (int i = 1; i < prices.length; i ++) {
if (prices[i] < min) {
min = prices[i];
} else {
int profit = prices[i] - min;
maxProfit = profit > maxProfit ? profit : maxProfit;
}
}
return maxProfit;
}
public static void main(String[] args) {
BestTimeToBuyAndSellStock bestTimeToBuyAndSellStock = new BestTimeToBuyAndSellStock();
System.out.println(bestTimeToBuyAndSellStock.maxProfit(new int[]{1,2,3,4,5}));
System.out.println(bestTimeToBuyAndSellStock.maxProfit(new int[]{1,-2,3,5,-9}));
}
}
leetcode — best-time-to-buy-and-sell-stock的更多相关文章
- LeetCode:Best Time to Buy and Sell Stock I II III
LeetCode:Best Time to Buy and Sell Stock Say you have an array for which the ith element is the pric ...
- [LeetCode] Best Time to Buy and Sell Stock with Cooldown 买股票的最佳时间含冷冻期
Say you have an array for which the ith element is the price of a given stock on day i. Design an al ...
- [LeetCode] Best Time to Buy and Sell Stock IV 买卖股票的最佳时间之四
Say you have an array for which the ith element is the price of a given stock on day i. Design an al ...
- [LeetCode] Best Time to Buy and Sell Stock III 买股票的最佳时间之三
Say you have an array for which the ith element is the price of a given stock on day i. Design an al ...
- [LeetCode] Best Time to Buy and Sell Stock II 买股票的最佳时间之二
Say you have an array for which the ith element is the price of a given stock on day i. Design an al ...
- [LeetCode] 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 ...
- LEETCODE —— Best Time to Buy and Sell Stock II [贪心算法]
Best Time to Buy and Sell Stock II Say you have an array for which the ith element is the price of a ...
- LeetCode Best Time to Buy and Sell Stock IV
原题链接在这里:https://leetcode.com/problems/best-time-to-buy-and-sell-stock-iv/ 题目: Say you have an array ...
- [LeetCode] Best Time to Buy and Sell Stock with Transaction Fee 买股票的最佳时间含交易费
Your are given an array of integers prices, for which the i-th element is the price of a given stock ...
- Algorithm - 贪心算法使用场景 ( LEETCODE —— Best Time to Buy and Sell Stock II)
先看一道leetcode题: Best Time to Buy and Sell Stock II Say you have an array for which the ith element is ...
随机推荐
- Html5+CSS3之音视频播放
一.视频播放控制 1.Html5支持的视频格式有.webm,.ogg,在Html5中播放视频所用的标签为<video controls="controls" src=&quo ...
- 05解决flask循环引用的问题
1, 什么是循环引用问题?为什么会导致循环引用? 1.1先讲是什么? 主文件中class类过多会导致主文件冗余,如下图,所以我们单独给class类一个文件,然后再引用它. 1.2再讲为什么? 主文件为 ...
- PHP调用微博接口实现微博登录的方法示例
在平时项目开发过程中,除了注册本网站账号进行登录之外,还可以调用第三方接口进行登录网站.这里以微博登录为例.微博登录包括身份认证.用户关系以及内容传播.允许用户使用微博帐号登录访问第三方网站,分享内容 ...
- fiddler 应用
一 pc 端抓取 例:本地调试代码,转换域名,请求网络数据 1:设置代理,以smart header 为例 ip为 127.0.0.1 端口与自己的fillder一致,注意将不代理的地址列表做修改 ...
- md5 加密文件
import hashlibimport os def get_md5(file_path): md5 = None if os.path.isfile(file_path): f = open(fi ...
- 马昕璐 201771010118《面向对象程序设计(java)》第十八周学习总结
实验十八 总复习 实验时间 2018-12-30 1.实验目的与要求 (1) 综合掌握java基本程序结构: (2) 综合掌握java面向对象程序设计特点: (3) 综合掌握java GUI 程序设 ...
- session源码剖析
session机制采用的是一种在客户端与服务端之间保持状态的解决方案,由于采用服务器端保持状态的方案在客户端也要保存标识,session机制也要借助于cookie机制达到目的.session保存了客户 ...
- Centos7 编译测试工具 wrk bombardier iftop
1.wrk 安装及使用----------------------------------------------------------------------------------------- ...
- 正则表达示 for Python3
前情提要 从大量的文字内容中找到自己想要的东西,正则似乎是最好的方法.也是写爬虫不可缺少的技能.所以,别墨迹了赶紧好好学吧! 教程来自http://www.runoob.com/python3/pyt ...
- iOS浏览器 new Date() 返回 NaN
问题 项目中某个地方用到了倒计时,因此打算通过 new Date() 函数实现.但在 iPhone 真机测试的时候,显示的结果不符合预期.通过调试发现 iOS 中 new Date('2017-01- ...