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. OpenProj打开不了或者提示”Failed to load Java VM Library”的错误的解决方案

    一.双击打开OpenProj.exe没反应的解决方案: 1) 修改OpenProj1.4.0.ini,将Maximum Version=any改为Maximum Version=1.7,保存. 2)这 ...

  2. Metasploit实现木马生成、捆绑、免杀

    原创博客,转载请注出处! 我的公众号,正在建设中,欢迎关注: Meatsploit介绍 2018/01/03 更新 Metasploit是一款优秀的开源(!= 完全免费)渗透测试框架平台,在该平台下可 ...

  3. U盘刻录kali linux启动盘提示找不到镜像解决方案

    选择“继续”后会来到步骤菜单,选择从shell启动,命令 df -m 查看当前磁盘挂载情况,看到  /media 目录 输入命令 umount /media 进行挂载然后输入 exit 退出

  4. Android 即时通讯开发小结(二)

    <Android 即时通讯开发小结>基于IM Andriod 开发的各种常见问题,结合网易云信即时通讯技术的实践,对IM 开发做一个全面的总结. 相关推荐阅读:. Android 即时通讯 ...

  5. 浅谈IHttpHandler

    在Web应用开发或接口开发时,处理请求接口IHttpHandler随处可见,那么我们这次来简单聊一下这个接口. ASP.NET响应Http请求时常用的两个处理接口,分别是IHttpHandler和IH ...

  6. 安装R和Rstudio后,Rstudio出现空白和fatal error问题

    解决方法: 1.一定要以管理员方式启动Rstudio 2.只能安装一个R版本,之前安装的其他版本需要卸载干净 3.R和Rstudio需要安装在同一目录下

  7. C++ 洛谷 P2458 [SDOI2006]保安站岗 from_树形DP

    P2458 [SDOI2006]保安站岗 没学树形DP的,看一下. 题目大意:一棵树有N个节点,现在需要将所有节点都看守住,如果我们选择了节点i,那么节点i本身,节点i的父亲和儿子都会被看守住. 每个 ...

  8. JavaScript面试核心考点(精华)

    引言 Javascript是前端面试的重点,本文重点梳理下 Javascript 中的常考基础知识点,然后就一些容易出现的题目进行解析.限于文章的篇幅,无法将知识点讲解的面面俱到,本文只罗列了一些重难 ...

  9. MyBatis从入门到精通(八):MyBatis动态Sql之foreach标签的用法

    最近在读刘增辉老师所著的<MyBatis从入门到精通>一书,很有收获,于是将自己学习的过程以博客形式输出,如有错误,欢迎指正,如帮助到你,不胜荣幸! 本篇博客主要讲解如何使用foreach ...

  10. HDU 5723:Abandoned country(最小生成树+算期望)

    http://acm.hdu.edu.cn/showproblem.php?pid=5723 Abandoned country Problem Description   An abandoned ...