121 Best Time to Buy and Sell Stock
输入:一个数组prices,prices[i]表示第i天股票的价格。
输出:买卖股票的最大收益。
规则:只允许最多买一次,最多卖一次股票。如果觉得价格不合适,可以不买卖。
分析1:最先想到的是暴力搜索,每天都可以买或者卖,当然要符合规则。倒是AC了,但是时间太长。这个复杂度应该是2^n。
private int maxProfit;public int maxProfit(int[] prices) {maxProfit = 0;trasaction(prices,0,false,true,0);return maxProfit;}private void trasaction(int[] prices,int idx,boolean sell,boolean buy,int profit){if(idx==prices.length){maxProfit = Math.max(maxProfit,profit);}else{if(buy){trasaction(prices,idx+1,true,false,profit-prices[idx]);}else if(sell){maxProfit = Math.max(maxProfit,profit+prices[idx]);}trasaction(prices,idx+1,sell,buy,profit);}}
分析2:这是参考别的答案。只要找到最低成本minPrice,如果其下标为i,只要j>i,prices[j]-minPrice就是收益。各个收益取最大值。这是技巧问题,背多次,形成这样的思维。
public int maxProfit(int[] prices) {int maxProfit = 0;int minPrice = Integer.MAX_VALUE;for(int i=0;i<prices.length;i++){minPrice = Math.min(minPrice,prices[i]);maxProfit = Math.max(maxProfit,prices[i]-minPrice);}return maxProfit;}
进阶题目122:可以多次买卖股票。但是得先买股票才能卖股票。如果已经买了股票,需要卖了,才能再买。
分析1:暴力购买。超时。
学习1:参考网页。收益profit是子收益sub-profit的和。在第i天购买,第j天卖,每个sub-profit是不同的。在[i,j]范围内我们该怎么选择才能让sub-profit最大呢?我们应该找到的j是在这个范围内prices[j]是尽可能大的那个值,i是在这个范围内prices[i]尽可能小的那个值。
举例来说。我们有数组[3,2,5]。我们会选择[2,5],而不会选择[3,5],因为2<5。
如果数组[3,2,5,8],我们会选择[2,8],因为5<8。
从这两个例子,我们观察到我们选择购买的那个x应该是一个连续值中的最小值。我们卖出去的那个y应该一个连续序列中的最大值。
再举个例子。[3,2,5,8,1,9],虽然1是最小值,但是我们选择2,因为2是一个连续递减序列的最小值(从3开始)。同样虽然9是最大值,但是我们选择8。因为8是从2开始的连续递增序列的最大值。
实际上,[3,2,5,8,1,9]被分成了2个子数组,分别选择[2,8]和[1,9]。
public int maxProfitV2(int[] prices) {int maxProfit = 0;int i=0;int n = prices.length;while(i<n-1){while(i<n-1 && prices[i+1]<=prices[i]) i++;int buy = prices[i];while(i<n-1 && prices[i+1]>prices[i]) i++;int sell = prices[i];maxProfit += sell - buy;}return maxProfit;}
121 Best Time to Buy and Sell Stock相关推荐
- 【贪心 和 DP + 卖股票】LeetCode 121. Best Time to Buy and Sell Stock
LeetCode 121. Best Time to Buy and Sell Stock Solution1:我的答案 动态规划和贪心不要区分的那么明显嘛~~~ class Solution { p ...
- LeetCode 121 Best Time to Buy and Sell Stock
Say you have an array for which the ith element is the price of a given stock on day i. If you were ...
- LeetCode 121. Best Time to Buy and Sell Stock
题目: Say you have an array for which the ith element is the price of a given stock on day i. If you w ...
- leetcode python3 简单题121. Best Time to Buy and Sell Stock
1.编辑器 我使用的是win10+vscode+leetcode+python3 环境配置参见我的博客: 链接 2.第一百二十一题 (1)题目 英文: Say you have an array fo ...
- Leetcode——121. Best Time to Buy and Sell Stock
题目原址 https://leetcode.com/problems/best-time-to-buy-and-sell-stock/description/ 题目描述 Say you have an ...
- LeetCode 121 Best Time to Buy and Sell Stock(股票买入卖出的最佳时间)
翻译 话说你有一个数组,其中第i个元素表示在第i天的股票价格.如果你被只被允许最多一次交易(例如,买入然后卖出一个股票),设计一个算法并找出最大利润. 原文 Say you have an array ...
- leetcode 121.Best Time to Buy and Sell Stock 股票买卖最佳时间
题目: Say you have an array for which the ith element is the price of a given stock on day i. If you w ...
- 121. Best Time to Buy and Sell Stock买卖股票的最佳时机
You are given an array prices where prices[i] is the price of a given stock on the ith day. 给定一个数组 p ...
- 121. Best Time to Buy and Sell Stock
一.题目 1.审题 2.分析 给出一个数组,让你先已 val1 买入,再 以 val2 卖出,求获得的利润最大是多少. 二.解答 1.思路: 方法一. 用遍历 min 记录到当前位置的最小值: max ...
最新文章
- 汇编-理解call,ret
- PHP代码程序,php代码自动执行
- Java 错误: 找不到或无法加载主类,问题集合
- 图解HTTP学习记录(六)
- thinkphp模版常量替换机制
- Tomcat几种启动报错及解决办法
- iOS开发 - 抛出异常
- ehcache springboot_阿里内部进阶学习SpringBoot+Vue全栈开发实战文档
- windows2012 wsus6 更新慢
- vue axios的路由拦截器
- Python [6] IT资产管理(下)
- 令人眼前一亮的下拉式终端 Tilda Guake
- 小萝卜控机大师录制脚本(手机app自动化)
- 浅析物联网及云计算技术在传统汽车检测线中的应用
- F2FS之磁盘NAT和SIT布局及相关函数分析
- Tableau 第五章 创建仪表板和故事
- 初中英语语法(017)-关系代词
- PS CS6视频剪辑基本技巧(三)添加声音和字幕
- 计算机一级考试文件夹移动怎么操作,怎么用键盘移动文件夹而不用鼠标?电脑老师下的任务、、、...
- leetcode之Kth Largest Element in an Array
热门文章
- 【NOIP 2017】列队
- [转]unity3D游戏开发之GUI
- 【Android Developers Training】 58. 缓存位图
- HDU 2222 Keywords Search
- 秘罗地伤痕 -- 暂存小说草稿
- 我写的几篇技术文章之一:Windows消息拦截技术的应用
- linux可用机场客户端,Linux系统可用的6款Bittorrent客户端
- 解决org.apache.hadoop.io.nativeio.NativeIOException: 当文件已存在时,无法创建该文件。
- Vue介绍-Vue简单应用-Vue基本用法
- 从底层重学 Java 之 Stream 初探 Gitchat连接