Say you have an array for which the i th 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 (ie, buy one and sell one share of the stock multiple times). However, you may not engage in multiple transactions at the same time (ie, you must sell the stock before you buy again).

public class Solution {
public int maxProfit(int[] prices) {
int sum = 0;
if(prices.length <= 1)
return 0;
for(int i=0; i < prices.length-1 ; i++){
if(prices[i+1] > prices[i]){
int y = prices[i+1] - prices[i];
sum = sum + y;
}
}
if(sum<0)
return 0;
else
return sum;
}
}

  

贪心算法---The best time to buy and sell store-ii的更多相关文章

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

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

  2. 算法(12)Best Time to Buy and Sell Stock II

    题目:最大收益 [1,2,3,9,2,3] 思路:这道题竟然是easy的?! 最终的解法非常简单,只要把单个波峰减去波谷就可以了,比如在上面的例子中[1-2-3-9][2-3]这就是单个波峰波谷!为什 ...

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

  4. Algorithm - 贪心算法使用场景 ( LEETCODE —— Best Time to Buy and Sell Stock II)

    先看一道leetcode题: Best Time to Buy and Sell Stock II Say you have an array for which the ith element is ...

  5. 【LEETCODE】37、122题,Best Time to Buy and Sell Stock II

    package y2019.Algorithm.array; /** * @ProjectName: cutter-point * @Package: y2019.Algorithm.array * ...

  6. LeetCode122——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 a ...

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

  8. [LintCode] 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 ...

  9. 27. Best Time to Buy and Sell Stock && Best Time to Buy and Sell Stock II && Best Time to Buy and Sell Stock III

    Best Time to Buy and Sell Stock (onlineJudge: https://oj.leetcode.com/problems/best-time-to-buy-and- ...

随机推荐

  1. Laravel --- 查询字段中使用表达式

    比如: select id, name, count(post) from ... 在laravel中: $user = $this ->select( 'id', 'name', DB::ra ...

  2. 如何有效预防XSS?这几招管用!!!

    原文链接 预防XSS,这几招管用 最近重温了一下「黑客帝国」系列电影,一攻一防实属精彩,生活中我们可能很少有机会触及那么深入的网络安全问题,但工作中请别忽略你身边的精彩 大家应该都听过 XSS (Cr ...

  3. 打包成war包之后如何读取配置文件

    今天工作开发中遇到一个问题:在idea运行的项目读取配置文件没有问题,打包成war包之后就会报错java.io.FileNotFoundException: class path resource 原 ...

  4. springboot 2.X 在访问静态资源的的时候出现404的问题

    通过idea快速搭建一个springboot项目: springboot版本2.1.6 在网上看的资料,springboot静态资源访问如下: "classpath:/META‐INF/re ...

  5. 【过时】项目转Maven后出现的问题记录

    上图,文字后补充 1.过程 创建一个新的web项目,项目名称与原项目名称一致.注意勾选“添加mvn支持(红框部分)”,勾选后运行目标服务器会变为none,这里无法进行添加. 2.项目创建完成后,会报错 ...

  6. 转载 make版MYsql 5.5.13

    使用cmake安装mysql5.5.132014-04-09 12:59:42 分类: Mysql/postgreSQL 原文地址:使用cmake安装mysql5.5.13 作者:isqlw 安装cm ...

  7. 点菜网---Java开源生鲜电商平台-商品基础业务架构设计-商品分类(源码可下载)

    点菜网---Java开源生鲜电商平台-商品基础业务架构设计-商品分类 (源码可下载) 说明:我们搞过电商的人都可以体会到,搞生鲜电商是最复杂的,为什么复杂呢?我总结了有以下几个业务特性决定的: 1. ...

  8. while循环语句、格式化输出、常用运算符、字符编码

    1.while循环 while 空格 条件 冒号 缩进 循环体 num=1 while num<11: print(num) num=num+1 变量都是先执行等号右边的,然后执行等号左边的. ...

  9. leetcode的Hot100系列--461. 汉明距离

    求两个数的二进制位不同的位置,最先想到的就是异或操作, 异或:按位运算,相同为0,不同为1. 比如: a = 6 对应的二进制表示为: 0 0 1 1 1 ​ b = 9 对应的二进制表示为: 0 1 ...

  10. IDEA中Maven依赖包下载不了的问题解决方案汇总

    第一种方案: 第二种方案:下面的几个不要选择. 第三种方案:可能是某一个 dependency 依赖无法下载,导致整个项目都报错 打开具体的报错的maven项目的pom.xml.试着去删除一些 dep ...