【LEETCODE】36、121题,Best Time to Buy and Sell Stock
package y2019.Algorithm.array; /**
* @ProjectName: cutter-point
* @Package: y2019.Algorithm.array
* @ClassName: MaxProfit
* @Author: xiaof
* @Description: 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 (i.e., buy one and sell one share of the stock),
* design an algorithm to find the maximum profit.
* Note that you cannot sell a stock before you buy one.
*
* Input: [7,1,5,3,6,4]
* Output: 5
* Explanation: Buy on day 2 (price = 1) and sell on day 5 (price = 6), profit = 6-1 = 5.
* Not 7-1 = 6, as selling price needs to be larger than buying price.
*
* 说白了,这个题就是求最大差额,数组是每日的价格表,我们要选2天,买进和卖出,然后获取能获益的差额,而且卖只能在买进之后
* @Date: 2019/7/2 9:42
* @Version: 1.0
*/
public class MaxProfit { public int solution(int[] prices) {
//这里有点想之前的dp一维数组的意思
//我们只需要判断当前的数据和之前最小的那个数据的差值是否是更大
int result = 0, small = 0;
if(prices.length == 0)
return 0;
else {
small = prices[0];
} for(int i = 1; i < prices.length; ++i) {
int temp = prices[i];
//我们只需要判断当前的数据和之前最小的那个数据的差值是否是更大
result = (temp - small) > result ? temp - small : result; if(temp < small) {
small = temp;
}
} return result;
} public static void main(String args[]) { int pres[] = {1,2};
System.out.println(new MaxProfit().solution(pres)); }
}

【LEETCODE】36、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 we ...
- LeetCode(122) Best Time to Buy and Sell Stock II
题目 Say you have an array for which the ith element is the price of a given stock on day i. Design an ...
- LeetCode之“动态规划”:Best Time to Buy and Sell Stock I && II && III && IV
Best Time to Buy and Sell Stock I 题目链接 题目要求: Say you have an array for which the ith element is the ...
- LeetCode算法题-Best Time to Buy and Sell Stock
这是悦乐书的第172次更新,第174篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第31题(顺位题号是121).假设有一个数组,其中第i个元素是第i天给定股票的价格.如果 ...
- LeetCode算法题-Best Time to Buy and Sell Stock II
这是悦乐书的第173次更新,第175篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第32题(顺位题号是122).假设有一个数组,其中第i个元素是第i天给定股票的价格.设计 ...
- <LeetCode OJ> 121. /122. Best Time to Buy and Sell Stock(I / II)
Say you have an array for which the ith element is the price of a given stock on day i. If you were ...
- LeetCode(123) Best Time to Buy and Sell Stock III
题目 Say you have an array for which the ith element is the price of a given stock on day i. Design an ...
- LeetCode Array Easy 122. Best Time to Buy and Sell Stock II
Description Say you have an array for which the ith element is the price of a given stock on day i. ...
- [Leetcode 122]买股票II Best Time to Buy and Sell Stock II
[题目] Say you have an array for which the ith element is the price of a given stock on day i. Design ...
- [LeetCode&Python] Problem 122. Best Time to Buy and Sell Stock II
Say you have an array for which the ith element is the price of a given stock on day i. Design an al ...
随机推荐
- nrm -- 一键切换npm源
0. 背景 先描述一下没有nrm时我们是怎样使用npm源的 查看npm源地址,终端中输入 npm config list 可以看到npm源 metrics-registry = "https ...
- Javascript之hoisting变量提升
javascript不仅仅是一门弱类型语言,还是一门解释型语言.一门编程语言的本质就是这样,优点即是缺点,缺点也往往是优点.JS因为有了变量提升,能够使我们在编程时可以忽略“先声明,再使用”的规则,但 ...
- ActiveMQ相关API
一.Producer 1,发送消息 MessageProducer send(Message message)发送消息到默认目的地,就是创建Producer时指定的目的地. send(Destinat ...
- 手动卸载wine-stable卸载
装了wine发现运行TIM全都乱码,于是手动卸载wine: 删除文件:(参考https://blog.csdn.net/taizhoufox/article/details/5496568) ~/.w ...
- mtcnn论文学习
Joint Face Detection and Alignment using Multi-task Cascaded Convolutional Networks 使用多任务级联卷积网络连接人脸检 ...
- 009-Spring Boot全局配置跨域请求支持
1.Spring Boot 2.0以前全局配置跨域主要是继承WebMvcConfigurerAdapter @Configuration public class CorsConfig extends ...
- java-mybaits-016-mybatis知识点StatementType
1.statementType 在mapper文件中可以使用statementType标记使用什么的对象操作SQL语句. statementType:标记操作SQL的对象 要实现动态传入表名.列名,需 ...
- ES6深入浅出-5 新版对象-2.属性修饰符
对象语法增强 已经有了个对象的新增语法 还需要一个api来做呢?. 因为有的时候,你需要在旧的对象上添加get.set. 读的时候就走get 写的时候就走set 假设很早之前在项目里写了一个old对象 ...
- 【 Linux 】Systemd 使用说明(1)
1. 前言 在 CentOS 7 中使用 systemd 取代了 init 的启动模式,这样的更新换代有什么好处呢?首先需要对 init 和 systemd 有个概念的认识. 2. init 概述 在 ...
- 【PromQL】prometheus查询语言
常用查询: https://songjiayang.gitbooks.io/prometheus/content/exporter/nodeexporter_query.html group by 操 ...