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 ...
随机推荐
- 高级同步器:信号量Semaphore
引自:https://blog.csdn.net/Dason_yu/article/details/79734425 一.信号量一个计数信号量.从概念上讲,信号量维护了一个许可集.Semaphore经 ...
- docker 启动 nginx 访问不了的问题
使用版本:nginx version: nginx/1.13.8 正使用docker启动nginx容器的时候,一切都很正常,容器也起来了 docker run -dit -p 80:80 --name ...
- php实现redis
<?php //实例化Redis对象 $red=new Redis(); //链接redis服务 $red->connect('localhost','6379'); //具体操作 $re ...
- while,格式化输出
1. while循环: while 条件: 代码块(循环体) num=1 while num<=5: print(num) num+=1 break:结束循环;停止当前本层循环 continue ...
- http一些常见知识记录
HTTP请求包(浏览器信息) 我们先来看看Request包的结构, Request包分为3部分,第一部分叫Request line(请求行), 第二部分叫Request header(请求头),第三部 ...
- 10-C++远征之模板篇-学习笔记
C++远征之模板篇 将会学到的内容: 模板函数 & 模板类 -> 标准模板类 友元函数 & 友元类 静态数据成员 & 静态成员函数 运算符重载: 一切皆有可能 友元函数 ...
- 关于xampp 集成开发包电脑重启mysql无法启动的问题
关于xampp 集成开发包电脑重启mysql无法启动的问题. 在做php开发时,安装过xampp,也不知道是版本老了还是什么问题,总是出现当天晚上下班关机,第二天上班mysql不能启动,在网上查找些资 ...
- SQL 公用表表达式(CTE)
1.概念 公用表表达式(Common Table Expression)是SQL SERVER 2005版本之后引入的一个特性.CTE可以看作是一个临时的结果集,可以在接下来的一个SELECT,INS ...
- .Net 面试题 汇总(二)
51..net中读写XML的类都归属于哪些命名空间? 答:System.Xml 52.解释一下UDDI.WSDL的意义及其作用. 答:UDDI即统一描述.发现和集成协议.作用: 用来说明一个Web服务 ...
- ChipScope软件使用
内容组织 1.建立工程 2.插入及配置核 2.1运行Synthesize 2.2新建cdc文件 2.3 ILA核的配置 3. Implement and generate programmi ...