题目描述1

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 maxProfile=0;for(int i=0;i<prices.length-1;i++){if(prices[i]<prices[i+1]){maxProfile+=prices[i+1]-prices[i];}}return maxProfile;}
}

题目描述2

Say you have an array for which the i th element is the price of a given stock on day i.
If you were only permitted to complete at most one transaction (ie, buy one and sell one share of the stock), design an algorithm to find the maximum profit.

解析:题目表示最多只能买卖一次

public class Solution {public int maxProfit(int[] prices) {int maxProfile=0;for(int i=0;i<prices.length;i++){for(int j=i+1;j<prices.length;j++){maxProfile=maxProfile>(prices[j]-prices[i])?maxProfile:(prices[j]-prices[i]);}}return maxProfile;}
}

题目描述3(较为复杂些了

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 at most two transactions.
Note:
You may not engage in multiple transactions at the same time (ie, you must sell the stock before you buy again).

解析:最多买卖两次,可以以i为分界线,然后在i的左侧搜索最大的利润,在i的右侧也搜索最大的利润,最后算总的利润。

测试用例
输入:
[6,1,3,2,4,7]

输出:
7

import java.util.*;
public class Solution {public int maxProfit(int[] prices) {int maxProfile=0;int length=prices.length;for(int i=0;i<length;i++){int leftMax=0,rightMax=0;for(int j=0;j<i;j++){//搜索左侧的最大利润for(int k=j+1;k<=i;k++){int temp =prices[k]-prices[j];leftMax=leftMax>temp?leftMax:temp;}}for(int j=i+1;j<length;j++){//搜索右侧的最大利润for(int k=j+1;k<length;k++){int temp =prices[k]-prices[j];rightMax=rightMax>temp?rightMax:temp;}}if(leftMax+rightMax>maxProfile){maxProfile=leftMax+rightMax;}}return maxProfile;}
}

《leetcode》best-time-to-buy-and-sell-stock-i-ii-iii相关推荐

  1. 【LeetCode】 Best Time to Buy and Sell Stock I II III IV 解题报告

    原文地址:http://liangjiabin.com/blog/2015/04/leetcode-best-time-to-buy-and-sell-stock.html Best Time to ...

  2. Best Time to Buy and Sell Stock I II III IV (第四周 动态规划)

    Best Time to Buy and Sell Stock I II III IV (第四周 动态规划) Best Time to Buy and Sell Stock I Say you hav ...

  3. 【leetcode】best time to buy and sell stocks(i, ii, iii, iv, v)

    i.只允许一次交易 思路:从前向后遍历数组,记录当前出现过的最低价格,作为买入价格,并计算以当天价格出售的收益,作为可能的最大收益,整个遍历过程中,出现过的最大收益就是所求. class Soluti ...

  4. LeetCode 123. Best Time to Buy and Sell Stock III--Python解法--动态规划--数学题

    此文首发于我的个人博客:zhang0peter的个人博客 LeetCode题解文章分类:LeetCode题解文章集合 LeetCode 所有题目总结:LeetCode 所有题目总结 题目地址:Best ...

  5. leetcode 714. Best Time to Buy and Sell Stock with Transaction Fee | 714. 买卖股票的佳最时机含手续费(递归->傻缓存->dp)

    题目 https://leetcode.com/problems/best-time-to-buy-and-sell-stock-with-transaction-fee/ 题解 经典的 暴力递归 - ...

  6. 【DP + 卖股票】LeetCode 714. Best Time to Buy and Sell Stock with Transaction Fee

    LeetCode 714. Best Time to Buy and Sell Stock with Transaction Fee Solution1: 参考网址:http://www.cnblog ...

  7. 【DP + 卖股票】LeetCode 309. Best Time to Buy and Sell Stock with Cooldown

    LeetCode 309. Best Time to Buy and Sell Stock with Cooldown Solution1: 比较有难度的一道动态规划题了! 参考网址:http://z ...

  8. 【DP + 卖股票】LeetCode 188. Best Time to Buy and Sell Stock IV

    LeetCode 188. Best Time to Buy and Sell Stock IV Solution1:我的答案 参考链接:http://www.cnblogs.com/grandyan ...

  9. 【DP + 卖股票】LeetCode 123. Best Time to Buy and Sell Stock III

    LeetCode 123. Best Time to Buy and Sell Stock III Solution1: 不得不让人感叹算法之精妙啊!!! 参考网址:[1]http://www.cnb ...

  10. 【贪心 和 DP + 卖股票】LeetCode 122. Best Time to Buy and Sell Stock II

    LeetCode 122. Best Time to Buy and Sell Stock II Solution1:我的答案 贪心和DP傻傻分不清! class Solution { public: ...

最新文章

  1. android系统action大全
  2. Spring Boot集成Debezium监控数据库变化
  3. 计算机与人力资源管理论文,计算机专业毕业论文-人力资源管理系统的研究与实现...
  4. 傅里叶变换音频可视化_HTML5如何实现音频可视化频谱跳动
  5. Oracle 单行函数
  6. 奇妙的安全旅行之DSA算法
  7. CentOS英文版下安装中文语言包
  8. 我的Linux系统的一些截图 之二
  9. leetcode28 Implement strStr() 在字符串中寻找目标字符串
  10. exchange2003系列总结:-5邮件加密与签名的工作流程
  11. Perl 脚本命令行传入参数
  12. 电子科技大学《图论及其应用》复习总结--第五章 匹配与因子分解
  13. 服务器temp文件夹文件都能清理吗,Temp文件夹是什么?Windows中Temp文件夹下内容可以随意删除吗?...
  14. 【C++要笑着学】多态 | 重写(覆盖) | 协变构多态 | 父虚子非虚也构多态 | final与override关键字(C++11) | 抽象类 | 纯虚函数 | 抽象类指针
  15. 最少多少步才能逮住兔子
  16. 语言模型(马尔可夫模型,n元语法)
  17. 微软为什么能转型成功?
  18. 【JavaSe】面向对象篇(五) 三大特征之二继承
  19. Vue2和Vue3的双向数据绑定原理
  20. 金山软件2015校园招聘

热门文章

  1. leetcode--5. 最长回文子串
  2. JavaScript的运动——模拟重力场
  3. 22行代码AC——习题5-6 对称轴(Symmetry,UVa1595)——解题报告
  4. 第一个linux桌面,Ubuntu 4.10 “Warty Warthog”:回顾第一个Ubuntu Linux桌面
  5. 企业日志分析ELK(Logstash+Elasticsearch+Kibana)介绍及搭建
  6. java gc 可以对方法区进行回收_浅谈 Java 之 GC
  7. clickhouse原理解析与应用实践_Hybrid App (混合应用) 技术全解析 方案原理篇
  8. RT-Thread工程代码框架分析——(1)启动流程
  9. ubuntu下gcc的安装与使用
  10. jsonp跨域读取cookie