LeetCode -- Best Time to Buy and Sell Stock系列
Question: 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 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.
Analysis:
问题描述:给出一个数组,其中第i个元素表示第i天的股票售价。如果只允许一次买入卖出,请给出最大利润方案(即在第i天买入,第j天卖出使得利益最大)。
思路:要求保证在 i < j 的前提下,找出数组中最大和最小的元素,两者之间的差就是最大利润。因此用一个指针指向最低元素(若当前元素的值比史上最低值还低,则指向该元素),一个整数保存目前的利润(如果当前元素减去最低值还要大于已有利润,则更新一下)。
Answer:
public class Solution {
public static int maxProfit(int[] prices) {
if(prices.length == 0 || prices.length == 1)
return 0;
int low = prices[0];
int profile = 0;
for(int i=1; i<prices.length; i++) {
if(prices[i] < low)
low = prices[i];
if(prices[i] - low > profile)
profile = prices[i] - low;
}
return profile;
}
}
Question:Best Time to Buy and Sell Stock II
Question:
Say you have an array for which the ith 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).
Analysis:
题目描述:给出一个整数数组,第i个元素表示第i天得股票售价。 设计一个算法得到最大利润,与第一个版本不同的是,这次交易允许多次买卖,但是必须保证在买入股票之前要先卖出。
思路:开始想是否要用到动态规划的思想,后来发现,这个题目是要将原来的数组划分成一个个的小波峰波谷,而每次波峰与波谷之间的差值都可以作为利润。
Answer:
public class Solution {
public int maxProfit(int[] prices) {
if(prices.length == 0 || prices.length == 1)
return 0;
int profit = 0;
for(int i=1; i<prices.length; i++) {
int diff = prices[i] - prices[i-1];
if(diff > 0) {
profit += diff;
}
}
return profit;
}
}
Question:Best Time to Buy and Sell Stock III
Say you have an array for which the ith 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).
Analysis:
问题描述:给出一个整数数组,其中第i个元素表示第i天得股价。
设计一个程序找到最大利润,最多只能进行两次交易。
思路一:暴力求解方法。一次循环分成两段,每段求最大利润,然后找到最大利润,时间复杂度为O(n2).
思路二:Dynamic Programming的思想。首先正向遍历一遍,计算若是当前交易能够得到的profit;然后逆向遍历一遍(正向遍历与逆向遍历要求的东西是不一样的,正向是求当前如果进行交易的话能够得到的profit,而逆向遍历要求从i到最后能够获得的最大收益)。
Answer:
public class Solution {
public static int maxProfit(int[] prices) {
if(prices.length == 0 || prices.length == 1)
return 0;
int n = prices.length;
//正向寻找最大利润
int low = prices[0];
int profit0 = 0;
int [] pro = new int[n];
pro[0] = 0;
for(int i=1; i<n; i++) {
if(prices[i] < low)
low = prices[i];
int temp = prices[i] - low;
if(profit0 < temp)
profit0 = temp;
pro[i] = temp;
}
//逆向寻找最大利润
int high = prices[prices.length - 1];
int profit1 = 0;
int[] pro1 = new int[n];
pro1[n-1] = 0;
for(int i=n - 2; i>=0; i--) {
if(high < prices[i])
high = prices[i];
int temp = high - prices[i];
if(profit1 < temp)
profit1 = temp;
pro1[i] = profit1;
}
int res = 0;
for(int i=0; i<n; i++) {
int temp = pro[i] + pro1[i];
//System.out.println("pro: "+pro[i]+" pro1: "+pro1[i] );
if(res < temp)
res = temp;
}
return res;
}
}
LeetCode -- Best Time to Buy and Sell Stock系列的更多相关文章
- LeetCode:Best Time to Buy and Sell Stock I II III
LeetCode:Best Time to Buy and Sell Stock Say you have an array for which the ith element is the pric ...
- [LeetCode] Best Time to Buy and Sell Stock with Cooldown 买股票的最佳时间含冷冻期
Say you have an array for which the ith element is the price of a given stock on day i. Design an al ...
- [LeetCode] Best Time to Buy and Sell Stock IV 买卖股票的最佳时间之四
Say you have an array for which the ith element is the price of a given stock on day i. Design an al ...
- [LeetCode] Best Time to Buy and Sell Stock III 买股票的最佳时间之三
Say you have an array for which the ith element is the price of a given stock on day i. Design an al ...
- [LeetCode] 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 ...
- [LeetCode] 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 —— 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 ...
- LeetCode Best Time to Buy and Sell Stock IV
原题链接在这里:https://leetcode.com/problems/best-time-to-buy-and-sell-stock-iv/ 题目: Say you have an array ...
- [LeetCode] Best Time to Buy and Sell Stock with Transaction Fee 买股票的最佳时间含交易费
Your are given an array of integers prices, for which the i-th element is the price of a given stock ...
随机推荐
- 用 jQuery 实现表单验证(转载)
jQuery 官方 API 地址: http://api.jquery.com/ 在线引用 jQuery:http://code.jquery.com/ ——选自<锋利的jQuery>(第 ...
- Layabox进阶之资源加载
资源加载失败,图片资源默认类型是image 如果是sprite可能找不到. 资源的加载顺序,场景被加载出来时,要判断该场景的资源是否都已经加载到. 点击A界面弹出来B界面,A界面的资源要在B界面之前加 ...
- 简易的vuex用法
vuex是vue中用于管理全局状态的一个组件,用于不同组件之间的通信,下面将介绍它的简单用法 首先安装vue与vuex npm install vue npm install vuex --save ...
- npm 如何提升最新版本
首先我们查看一下npm当前版本,打开cmd 运行命令: npm -v 如果不是最新版本,运行一下代码即可. npm install -g npm 这样npm就更新到最新版本了. 如果想更新到指定版本, ...
- 按平均成绩从高到低显示所有学生的“数据库”、“企业管理”、“英语”三门的课程成绩,按如下形式显示: 学生ID,,数据库,企业管理,英语,有效课程数,有效平均分
SELECT S# as 学生ID ,(SELECT score FROM SC WHERE SC.S#=t.S# AND C#='004') AS 数据库 ,(SELECT score FROM S ...
- 使用 MySQL 存储 Hue 元数据
1.在 MySQL 中增加数据库 hue 2.编辑 hue.ini 文件 [[database]] # Database engine is typically one of: # postgresq ...
- 开启一个项目如何上传到git
1.(先进入项目文件夹)通过命令 git init 把这个目录变成git可以管理的仓库 git init 2.把文件添加到版本库中,使用命令 git add .添加到暂存区里面去,不要忘记后面的小数点 ...
- 初步学习pg_control文件之十二
接前问,初步学习pg_control文件之十一,再来看下面这个 XLogRecPtr minRecoveryPoint; 看其注释: * minRecoveryPoint is updated to ...
- ReentrantLock类的hasQueuedPredecessors方法和head节点的含义
部分启发来源自文章:Java并发编程--Lock PART 1 1.如果h==t成立,h和t均为null或是同一个具体的节点,无后继节点,返回false.2.如果h!=t成立,head.next是否为 ...
- 高德API+.NET解决租房问题(新增诚信房源)
作者:李国宝链接:https://zhuanlan.zhihu.com/p/22105008(欢迎点赞)来源:知乎著作权归作者所有.商业转载请联系作者获得授权,非商业转载请注明出处. 之前有小伙伴反应 ...