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

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

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

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

  3. leetcode 121 122 123 . Best Time to Buy and Sell Stock

    121题目描述: 解题:记录浏览过的天中最低的价格,并不断更新可能的最大收益,只允许买卖一次的动态规划思想. class Solution { public: int maxProfit(vector ...

  4. 【刷题-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 ...

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

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

  7. 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 题目的要求是只买卖一次,买的价格越低,卖的价格越高,肯定收益就越大 遍历整个数组,维护一个当前位置之前最低的买入价格,然后每次 ...

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

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

随机推荐

  1. 机器学习---逻辑回归(一)(Machine Learning Logistic Regression I)

    逻辑回归(Logistic Regression)是一种经典的线性分类算法.逻辑回归虽然叫回归,但是其模型是用来分类的. 让我们先从最简单的二分类问题开始.给定特征向量x=([x1,x2,...,xn ...

  2. 最大の和(洛谷 AT2412)

    题意 读入n个整数的数列a1,a2,…,an和正整数k(1<=k<=n),请输出连续排列的k个整数的和的最大值 输入 第一行是正整数n(1<=n<=100000)和正整数k(1 ...

  3. 【18NOIP普及组】对称二叉树(信息学奥赛一本通 1981)(洛谷 5018)

    [题目描述] 一棵有点权的有根树如果满足以下条件,则被轩轩称为对称二叉树: 1.二叉树: 2.将这棵树所有节点的左右子树交换,新树和原树对应位置的结构相同且点权相等. 下图中节点内的数字为权值,节点外 ...

  4. mysql 选择所有同学名字

    mysql> select * from test; +----+----------+-------+-----------+ | id | name | score | subject | ...

  5. mysql 根据发音查找内容

    当前表 mysql> select * from table1; +----------+------------+-----+ | name_new | transactor | pid | ...

  6. [SNOI2019]纸牌

    传送门 Description 有一副纸牌.牌一共有\(n\)种,分别标有 \(1,2,...,n\),每种有\(C\)张.故这副牌共有\(nC\)张. 三张连号的牌(\(i,i+1,i+2\))或三 ...

  7. 使用OSCache优化性能,及JPA二级缓存

    1.使用静态化页面技术: 要统计产品的浏览次数: 在<body> <img src="http://www.site.com/data/count.do?productId ...

  8. Predictive Analysis in Network Function Virtualization

    摘要 网络功能虚拟化(NFV)体系结构的最新部署获得了极大的关注.虚拟化虽然带来了诸如降低成本和简化网络功能部署之类的好处,但它增加了附加层,从而降低了较低层故障的透明度.为了改善虚拟网络功能(VNF ...

  9. QTP测试学习笔记

    QuickTest Professional(简称QTP)功能自动化测试,原属于Mercury Interactive公司产品,2006年7月被惠普公司收购了,通过安装文件目录可以看到,都是默认放在C ...

  10. [转]Java 之 Serializable 序列化和反序列化的概念,作用的通俗易懂的解释

    原文地址:https://blog.csdn.net/qq_27093465/article/details/78544505 遇到这个 Java Serializable 序列化这个接口,我们可能会 ...