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解题笔记的更多相关文章

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

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

  3. 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 题目的要求是只买卖一次,买的价格越低,卖的价格越高,肯定收益就越大 遍历整个数组,维护一个当前位置之前最低的买入价格,然后每次 ...

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

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

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

  7. 【刷题-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 ...

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

  9. 【LeetCode】309. Best Time to Buy and Sell Stock with Cooldown 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 动态规划 日期 题目地址:https://leetc ...

随机推荐

  1. EBS中启用OAF页面个性化三个配置

    启用OAF页面个性化三个配置(Profiles) FND:诊断英文为FND: Diagnostics,用于设置是否显示“关于此页” 个性化自助定义英文为Personalize Self-Service ...

  2. UNDO内存结构剖析

    UNDO内存结构剖析 一.场景 Oracle的 C事物从早上9:00开始读取A表全部10w行数据,这个而读取需要经历5分钟.在9:01的时候,B事物将A表删除100条记录,那么,当9:05的时候,事物 ...

  3. php实现只保留mysql中最新1000条记录

    这篇文章主要介绍了php实现只保留mysql中最新1000条记录的方法和相关示例及数据库结构,十分的全面,有需要的小伙伴可以参考下. ? 1 2 3 4 5 6 7 8 9 10 11 12 13 1 ...

  4. iPhone手机安全指南

    摘要:iPhone手机安全指南 - 1.iPhone解锁使用指纹:2.启用“查找我的iPhone”功能:3.Apple ID启用两步验证:4.修改SIM卡PIN码.5.iPhone被盗或丢失后,登录i ...

  5. sublime text 3 安装 pakcage control

        安装sublime的pakcage control 参考:  https://packagecontrol.io/installation#st3 ctrl+`   import urllib ...

  6. 在做基于LBS应用的一些随笔

    公司做了一个基于LBS的APP,在做服务端的时候出现了一些注意事项,还是记录下把. 首先是关于坐标: 弧长公式:L=nπr/180°或l=|α|r.地球半径大致是6400千米.以纬度0.000001为 ...

  7. 广播后刷新界面-调用其他界面的方法触动广播后刷新该界面UI

    new CigaretteLoginActivity().login(ac,"switch_account",list.get(arg2).CUST_CODE,list.get(a ...

  8. PPTP-VPN第一章——部署与简单使用

    最近前游戏同事搞了台的VPS,贡献出来做VPN使用.目前流行的服务器VPN软件主要有PPTP VPN和Openvpn.Openvpn虽然功能较为强大,且安全性高,支持nat穿越等等,但服务器端和客户端 ...

  9. 初学c# -- 学习笔记(七) RichTextBox支持GIF

    园子里许明吉博客写的一篇,刚好用到这个,写的非常好.转过来了 不过在应用中也有一些问题,win10下不能中文输入,凑合着进行了修改, 下面是原来的代码: private void button2_Cl ...

  10. jquery的hide()和show()

    jquery用hide()和show()函数来控制html元素的显示和隐藏. hide()和show()都可以带参数的,hide(1000)表示隐藏所需的时间为1秒.此外还可以用slow,fast参数 ...