【LEETCODE】37、122题,Best Time to Buy and Sell Stock II
package y2019.Algorithm.array; /**
* @ProjectName: cutter-point
* @Package: y2019.Algorithm.array
* @ClassName: MaxProfit2
* @Author: xiaof
* @Description: 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 algorithm to find the maximum profit. You may complete as many transactions as you like
* (i.e., buy one and sell one share of the stock multiple times).
*
* Input: [7,1,5,3,6,4]
* Output: 7
* Explanation: Buy on day 2 (price = 1) and sell on day 3 (price = 5), profit = 5-1 = 4.
* Then buy on day 4 (price = 3) and sell on day 5 (price = 6), profit = 6-3 = 3.
*
* 你可以尽可能多的进行交易
* 那么这个题就是有的赚就买的意思了
*
* @Date: 2019/7/2 10:12
* @Version: 1.0
*/
public class MaxProfit2 { public int solution(int[] prices) {
//那就是贪心算法了
Integer curSmall = 0, result = 0;
if(prices.length == 0)
return 0; //没得赚
else {
curSmall = prices[0];
} for(int i = 1; i < prices.length; ++i) {
int temp = prices[i]; if(curSmall == null || temp < curSmall) {
curSmall = temp;
} else if(temp > curSmall) {
result += temp - curSmall;
curSmall = temp;
}
} return result;
} public static void main(String args[]) { int pres[] = {1,2,3,4,5};
System.out.println(new MaxProfit2().solution(pres)); } }

【LEETCODE】37、122题,Best Time to Buy and Sell Stock II的更多相关文章
- 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 II
这是悦乐书的第173次更新,第175篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第32题(顺位题号是122).假设有一个数组,其中第i个元素是第i天给定股票的价格.设计 ...
- leetcode 121 122 123 . Best Time to Buy and Sell Stock
121题目描述: 解题:记录浏览过的天中最低的价格,并不断更新可能的最大收益,只允许买卖一次的动态规划思想. class Solution { public: int maxProfit(vector ...
- 【刷题-LeetCode】122 Best Time to Buy and Sell Stock II
Best Time to Buy and Sell Stock II Say you have an array for which the ith element is the price of a ...
- Leetcode之动态规划(DP)专题-122. 买卖股票的最佳时机 II(Best Time to Buy and Sell Stock II)
Leetcode之动态规划(DP)专题-122. 买卖股票的最佳时机 II(Best Time to Buy and Sell Stock II) 股票问题: 121. 买卖股票的最佳时机 122. ...
- 31. leetcode 122. Best Time to Buy and Sell Stock II
122. Best Time to Buy and Sell Stock II Say you have an array for which the ith element is the price ...
- 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 题目的要求是只买卖一次,买的价格越低,卖的价格越高,肯定收益就越大 遍历整个数组,维护一个当前位置之前最低的买入价格,然后每次 ...
- leetcode:122. Best Time to Buy and Sell Stock II(java)解答
转载请注明出处:z_zhaojun的博客 原文地址 题目地址 Best Time to Buy and Sell Stock II Say you have an array for which th ...
- LEETCODE —— Best Time to Buy and Sell Stock II [贪心算法]
Best Time to Buy and Sell Stock II Say you have an array for which the ith element is the price of a ...
随机推荐
- 和jz姐姐的vp记录
即使如此,jz姐姐也漂亮的取得了胜利 有些懒得写直接口胡,所以代码也不一定有 暂时停更了 2015-2016 Petrozavodsk Winter Training Camp, Makoto rng ...
- socket简单实践
目录 socket模块: family(socket家族) fileno=None 请忽略,特殊用途 socket模块: 把tcp/ip协议层的各种数据封装啦.数据发送.接收等通过代码已经给你封装 应 ...
- Android入门教程(四)
关注我,每天都有优质技术文章推送,工作,学习累了的时候放松一下自己. 本篇文章同步微信公众号 欢迎大家关注我的微信公众号:「醉翁猫咪」 学习Android要掌握Android程序结构,和通信技术,和如 ...
- vue引入nutUI
这段时间需要做一个移动端项目,我需要选着用哪个UI库,其它的UI库没多看,看了看mintUI和nutUI,感觉mintUI的功能要比nutUI的功能少点,mintUI是饿了么团队开发的,而nutUI是 ...
- 【2019】Charles视频教程,接口测试工具最新教程
Charles 是在 windows/mac/linux下常用的网络封包截取工具,也是电商/直播/搜索/金融/H5/App等测试专用接口测试工具. Charles 支持Http/Https/Webso ...
- SSM项目实战 之 权限管理系统
目录 SSM权限管理系统 项目搭建 1.创建Maven-webapp工程 2.SSM框架集成 3.添加代码生成器 主页搭建 EasyUI主页 员工列表 1.在tree当中指定跳转的地址--暂时用tre ...
- 【转】使用eclipse的todo标签管理任务
Eclipse中的一些特殊的注释技术包括: 1. // TODO —— 表示尚未完成的待办事项. 2. // XXX —— 表示被注释的代码虽然实现了功能,但是实现方案有待商榷 ...
- mybatis Example Criteria like 模糊查询
用Mybatis代码生成工具会产生很多个XXXExample类,这些类的作用是什么? 查阅了很多资料,在这里总结归纳一下 简介XXXExample类用于构造复杂的筛选条件 它包含一个名为Criteri ...
- PHP系列 | 编译安装msgpack-php
Msgpack 是一个 PECL 扩展,此扩展提供用于与 MessagePack 序列化通信的 API. MessagePack 是一个基于二进制高效的对象序列化类库,可用于跨语言通信.它可以像JSO ...
- nodejs接收前端formData数据
转:https://www.cnblogs.com/zhensg123/p/11078579.html 很多时候需要利用formdata数据格式进行前后端交互. 前端代码可以是如下所示: <!D ...