【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 ...
随机推荐
- 深度学习面试题11:池化(same池化、valid池化、带深度的池化)
目录 Same最大值池化 多深度的same池化 Same平均值池化 Valid池化 参考资料 池化(Pooling)操作与卷积类似,取输入张量的每个位置的矩形领域内的最大值或平均值作为该位置的输出. ...
- Oracle导出/导入数据库的三种模式
导出 模式一:全量导出(慎用) exp 用户名/密码@数据库实例 owner=用户名 file=文件存储路径 log=日志存储路径 full=y 栗子:exp Mark/123456@151.2.*. ...
- Firefox disable search in the address bar
disable search in the address bar Hi oitconz, setting keyword.enabled to false prevents Firefox from ...
- Unity3D ACT动作游戏《武士2》项目源码附教程
武士二源码亲测unity2018.3.6能运行 仅供学习附有教程 教程完整73课,网上大部分一般为65课, 教程大小27GB,mp4格式 整理不易 扫码时备注或说明中留下邮箱 付款后如未回复请至htt ...
- c++ 模拟java的反射,根据类名动态创建类
参考: https://blog.csdn.net/jnu_simba/article/details/9318799 原先有静态变量依赖问题, https://blog.csdn.net/anony ...
- CMU Advanced DB System - Query Optimizer
Overview Optimizer模块所处在的位置如图, 那么做optimize的目的是, 找出所有‘correct’执行计划中‘cost’最低的 那么这里首先要明确的概念,‘correct’,关系 ...
- MiniUI treeGrid 动态加载数据与静态加载数据的区别
说明:treegrid静态数据加载时数据结构是一棵树包含children节点集合,而采用动态加载数据时数据是List结构的具体项. 静态加载数据 test1.html <!DOCTYPE htm ...
- MyBatis 示例之存储过程
存储过程在数据库中比较常见,虽然大多数存储过程比较复杂,但是使用 MyBatis 调用时,用法都一样,因此我们这一节使用一个简单的存储过程来了解 MyBatis 中存储过程的使用方法. 基本准备 存储 ...
- Diffie-Hellman算法简介
一.DH算法是一种密钥交换协议,它可以让双方在不泄漏密钥的情况下协商出一个密钥来. DH算法基于数学原理,比如小明和小红想要协商一个密钥,可以这么做: . 小明先选一个素数和一个底数,例如,素数p=, ...
- py文件单独调试django ORM的配置
1.新建一个py文件 2.代码环境搭建 import os if __name__ != '__main__': exit() # 加载django项目的配置信息 os.environ.setdefa ...