/**
* 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的更多相关文章

  1. 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 ...

  2. [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 ...

  3. [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 ...

  4. [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 ...

  5. [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 ...

  6. [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 ...

  7. 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 ...

  8. 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 ...

  9. [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 ...

  10. 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 ...

随机推荐

  1. org.apache.http.client.ClientProtocolException: URI does not specify a valid host name

    问题截图: 原因:http:// 少了两个//

  2. vue-cli利用router创建单页面

    一.创建脚手架 第一步 打开命令输入vue init webpack-simple vue-name 下载webpack-simple  (vue-name是你要创建的项目名称) 第二步 输入cd v ...

  3. 关于VB里判断逻辑的说明

    如上图,当进行连续判断的时候,即使第一个已经不符合条件了,后面的依然会计算.这点一定要记住,除非你所有的函数都有必要执行,否则会导致效率降低. 减代码不一定能提高效率,对于IIF和连续判断写法,貌似很 ...

  4. Spark内部执行机制

    Spark内部执行机制 1.1 内部执行流程 如下图1为分布式集群上spark应用程序的一般执行框架.主要由sparkcontext(spark上下文).cluster manager(资源管理器)和 ...

  5. pywin32模块安装

    安装流程: 1.查看python版本和位数: 2.下载对应的的pywin32,下载目录任意 https://sourceforge.net/projects/pywin32/files%2Fpywin ...

  6. redux 与 react-redux

    Redux 一.Redux 三大原则: 1.一个应用永远只有一个数据源(整个应用状态都保存在一个对象中,Redux提供的工具函数combineReducers可以解决庞大的数据对象的问题) 2.状态是 ...

  7. dedecms 后台 菜单点击后打开的慢

    原因之一: 加载后台信息的时候  耗费的时间太长. 如果不关注这边的数据, 可以将他们删除掉. 删除  “信息统计”   : 找到   www.yoursite.com/dede/js/indexbo ...

  8. 快速搭建react项目骨架(按需加载、redux、axios、项目级目录等等)

    一.前言 最近整理了一下项目骨架,顺便自定义了一个脚手架,方便日后使用.我会从头开始,步骤一步步写明白,如果还有不清楚的可以评论区留言.先大致介绍一下这个骨架,我们采用 create-react-ap ...

  9. vue — 安装并创建vue项目

    1.先从node.js官网(https://nodejs.org/en/download/) 下载并安装node,然后通过在命令行输入node -v命令,查看node的版本,要是出现相应的版本号就证明 ...

  10. CSS引用方式及样式层叠机制

    CSS引用方式有3种,三种分别为:外部引入.内部引入.行内样式,下面一 一进行介绍. 1.外部引入:CSS代码在一个独立的文件中,HTML通过Link标签引入到页面. 代码格式:<link re ...