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 ...
随机推荐
- SpirentTestcenter测试仪的自动化
SpirentTestcenter,美国思博伦公司的网络测试仪表,覆盖以太网L2~L7层,使用过的仪表中功能最强大的. 1.SpirentTestcenter的自动化测试场景 测试PC上的AT框架-- ...
- Gson---简单入门
1-1.Diaosi.java(bean) package Bean; import com.google.gson.annotations.SerializedName; public class ...
- css小知识之伪元素
<!DOCTYPE html><html><head lang="en"> <meta charset="UTF-8" ...
- 优化 从Draw Calls到GC
原文出处: 慕容小匹夫的博客(@慕容小匹夫) 欢迎分享原创到伯乐头条 前言: 刚开始写这篇文章的时候选了一个很土的题目...<Unity3D优化全解析>.因为这是一篇临时起意才写的文章 ...
- [转]Redmine 配置163邮箱
redmine的邮件发送功能还是很有用的.像项目有更新啦,任务分配啦,都能邮件发送的相关责任人.我自己在linux服务器上安装并启动了redmine后,邮件一直发送了不了.查了网上的资料,都是讲修改下 ...
- NET Core1.0之CentOS平台开发控制台程序DEMO
微软发布NET Core1.0正式版本,并支持了red hat linux平台,所以在CentOS平台,通过编辑器玩下控制器程序. 一.安装.NET Core SDK 先下载SDK并放在指定目录. s ...
- C6000代码层面优化(一)
2014年8月7日,看了一片很长见识的博文,关于DSP如何优化的,有一个问题没有搞通,“百度”一下关键字,居然搜查了一模一样的博文N片,现在也搞不懂这篇博文的原创作者是谁了.反正我感觉直接转摘过去,要 ...
- [delphi]运行cmd命令,并取得输出字符
http://blog.csdn.net/nerdy/article/details/8969189 [delphi]运行cmd命令,并取得输出字符 标签: delphiCMD命令 2013-05- ...
- information_schema.TABLES
获取所有表结构(TABLES) SELECT * FROM information_schema.TABLES WHERE TABLE_SCHEMA='数据库名'; TABLES表:提供了关于数据库中 ...
- crontab 系列
crontab是一个很方便的在unix/linux系统上定时(循环)执行某个任务的程序使用cron服务,用 service crond status 查看 cron服务状态,如果没有启动则 servi ...