121. Best Time to Buy and Sell Stock (一) 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 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.
Example 1:
Input: [7, 1, 5, 3, 6, 4]
Output: 5 max. difference = 6-1 = 5 (not 7-1 = 6, as selling price needs to be larger than buying price)
Example 2:
Input: [7, 6, 4, 3, 1]
Output: 0 In this case, no transaction is done, i.e. max profit = 0.
仿佛是怕我们看不懂么 特意给出了两个例子 其实这个题目的意思是给你一个数组 数组的第i个元素意味着商品在第i天的价格 如何选择最佳时间买入和售出商品获得最大利润(只允许一次交易)
那无疑是尽量在最低的时候买入 最高的时候卖出 同时买入时间肯定要先于卖出时间 感觉一次遍历够用了 只需要维护一个最小值即可
思路 从第一个元素开始遍历 同时维护一个已遍历元素中的最小值 和最大收益值 便利完返回收益即可
有了思路 接下里就是码代码了 这里养成一个习惯吧 尽量在文本编辑器 或者直接在leetcode的代码输入框写代码 不过分依赖工具 除非找不出问题再用工具调试
public class Solution {
public int maxProfit(int[] prices) {
int min=Integer.MAX_VALUE;
int profit=0;
for(int i=0;i<prices.length-1;i++){
min=prices[i]<min?prices[i]:min;
profit=(prices[i+1]-min)>profit?prices[i+1]-min:profit;
}
return profit;
}
}
依旧是一次过。

惯例 看看详情

还可以 2ms o(1)的空间复杂度 o(n)的时间复杂度 个人认为没有最优解了 时间比我短的可能是测试用例不一样这里就不去看别人的discuss了 没必要 转战下一题
121. Best Time to Buy and Sell Stock (一) leetcode解题笔记的更多相关文章
- 188. Best Time to Buy and Sell Stock IV leetcode解题笔记
Say you have an array for which the ith element is the price of a given stock on day i. Design an al ...
- 30. leetcode 121. Best Time to Buy and Sell Stock
121. Best Time to Buy and Sell Stock Say you have an array for which the ith element is the price of ...
- leetcode 121. Best Time to Buy and Sell Stock 、122.Best Time to Buy and Sell Stock II 、309. Best Time to Buy and Sell Stock with Cooldown
121. Best Time to Buy and Sell Stock 题目的要求是只买卖一次,买的价格越低,卖的价格越高,肯定收益就越大 遍历整个数组,维护一个当前位置之前最低的买入价格,然后每次 ...
- 121. Best Time to Buy and Sell Stock【easy】
121. Best Time to Buy and Sell Stock[easy] Say you have an array for which the ith element is the pr ...
- 121. Best Time to Buy and Sell Stock@python
Say you have an array for which the ith element is the price of a given stock on day i. If you were ...
- [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 ...
- 【刷题-LeetCode】121 Best Time to Buy and Sell Stock
Best Time to Buy and Sell Stock Say you have an array for which the ith element is the price of a gi ...
- 123. Best Time to Buy and Sell Stock III ——LeetCode
Say you have an array for which the ith element is the price of a given stock on day i. Design an al ...
- 【LeetCode】309. Best Time to Buy and Sell Stock with Cooldown 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 动态规划 日期 题目地址:https://leetc ...
随机推荐
- 向IPython Notebook中导入.py文件
IPython Notebook使用起来简洁方便,但是有时候如果需要导入一个现有的.py文件,则需要注意选择导入的方法以达到不同的效果.目前遇到3种方法. (1) 将文件保存为.ipynb格式,直接拖 ...
- nice-validator验证插件
主要是作为form表单的验证,密码,确认密码的验证,远程验证的功能: 1:先导包:nice-validator 2:引入文件css,js 3: 使用 使用文档:http://www.niceue.co ...
- $(document).ready()方法和window.onload区别
事件: javascript 和 HTML之间的交互式通过用户和浏览器操作页面时引发的事件来处理的.当文档或者它的某些元素发生某些变化和操作时,浏览器会自动生成一个事件:例如:当用户单击某个按钮时,也 ...
- linux Makefile obj-m obj-y
目标定义是Kbuild Makefile的主要部分,也是核心部分.主要是定义了要编 译的文件,所有的选项,以及到哪些子目录去执行递归操作. 最简单的Kbuild makefile 只包含一行: 例子: ...
- 传输层(2)-TCP连接的建立和终止、TIME_WAIT状态
1.TCP连接的建立和终止 1)三路握手 客户端发送一个SYN(同步)分解,告诉服务器客户将在连接中发送的数据的初始序列号. 服务器发送确认客户的SYN(ACK),同时自己也得发送一个SYN分节,它含 ...
- Sql Server 2012 Enterprise Edition 企业版 迅雷 下载地址
Sql Server 2012 Enterprise Edition 企业版 迅雷 下载地址 版本号 cn_sql_server_2012_enterprise_edition_x86_x64_dvd ...
- 在 .NET 4.0 中使用 .NET 4.5 中新增的特性(CallerMemberNameAttribute/CallerFilePathAttribute/CallerLineNumberAttribute)
介绍 标题中所说的三个特性 CallerMemberNameAttribute / CallerFilePathAttribute / CallerLineNumberAttribute 我们统称为调 ...
- 使用JSONObject遇到的问题,java.lang.NoClassDefFoundError: net/sf/json/JSONObject
先是报 java.lang.NoClassDefFoundError: net/sf/json/JSONObject 这个错误, 打开项目属性找到java build path中的libaries,找 ...
- Android_Activity生命周期
通过前面一段时间的学习,我们很清楚我们的一系列操作都离不开的一个东西,就是我们的activity .接下来我们对 Activity 进行系统的总结. Activity 的四种基本状态 1.运行态(Ru ...
- java io流之BufferReader&BufferedWriter
BufferedReader 由Reader类扩展而来,提供通用的缓冲方式文本读取,而且提供了很实用的readLine,读取一个文本行,从字符输入流中读取文本,缓冲各个字符,从而提供字符.数组和行的高 ...