【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 ...
随机推荐
- circus 进程以及socket 管理工具&&docker运行
circus 是由mozilla 团队开发基于python 以及zeromq 的进程以及socket 管理的工具,类似supervisord 但是比supervisord 更灵活方便 来自官方的使用比 ...
- JMeter学习2
JMeter学习(四)参数化 参数化:录制脚本中有登录操作,需要输入用户名和密码,假如系统不允许相同的用户名和密码同时登录,或者想更好的模拟多个用户来登录系统. 这个时候就需要对用户名和密码进行参数化 ...
- 【loj2983】【WC2019】数树
题目 两颗\(n\)个点的树T1和T2,有\(y\)种颜色; 现在给每个点染色,要求公共边端点的颜色相同,求: 1.op=0 , T1和T2都确定,求合法染色方案数: 2.op=1 , T1确 ...
- manjaro AwesomeWM 上使用双显示器
本文通过MetaWeblog自动发布,原文及更新链接:https://extendswind.top/posts/technical/dual_monitor_manjaro_awesome 安装ma ...
- PHP 打印输出数组内容及结构 print_r 与 var_dump 函数
利用 print_r() 函数可以打印输出整个数组内容及结构,按照一定格式显示键和元素.注意 print_r() 函数不仅是只用于打印,实际它是用于打印关于变量的易于理解的信息. 例子1 <?p ...
- CF1221F Choose a Square(二维偏序)
由于y=x,我们可以将点对称过来,以便(x,y)(x<y) 考虑选取正方形(a,a,b,b),点集则为\((a\le x\le y\le b)\),相当于二维数点 将点按x降序,y升序排列,线段 ...
- Kerberos(一) 安装
1.服务器基本信息 操作系统:centos7 数量:2 主机名映射关系 IP hostname server 192.168.4.50 manager1 Kerberos server(kdc) 19 ...
- firewalld添加/删除服务service,端口port
启动CentOS/RHEL 7后,防火墙规则设置由firewalld服务进程默认管理. 一个叫做firewall-cmd的命令行客户端支持和这个守护进程通信以永久修改防火墙规则. # firewall ...
- rk3288 usb无线网卡支持 8188eu
第一部分是kernel 内核配置参考rk文档,把device driver 下wireless相关的先勾选上. 编译到buildin有问题,识别不到,所以打算编译成ko cd kernel/driv ...
- Java基础 throws 提示调用方法时要注意处理相关异常
JDK :OpenJDK-11 OS :CentOS 7.6.1810 IDE :Eclipse 2019‑03 typesetting :Markdown code ...