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. LCN自动补偿

    LCN自动补偿 1当出现需要补偿的数据的时候,LCN将请求tm.compensate.notifyUrl配置的通知地址, 请求补偿的样例数据格式: {"groupId":" ...

  2. Rails.cache相关知识

    可能里面的一些知识已经不被大家使用了,但是作为学习,我想和大家分享一下个人关于Rails.cache的浅显的认识,望大家指教. 1.Rails.cache是什么 它是Rails中的缓存,拥有所有缓存的 ...

  3. 编写loadrunner的ftp脚本(详细步骤)

    大家好,主要给大家讲解编写loadrunner的ftp脚本详细步骤,及FTP函数注释,及FTP脚本两种编写方式,手动和录制.亲测 No problem!^_^ 1.首先要了解loadrunner中几个 ...

  4. 查看oracle/mysql数据库版本号

    1.1.       ORACLE 软件版本 使用oracle用户登录,输入echo "select * from v\$version;"|sqlplus -S / as sys ...

  5. 【java】MD5加密工具

    MD5: /** * 对指定字段进行MD5加密 * 参数为空或发生异常都会返回 @PASE-_FAIL (-1) * @author ZX * @date 2018年09月10日16:03:07 * ...

  6. appcan 多按钮提示框

    使用  appcan.window.alert EG: var btnList=new Array(); btnList[0]="确认"; btnList[1]="取消& ...

  7. 前端js倒计时(精确到毫秒)

    话不多说,直接上代码: 有需要直接拿走, <html> <head> <style> div{ width:100%; text-align:center; fon ...

  8. MediatR一个.net中简单好用的中介者模式实现方案

    MediatRGit地址:https://github.com/jbogard/MediatR 1.安装妞盖特包 一般来说只需要安装一个MediatR就行了,.net core程序需要再安装一个Med ...

  9. python之内置函数,匿名函数,递归函数

    一. 内置函函数 什么是内置函数?就是Python给你提供的,拿来直接用的函数,比如print,input等等.截止到python版本3.6.2,现在python一共为我们提供了68个内置函数.它们就 ...

  10. Linux中修改远程地址

    首先跳转到本地用户root,如果不是的话可能没有权限 第一步:安装ssh服务 执行命令:yum install openssh-server (因为我已经安装过了,所以显示的是已安装) 第二步:修改S ...