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. springboot 使用c3p0数据库连接池的方法

    使用springboot开发时,默认使用内置的tomcat数据库连接池,经常碰到这种情况:运行时间一长,数据库连接中断了.所以使用c3p0连接池吧. 引入的maven依赖: <dependenc ...

  2. bootstrap dialog 使用模态对话框

    bootstrap3-dialog 使用模态对话框 <div class="modal fade"> <div class="modal-dialog& ...

  3. Tomcat安装后,远程IP无法访问的问题。

    我在使用阿里云与聚石塔的时候,发现Tomcat启动后,本地可以访问,但是外网无法访问,即使关闭防火墙也无法访问. 原因是 云平台的网络拦截. 阿里云:有一个入网规则 和 出网规则 ,流入数据端口  流 ...

  4. Shell 的变量功能

    搜寻路径PATH(系统预设变量) 执行命令时,系统透过PATH得路径顺序搜寻指令,如果再搜寻完后还找不到该指令,就会打印错误讯息[command not fount].   环境变量 进入shell之 ...

  5. 大毕设-MATLAB-滤波器的实现

    在工程实际中遇到的信号经常伴有噪声,为了消除或减弱噪声,提取有用信号,必须进行滤波,能实现滤波功能的系统称为滤波器.严格地讲,滤波器可以定义为对已知的激励提供规定响应的系统,响应的要求可以在时域或频域 ...

  6. centos7安装openoffice

    [摘要:间接登录openoffice民网下载硬件包,但跳转的页里却不停挨没有开.末了只能正在末端里干活下了: 1. 挑选得当的版本:http://www.openoffice.org/download ...

  7. web前端安全 XSS跨站脚本 CSRF跨站请求伪造 SQL注入

    web安全,从前端做起,总结下web前端安全的几种技术: 1,XSS XSS的全称是Cross Site Scripting,意思是跨站脚本,XSS的原理也就是往HTML中注入脚本,HTML指定了脚本 ...

  8. iOS(本地通知与远程通知)

    iOS 推送通知有两种:本地推送.远程推送. 本地推送 :  在不需要联网的情况下,由APP发出推送,常用于某一时刻的通知,如闹钟.本地通送有局限性在于当APP处于后台或者退出时就无法发出通知. 远程 ...

  9. javax/javaee-api/ Maven依赖

    <dependency>    <groupId>javax</groupId>    <artifactId>javaee-api</artif ...

  10. javascript:history.go()和History.back()的区别

    http://www.mikebai.com/Article/2009-11/757.html