【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 ...
随机推荐
- Java如何正确的将数值转化为ArrayList?
Java中使用工具类Arrays.asList()看似可以把一个数组转为List,但实际使用时有两个坑:1.它是泛型方法,传入的参数必须是对象数组,当传入一个原生数据类型数组时,Arrays.asLi ...
- 了解Python-白 驹 过 隙 , 忽 然 而 已
白 驹 过 隙 , 忽 然 而 已 人 生 苦 短,我 用 Python -- Life is short , you need Python 代码量少,同一样问题 ,用不同的语言解决时,一般情况下P ...
- java通过请求对象获取ip地址、获取ip地址
/** * 获取登录ip */ public String getIp(){ HttpServletRequest request = this.getRequest(); String ip = & ...
- scrapy+splash 爬取京东动态商品
作业来源:https://edu.cnblogs.com/campus/gzcc/GZCC-16SE1/homework/3159 splash是容器安装的,从docker官网上下载windows下的 ...
- pyqt5设置背景图片出现问题
在使用pyqt5时,用qtdesign设置好背景图片,如何设置自行百度,预览没问题,用ptuic5转换为代码却发现显示不了: 首先:我在qtdesign中导入的是pic.qrc,但是转换的代码最后一句 ...
- 剑指offer:丑数
题目描述: 把只包含质因子2.3和5的数称作丑数(Ugly Number).例如6.8都是丑数,但14不是,因为它包含质因子7. 习惯上我们把1当做是第一个丑数.求按从小到大的顺序的第N个丑数. 解题 ...
- Linux Shell:根据指定的文件列表 或 map配置,进行文件位置转移
读取配置文件,进行文件位置转移 在whenb.csv中指定了需要从/home/root/cf/下移除到/home/root/cf_wh/下文件列表,whenb.csv中包含记录如下: enb- enb ...
- windows如何查看jdk的安装目录
1.检查电脑上是否安装了JDK可以在cmd窗口输入java -version查看是否需安装了JDK 2.查看JDK的安装目录 一种是在cmd窗口输入java -verbose,查看最后一行即为JDK安 ...
- SQL优化:一些简单的又实用的SQL优化方案【转】
面试过程中,面试官有极高的频率会问道数据库的优化,SQL语句的优化,网上关于SQL优化的教程很多,但是鱼目混杂,显得有些杂乱不堪.近日有空整理了一下,写出来跟大家分享一下,其中有错误和不足的地方,还请 ...
- Maven构建报错问题解决
[ERROR] Failed to execute goal on project zepeto-admin: Could not resolve dependencies -SNAPSHOT: Fa ...