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的更多相关文章

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

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

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

  4. LeetCode算法题-Best Time to Buy and Sell Stock

    这是悦乐书的第172次更新,第174篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第31题(顺位题号是121).假设有一个数组,其中第i个元素是第i天给定股票的价格.如果 ...

  5. LeetCode算法题-Best Time to Buy and Sell Stock II

    这是悦乐书的第173次更新,第175篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第32题(顺位题号是122).假设有一个数组,其中第i个元素是第i天给定股票的价格.设计 ...

  6. &lt;LeetCode OJ&gt; 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 ...

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

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

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

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

随机推荐

  1. 【一】SpringMVC框架原理

    springmvc基础知识 1.什么是springMVC 2.springMVC框架原理(掌握) 前端控制器.处理器映射器.处理器适配器.视图解析器 3.springmvc入门程序 目的:对前端控制器 ...

  2. nginx: [emerg] open() "/var/run/nginx.pid" failed (13: Permission denied)

    现象 1.centos6.9 用rpm包安装nginx 2.修改Nginx的多个配置文件和配置项 3.service nginx restart 报错: nginx: [emerg] open() & ...

  3. [转]Android四大核心组件:Activity+Service+BroadcastReceiver+ContentProvider

    原文地址:http://c.biancheng.net/view/2918.html Android 作为一个移动设备的开发平台,其软件层次结构包含操作系统 (OS).中间件 (MiddleWare) ...

  4. redis删除多个键

    DEL命令的参数不支持通配符,但我们可以结合Linux的管道和xargs命令自己实现删除所有符合规则的键.比如要删除所有以“user:”开头的键,就可以执行redis-cli KEYS "u ...

  5. redis-cli 使用密码登录

    #./redis-cli 输入auth +空格+ 刚才设置的密码 成功

  6. 品优购商城项目(二)AngularJS、自动代码生成器、select2下拉多选框

    品优购商城想项目第二阶段 AngularJS.自动代码生成器.select2下拉多选框 完成了课程第三天.第四天的的任务. 1.学习了AngularJs前端的mvc分层思想,js部分分成control ...

  7. RabbitMQ 入门教程(PHP版) 第二部分:工作队列(Work queues)

    工作队列 在第一篇教程中,我们已经写了一个从已知队列中发送和获取消息的程序.在这篇教程中,我们将创建一个工作队列(Work Queue),它会发送一些耗时的任务给多个工作者(Works ). 工作队列 ...

  8. hadoop记录-MapReduce之如何处理失败的task(转载)

    1.1作业某个任务阻塞了,长时间占用资源不释放 1.2在MapTask任务运行完毕,ReduceTask运行过程中,某个MapTask节点挂了,或者某个MapTask结果存放的那磁盘坏掉了 在Task ...

  9. iOS 不允许横屏的简单代码

    - (NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWi ...

  10. LODOP粒度TableRowThickNess合并行测试

    之前的博文:LODOP打印table不切行TableRowThickNess. ,中是没有合并行等的表格,通过设置增大分页粒度,会找附近的表格线,然后根据表格线分页,避免了切行.如果有比较复杂的表格, ...