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 ...
随机推荐
- Git提交代码(要有GitHub账号)
分享一下Git提交模式代码(只是提交到GitHub仓库而已,没有其他的操作) 这个的前提是你已经安装了Node.js.Git 下面来看: 1. cd进入目录 2. 把当前目录变成git可以管理的仓 ...
- 01安装Python虚拟环境
链接 一,为什么安装虚拟环境? 如果你有0.1版本的Flask已经完成项目,此时Flask1.1版本出来的时候就会对0.1有影响,我们通过虚拟环境可以做到互补影响. 二,安装方法 cmd--pip-- ...
- Java的四种引用——强引用、软引用、弱引用、虚引用
目录 强引用 软引用 弱引用 虚引用 强引用 拥有强引用的对象永远不会被GC,可以根据引用的get方法获取到被引用对象 软引用 在内存充足的额时候,拥有软引用的对象不会被GC:即将内存溢出的时候,会对 ...
- 原生JS制作简易Tabs组件
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- SpringBoot加Poi仿照EasyPoi实现Excel导出
POI提供API给Java程序对Microsoft Office格式档案读和写的功能,详细功能可以直接查阅API,因为使用EasyPoi过程中总是缺少依赖,没有搞明白到底是什么坑,索性自己写一个简单工 ...
- CentOS7 VMware-Tools安装与共享文件夹设置
一. VMware-Tools安装 1.加载VMware Tools的光驱:点击"虚拟机"->"安装VMware Tools".这里,由于我已经安装了,所 ...
- vue-cli利用router创建单页面
一.创建脚手架 第一步 打开命令输入vue init webpack-simple vue-name 下载webpack-simple (vue-name是你要创建的项目名称) 第二步 输入cd v ...
- 快速幂 ,快速幂优化,矩形快速幂(java)
快速幂形式 public static int f(int a,int b,int c){ int ans =1; int base=a; while(b!=0){ if((b&1)!=0) ...
- rest_framework之认证源码剖析
如果我们写API有人能访问,有人不能访问,则需要些认证. 如何知道该用户是否已登入? 如果用户登入成功,则给用户一个随机字符串,去访问另一个页面. 以前写session的时候,都是把session写c ...
- Git Bash for Windows add ssh key时报Could not open a connection to your authentication agent.
$ ssh-add id_rsa_bitbucketCould not open a connection to your authentication agent. 运行: $ ssh-agent ...