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 ...
随机推荐
- CI 框架 隐藏index.php 入口文件 和 设置访问application下子目录
1.隐藏根目录下 index.php, 在根目录下创建 .htaccess文件 内容如下: <IfModule mod_rewrite.c> RewriteEngine on Rewrit ...
- 【面试题】Java实现String的IndexOf方法
先说题后感:程序员这一行,很多时候,自驱学习能力是自我成长一个很重要的因素(当然技术最好的学习途径都是通过项目实践去学习.理解.掌握).而自学方法中,除了看官方文档.技术博客等途径之外,学习源码也是一 ...
- vscode设置中文语言
https://jingyan.baidu.com/article/7e44095377c9d12fc1e2ef5b.html
- jquery获取一组文本框的值
$("#btn5").click(function() { var str="";$("[name='checkbox'][checked]& ...
- 初入linux系统
作为微软的老铁粉了,看到微软进军linux这么久了,是时候该跟上脚本了,不然该落后了,脚步是如此之快,着实让我吃了一惊,说干就干, 绝不是开玩笑的,谁也阻止不了.net开源,跨平台的脚步了.以前别人说 ...
- golang二进制bit位的常用操作
golang作为一热门的兼顾性能 效率的热门语言,相信很多人都知道,在编程语言排行榜上一直都是很亮眼,作为一门强类型语言,二进制位的操作肯定是避免不了的,数据的最小的单位也就是位,尤其是网络中封包.拆 ...
- 把一下程序中的print()函数改写成
源代码: #include <iostream> using namespace std; void print( int w ) { ; i <= w ; i++ ) { ; j ...
- 将博客搬至CSDN http://blog.csdn.net/yi_xianyong
将博客搬至CSDN http://blog.csdn.net/yi_xianyong
- 1 eclipse 离线安装activiti插件
第一步:下载需要的离线activiti文件: 链接:https://pan.baidu.com/s/1-_XjIsuZfhiEZn6iLul6-Q 密码:mfyk (这是其他网友的链接) 第二步: ...
- js中的单例模式
1.场景:当我们需要多人合作完成一个项目,但是有一些操作是同样的操作时(例如:点击按钮显示加载的遮罩层:例如:提交表单时的验证都是一样的),这个时候我们就需要单例模式: 2.什么是单例模式:是一种常见 ...