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.

题意:给出一个数组,每个元素表示股票在第i天的价钱

思路:用动态规划。用dp(i)表示从第i个到第n天的最大的价钱,状态转移方程为dp(i) = max{dp(i +1),price[i]}

代码如下:

class Solution
{public int maxProfit(int[] prices){int len = prices.length;if (0 == len) return 0;int ans = 0;int max = prices[len - 1];for (int i = len - 1; i >= 0; i--){ans = Math.max(max - prices[i], ans);if (prices[i] > max){max = prices[i];}}return ans;}
}

LeetCode Best Time to Buy and Sell Stock(dp)相关推荐

  1. leetcode: Best Time to Buy and Sell Stock 系列

    leetcode: Best Time to Buy and Sell Stock 系列 一系列包括: - Best Time to Buy and Sell Stock Ⅰ - Best Time ...

  2. Leetcode Best Time to Buy and Sell Stock III

    Leetcode Best Time to Buy and Sell Stock III,本算法的关键为找出其动态子结构.可以发现,序列中的最小值可以做为其的一个分割,令左边序列为left,右边的序列 ...

  3. Leetcode Best Time to Buy and Sell Stock

    Leetcode Best Time to Buy and Sell Stock 相关代码,本题使用dp算法完成,本算应该算得上一个经典的dp算法题. #include <iostream> ...

  4. LeetCode Best Time to Buy and Sell Stock II

    原题链接在这里:https://leetcode.com/problems/best-time-to-buy-and-sell-stock-ii/ 题目: Say you have an array ...

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

  6. [leetcode]_Best Time to Buy and Sell Stock I II

    一个系列三道题,我都不会做,google之答案.过了两道,第三道看不懂,放置,稍后继续. 一.Best Time to Buy and Sell Stock I 题目:一个数组表示一支股票的价格变换. ...

  7. LeetCode Best Time to Buy and Sell Stock with Cooldown(动态规划)

    问题:给定一个整数数组,其中第i个元素表示第i天的股票价格.可以多次买卖股票,卖出股票后,无法在第二天买入股票. 思路:动态规划法,用dp(i,j)表示第i天持有或者不持有股票的最大利润.用dp(i, ...

  8. [leetcode]Best Time to Buy and Sell Stock III

    先说思路.参考了这篇:http://blog.unieagle.net/2012/12/05/leetcode%E9%A2%98%E7%9B%AE%EF%BC%9Abest-time-to-buy-a ...

  9. Leetcode Best Time to Buy and Sell Stock IV(最大子段和)

    朴素的O(n^2)过不了,这里借鉴前人的思想,总结一下. 主要利用栈维护对后面产生影响的字段,然后利用贪心的思想逐步合并. 首先,找到每一对最低点(vx),最高点(px); 其次,合并当前(vx, p ...

最新文章

  1. LeetCode简单题之寻找数组的中心下标
  2. Linux安装telnet
  3. 在SSH整合过程中:org/hibernate/engine/spi/SharedSessionContractImplementor
  4. 修改labelImg软件的yolo标注写入格式(.txt文件不换行的解决办法)(将换行符'\n'替换成'\r\n')
  5. 在asp.net 中应用POST传递和接收XML文件以及参数.
  6. 【资源】首发:徐亦达老师的机器学习课件及下载(中文目录)
  7. 这门国产编程语言,确实很棒!
  8. 强推!盘点阿里巴巴 15 款开发者工具 | 程序员硬核评测
  9. display 隐藏css,CSS-元素的显示与隐藏
  10. 国庆海报没有灵感,给你设计要点素材!
  11. 广义典型相关分析_数学建模/机器学习:广义加性模型(GAM)及其Python实现
  12. SiamFC代码分析(architecture、training、test)
  13. 低仿扫描全能王的选择区域功能
  14. JSP:JavaBean+EL函数+JSTL标签
  15. PSD95抗体研究丨SYSY PSD95抗体结果图展示
  16. 2020牛客暑期多校训练营(第七场)J.Pointer Analysis
  17. python 北上资金_北上资金持股相关因子
  18. Unity 3D模型展示之自由观察模型
  19. 实用的 Python 自动化办公技巧
  20. Python +SQL Server 框架及返回结果处理

热门文章

  1. 计算机科学与技术python方向是什么意思-第一模块·开发基础-第1章 Python基础语法...
  2. python常用命令汇总-python数据分析之pandas常用命令整理
  3. python在审计中的应用-基于python的自动化代码审计
  4. python对象编程例子-python(十二)面向对象编程、类
  5. python官网安装步骤-新手Windows下Python下载安装教程及配置注意事项
  6. python动态图-python之最炫抖音动态图
  7. python怎么导入文件-Python文件如何引入?详解引入Python文件步骤
  8. 西安python工资怎么样-西安Python和人工智能的薪资前景到底怎么样?
  9. 用python绘制柱状图标题-如何用Python绘制3D柱形图
  10. 上海python培训班-上海哪家python培训班比较靠谱?