题目:

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.

思路:

1.目标是求数组中的某两个值的差(最大差值),前提是大的数在小的数后面

2.循环遍历数组,设置一个当前最小值min(初始为Integer.MAX_VALUE)和一个最大差值max(初始化为0)

每次更新min,并更新max(当前元素值-min)

public class Solution {public int maxProfit(int[] prices) {int len=prices.length;     //数组长度int max=0;                    //最大差值int min=Integer.MAX_VALUE;              //当前最小值for(int i=0;i<len;i++) {if(prices[i]<min)min=prices[i];if(prices[i]-min>max)max=prices[i]-min;}return max;}
}

  

转载于:https://www.cnblogs.com/hwu2014/p/4422009.html

121-Best Time to Buy and Sell Stock相关推荐

  1. 【贪心 和 DP + 卖股票】LeetCode 121. Best Time to Buy and Sell Stock

    LeetCode 121. Best Time to Buy and Sell Stock Solution1:我的答案 动态规划和贪心不要区分的那么明显嘛~~~ class Solution { p ...

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

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

  4. leetcode python3 简单题121. Best Time to Buy and Sell Stock

    1.编辑器 我使用的是win10+vscode+leetcode+python3 环境配置参见我的博客: 链接 2.第一百二十一题 (1)题目 英文: Say you have an array fo ...

  5. Leetcode——121. Best Time to Buy and Sell Stock

    题目原址 https://leetcode.com/problems/best-time-to-buy-and-sell-stock/description/ 题目描述 Say you have an ...

  6. LeetCode 121 Best Time to Buy and Sell Stock(股票买入卖出的最佳时间)

    翻译 话说你有一个数组,其中第i个元素表示在第i天的股票价格.如果你被只被允许最多一次交易(例如,买入然后卖出一个股票),设计一个算法并找出最大利润. 原文 Say you have an array ...

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

  8. 121. Best Time to Buy and Sell Stock买卖股票的最佳时机

    You are given an array prices where prices[i] is the price of a given stock on the ith day. 给定一个数组 p ...

  9. 121 Best Time to Buy and Sell Stock

    输入:一个数组prices,prices[i]表示第i天股票的价格. 输出:买卖股票的最大收益. 规则:只允许最多买一次,最多卖一次股票.如果觉得价格不合适,可以不买卖. 分析1:最先想到的是暴力搜索 ...

  10. 121. Best Time to Buy and Sell Stock

    一.题目 1.审题 2.分析 给出一个数组,让你先已 val1 买入,再 以 val2 卖出,求获得的利润最大是多少. 二.解答 1.思路: 方法一. 用遍历 min 记录到当前位置的最小值: max ...

最新文章

  1. 大数据处理时用到maven的repository
  2. python 后台服务
  3. MySQL数据库引擎详解
  4. Andrew Ng机器学习编程作业:K-means Clustering and Principal Component Analysis
  5. 安装cx_Oracle 遇到的杂项问题
  6. 江西 高职 分数 计算机,快讯!江西高职(专科)批次各高校投档分数线出炉…...
  7. centos报acpi 错误解决方法实测有用
  8. JavaScript高级程序设计学习总结一
  9. 【数学建模】2 TOPSIS优劣解距离法
  10. 约翰诺依曼在1940年发明了计算机英语,冯诺依曼元胞自动机
  11. 计算机键盘fn,笔记本键盘fn键有什么用 笔记本键盘fn键使用说明大全
  12. 使用UDP遇到的问题小结
  13. TiDB 在中国电信翼支付的大规模深度实践
  14. 禅道api调用(爬虫方式)
  15. itext生成pdf文档时给文档添加背景图片
  16. 华为畅享10s值得买吗_华为畅享10s多少钱?值得入手么?
  17. 清华大学公开课线性代数2——第8讲:图和网络
  18. 前端点滴(JS进阶)(三)---- JavaScript 两链一包
  19. Apache CarbonData快速入门指南
  20. 销售订单发票计划导入

热门文章

  1. π是无理数证明定积分_证明圆周率是无理数很容易?人类花了2000年!
  2. 2018会考计算机成绩查询入口,2018年山东会考成绩查询时间及入口
  3. linux按文件名排序ls,linux – 如何使用shell脚本按名称对文件进行排序
  4. Java gdal .mif/.mid文件读取
  5. C语言:随笔8--结构体
  6. 首款微控制器级树莓派 Pico,超廉价只需4美元
  7. HDU 2586 How far away ? LCA ---tanjar+并查集 离线算法
  8. java远程方法调用(rmi)--好_RMI-Java远程方法调用的实现(二)
  9. 【实现js和css互通、共享常量参数值】js如何获取CSS/SCSS/LESS的常量、CSS/SCSS/LESS又是如何获取js的值(或者说js是如何主动推送参数给CSS使用的)?
  10. CDN和Web Cache领域相关的经典书籍推荐