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 ...
随机推荐
- 图论之最短路径floyd算法
Floyd算法是图论中经典的多源最短路径算法,即求任意两点之间的最短路径. 它可采用动态规划思想,因为它满足最优子结构性质,即最短路径序列的子序列也是最短路径. 举例说明最优子结构性质,上图中1号到5 ...
- [SCOI2015]国旗计划
Description: A 国正在开展一项伟大的计划 -- 国旗计划.这项计划的内容是边防战士手举国旗环绕边境线奔袭一圈.这项计划需要多名边防战士以接力的形式共同完成,为此,国土安全局已经挑选了 \ ...
- Linux 查询服务数据
1. htop 可以时时查看 2. free -m 查看缓存
- python利用xlrd读取excel文件始终报错原因
1.代码按照网上百度的格式进行书写如下: 但运行后,始终报错如下: 百度了xlrd网页: 分明支持xls和xlsx两种格式的文件,但运行始终报错. 最后找到原因是因为我所读取的文件虽然是以.xls命名 ...
- iphone 屏蔽系统自动更新,消除设置上的小红点
苹果ios系统的更新频率大家应该都知道,一般来说1个月就会来次更新.这一点让很多人讨厌.主要原因还是iPhone会自动下载更新包,然后一直不停地提示你是否安装更新,问题是我们还找不到关闭提醒和关闭自动 ...
- Centos服务器上NFS灾备环境及KVM的搭建及使用
1.概述 由于在单台服务器上搭建灾备环境需要KVM和NFS的支持,下面先列出KVM的搭建流程,再列出使用NFS实现单台服务器灾备的流程. A.搭建KVM环境 1>.主机环境准备 Linux Sy ...
- js 阻止事件执行
三种阻止事件执行的方式 event.preventDefault() event.stopPropagation() return false event.preventDefault() 阻止特定事 ...
- oracle11G 用户密码180天修改概要文件过程
oracle11G 用户密码180天修改概要文件过程 原因 创建用户的时候不指定概要文件的默认的概要文件是default, 而默认的概要文件中的设置如下,注意斜体部分 PROFILE RESOURCE ...
- 通过net time同步电脑时间
net use \\192.168.1.112\ipc$ admin /user:admin #第一个admin是密码,第二个admin是用户名: net time \\192.168.1.112 / ...
- 常见的java设计模式
单例模式 简单点说,就是一个应用程序中,某个类的实例对象只有一个,你没有办法去new,因为构造器是被private修饰的,一般通过getInstance()的方法来获取它们的实例. getInstan ...