题目

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.

又是时间复杂度,离真相就差一行代码。。。还是没想出来,看了discuss才知道答案。

代码

public class Solution {public int maxProfit(int[] prices) {if (prices.length<=0 || prices.length==1) return 0;int maxProfit=0;int minTemp=prices[0];for(int i=1;i<prices.length;i++){if(prices[i]<minTemp){minTemp=prices[i];              }else{if(prices[i]-minTemp>maxProfit){maxProfit=prices[i]-minTemp;}}}return maxProfit;     }
}
代码下载:https://github.com/jimenbian/GarvinLeetCode

/********************************

* 本文来自博客  “李博Garvin“

* 转载请标明出处:http://blog.csdn.net/buptgshengod

******************************************/

【LeetCode从零单排】No121 Best Time to Buy and Sell Stock相关推荐

  1. 【LeetCode】解题309:Best Time to Buy and Sell Stock with Cooldown(动态规划)

    LeetCode解题 309:Best Time to Buy and Sell Stock with Cooldown (动态规划) Problem 309: Best Time to Buy an ...

  2. leetcode python3 简单题122. Best Time to Buy and Sell Stock II

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

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

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

  4. LeetCode之路:122. Best Time to Buy and Sell Stock II

    一.引言 这是一道非常有趣的题目! 这是一道非常有趣的题目!! 这是一道非常有趣的题目!!! 重要的事情先说三遍 : ) 好了,接下来让我们看看这道题: Say you have an array f ...

  5. leetcode解题报告:188 Best Time to Buy and Sell Stock IV

    问题: 给定一个列表,第i个元素代表股票第i天的价值,最多只允许买入卖出k次,求最大收益 思路:动态规划 输入为列表p1p2...pm 代码:Python 转载于:https://blog.51cto ...

  6. LeetCode 309. Best Time to Buy and Sell Stock with Cooldown--Java解法-卖股票系列题目

    此文首发于我的Jekyll博客:zhang0peter的个人博客 LeetCode题解文章分类:LeetCode题解文章集合 LeetCode 所有题目总结:LeetCode 所有题目总结 题目地址: ...

  7. LeetCode 123. Best Time to Buy and Sell Stock III--Python解法--动态规划--数学题

    此文首发于我的个人博客:zhang0peter的个人博客 LeetCode题解文章分类:LeetCode题解文章集合 LeetCode 所有题目总结:LeetCode 所有题目总结 题目地址:Best ...

  8. LeetCode 122. Best Time to Buy and Sell Stock II--贪心--Java,C++,Python解法

    题目地址:Best Time to Buy and Sell Stock II - LeetCode Say you have an array for which the ith element i ...

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

最新文章

  1. noj数据结构稀疏矩阵的加法十字链表_数据结构学习(C )稀疏矩阵(十字链表1)
  2. 实验一HSRP与STP结合
  3. 如何使用Topshelf管理Windows服务
  4. 给插店一个成功案例给自己一次机会
  5. linux 下启动jar小程序
  6. 天池 在线编程 高效作业处理服务(01背包DP)
  7. 堆栈 cookie 检测代码检测到基于堆栈的缓冲区溢出_漏洞公告 | 华硕(ASUS)家庭无线路由器远程代码执行0day...
  8. jquery autocomplete的使用
  9. 【英语学习】【WOTD】teetotaler 释义/词源/示例
  10. linux安装typecho教程,Typecho上手指南
  11. 程序员的代码行数越少越好?
  12. OpenSSL 1.1.1 新特性: 全面支持国密SM2/SM3/SM4加密算法
  13. 给ubuntu设置静态ip —— How to set static IP Address in Ubuntu Server 16.04
  14. SecureCRT Ver 8.1.4 整合汉化绿色版一体包
  15. linux socket cups,Linux打印系统CUPS原理分析
  16. 《BBC 跟拍 49 年:穷人与富人的人生七年》
  17. jQuery(JavaScript类库)
  18. Pandownload 开发者被抓了,但是其背后,百度难道不需要反思吗?
  19. php css下划线,CSS文字下划线的设置方法介绍
  20. Python进行零售商品数据分析

热门文章

  1. java遍历集合选择题_Java集合知识测试B
  2. java 时间戳验证_Java中带有时间戳的数字签名
  3. textview 背景变形_重庆新中式床背景品牌
  4. 文本编辑器实现跳转到指定行的功能
  5. 文本编辑器第一阶段测试
  6. 舞蹈里需要用计算机的地方,【舞蹈教学论文】计算机多媒体技术在舞蹈教学中的应用(共2080字)...
  7. python编程快速上手-----让繁琐工作自动化_每周一书《Python编程快速上手 让繁琐工作自动化》分享!...
  8. 计划完成提醒系统C语言,通信录管理计划系统C语言知识程序设计.doc
  9. php foreach 修改数组,php如何使用foreach修改数组
  10. 打开了悬浮窗权限但是没有_给你的手机添加“樱花雨”特效,打开手机樱花就满屏飘落,漂亮!...