题目:

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.

思路:

  • 题意给定一个数组prices,每一个值prices[i]是第i天的股价,要求只能买入和卖出一次,求出最大的获益
  • 首先买入一定在卖出之前,所以要同步更新最小股价,然后最大利润,用一个变量代替最小的股价,一个值来代替最大利润,注意最大利润是负数的情况
  • -

代码:

public class Solution {
    public int maxProfit(int[] prices) {
        if(prices.length <= 1){
            return 0;
        }
        int minPrice = prices[0];
        int maxProfit = prices[1] - prices[0];
        for(int i = 2; i < prices.length;i++){
            minPrice = Math.min(minPrice,prices[i-1]);
            maxProfit = Math.max(maxProfit,prices[i]-minPrice);
        }
        if(maxProfit < 0){
            return 0;
        }else{
            return maxProfit;
        }
    }
}

LeetCode(42)-Best Time to Buy and Sell Stock(卖股票)的更多相关文章

  1. [LeetCode] 188. Best Time to Buy and Sell Stock IV 买卖股票的最佳时间 IV

    Say you have an array for which the ith element is the price of a given stock on day i. Design an al ...

  2. Java for LeetCode 188 Best Time to Buy and Sell Stock IV【HARD】

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

  4. [LeetCode] 122. Best Time to Buy and Sell Stock II 买卖股票的最佳时间 II

    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] 123. Best Time to Buy and Sell Stock III 买卖股票的最佳时间 III

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

  7. 【LeetCode】Best Time to Buy and Sell Stock IV

    Best Time to Buy and Sell Stock IV Say you have an array for which the ith element is the price of a ...

  8. [Leetcode Week6]Best Time to Buy and Sell Stock

    Best Time to Buy and Sell Stock 题解 原创文章,拒绝转载 题目来源:https://leetcode.com/problems/best-time-to-buy-and ...

  9. [Leetcode][JAVA] Best Time to Buy and Sell Stock I, II, III

    Best Time to Buy and Sell Stock Say you have an array for which the ith element is the price of a gi ...

随机推荐

  1. Storm并发机制详解

    本文可作为 <<Storm-分布式实时计算模式>>一书1.4节的读书笔记 在Storm中,一个task就可以理解为在集群中某个节点上运行的一个spout或者bolt实例. 记住 ...

  2. 自己写一个网页版的Markdown实时编辑器

    这几天忙着使用Python+Django+sqlite 搭建自己的博客系统,但是单纯的使用H5的TextArea,简直太挫了有木有.所以,就想模仿一下人家内嵌到网页上的Markdown编辑器,从而让自 ...

  3. 【移动开发】SparseArray替代HashMap

    SparseArray是android里为<Interger,Object>这样的Hashmap而专门写的class,目的是提高效率,其核心是折半查找函数(binarySearch). p ...

  4. 【编程练习】poj1111

    Image Perimeters Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 8632   Accepted: 5168 ...

  5. ubuntu连接android设备(附最简单方法)

    在ubuntu下连接android设备,虽然不用像windows那样安装驱动,然而却会遇见一个错误:输入adb shell,会提示insufficient permissions for device ...

  6. 09 ExpanableListView 的代码例子

    <span style="font-size:18px;">package com.qf.day09_expandablelistview03; import andr ...

  7. 免安装版本tomcat 指定的服务并未以已安装的服务存在,Unable to open the service

    今天在自己的电脑上安装了Tomcat6.0.14,是在Tomcat主页上直接下载的免安装版.但是把文件解压的之后,双击Tomcat6w.exe时,去出现了"指定的服务并未以已安装的服务存在, ...

  8. cocos2d-js(一)引擎的工作原理和文件的调用顺序

    Cocos2d-js可以实现在网页上运行高性能的2D游戏,实现原理是通过HTML5的canvas标签,该引擎采用Javascript编写,并且有自己的一些语法,因为没有成熟的IDE,一般建立工程是通过 ...

  9. 【OpenCV文档】用于角点检测的Fast算法

    原文地址:http://docs.opencv.org/trunk/doc/py_tutorials/py_feature2d/py_fast/py_fast.html#fast-algorithm- ...

  10. (NO.00003)iOS游戏简单的机器人投射游戏成形记(二十一)

    回到Xcode中,在MainScene.h接口中添加碰撞协议: @interface MainScene : CCNode <CCPhysicsCollisionDelegate> //. ...