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.

Solution 1:

min记录最小买入价

maxProfit记录最大利润

遍历array,不断更新最小买入价,计算更新最大利润

 public class Solution {
public int maxProfit(int[] prices) {
if (prices == null) {
return 0;
} int maxProfit = 0;
int minValue = Integer.MAX_VALUE; for (int i: prices) {
minValue = Math.min(minValue, i);
maxProfit = Math.max(maxProfit, i - minValue);
} return maxProfit;
}
}

GitHub代码链接

LeetCode: Best Time to Buy and Sell Stock 解题报告的更多相关文章

  1. 【LeetCode】121. Best Time to Buy and Sell Stock 解题报告(Java & Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 Java解法 Python解法 C++ 解法 日期 ...

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

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

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

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

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

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

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

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

随机推荐

  1. 【总结】java regex 正则表达式 提取数字和去除数字,过滤数字,提取价格

    @Test public void test33() { String phoneString = "哈哈,13888889999"; // 提取数字 Pattern patter ...

  2. 数据结构学习之stack

    不能小看这些基本的数据结构,写了才发现还是会有问题出现的. 有码有真相: #pragma once class MyStack { public: MyStack(void); ~MyStack(vo ...

  3. memcached内存管理机制[未整理]

    memcached默认采用的是Slab Allocator的机制分配管理内存的,在此之前,内存的分配是通过对所有的记录简单地进行malloc和free来进行的,但这种方式容易造成很多内存碎片,加重操作 ...

  4. 在家赚钱,威客网站的使用方法 CSDN项目频道、SXSOFT、任务中国、猪八戒四个网站的线上交易 三种交易模式(1)悬赏模式(2)招标模式(3)直接交易模式

    在家赚钱,威客网站的使用方法 很显然,<让猪八戒飞一会儿>作者对威客这一行业不熟悉,<让猪八戒飞一会儿>文章中错误有一些,不一一指出.我在CSDN项目频道.SXSOFT.任务中 ...

  5. 在Hadoop1.2.1上运行第一个Hadoop程序FileSystemCat

  6. openstack rpc机制

    一.概述: 在openstack项目中,api的调用规则: 跨项目:如nova调用keystone, glance,cinder等,使用rest api(通过相应的python-XXXclient 库 ...

  7. 【Spring】spring的7个模块

    Spring 是一个开源框架,是为了解决企业应用程序开发复杂性而创建的.框架的主要优势之一就是其分层架构,分层架构允许您选择使用哪一个组件,同时为 J2EE 应用程序开发提供集成的框架. Spring ...

  8. Swift 可选型

    1.可选型 Swift 语言为我们提供了一种全新的.更加安全的类型 "可选型".可选型是使用范型枚举的形式来组织的,也就是说此特性可以运用于所有的类型.结构体.类或者其他复杂数据类 ...

  9. CocoaPods iOS 开源库管理

    1.CocoaPods 简介 1.1 CocoaPods CocoaPods 是 macOS 和 iOS 下的一个第三类库管理工具,通过 CocoaPods 工具我们可以为项目添加被称为 " ...

  10. Windows键盘消息处理

    原文链接: http://blog.sina.com.cn/s/blog_5f8817250100taab.html 本文大部分来自MSDN和网友的博客,我在实践的基础上再作了一些总结. 1,虚拟键( ...