Best Time to Buy and Sell Stock I II III
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.
创建差分数组,即差分数组grid[i] = price[i+1]-price[i],因此,本题可以转化为求grid中最大子数组和问题
public int maxProfit(int[] prices) {
if(prices.length<=1){
return 0;
}
int minus[] = new int[prices.length-1];
for(int i=0;i<minus.length;i++){
minus[i] = prices[i+1]-prices[i];
}
int max = minus[0];
int now = minus[0];
for(int i=1;i<minus.length;i++){
if(minus[i]+now <minus[i]){
now = minus[i];
}else{
now = minus[i]+now;
}
if(max<now){
max = now;
}
}
return max>0?max:0;
}
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 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 int maxProfit(int[] prices) {
if(prices.length<=1){
return 0;
}
int max = 0;
for(int i=0;i<prices.length-1;i++){
int minus = prices[i+1]-prices[i];
max+=minus>0?minus:0;
}
return max;
}
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).
动态规划,用两个数组记录状态,f[i]表示区间[0,i](0<=i<=n-1) 的最大利润,g[i]表示区间[i,n-1](0<=i<=n-1) 的最大利润。
public int maxProfit(int[] prices) {
if (prices.length < 2)
return 0;
int f[] = new int[prices.length];
int g[] = new int[prices.length];
for (int i = 1, valley = prices[0]; i < prices.length; ++i) {
valley = Math.min(valley, prices[i]);
f[i] = Math.max(f[i - 1], prices[i] - valley);
}
for (int i = prices.length - 2, peak = prices[prices.length - 1]; i >= 0; --i) {
peak = Math.max(peak, prices[i]);
g[i] = Math.max(g[i], peak - prices[i]);
}
int max_profit = 0;
for (int i = 0; i < prices.length; ++i)
max_profit = Math.max(max_profit, f[i] + g[i]);
return max_profit;
}
Best Time to Buy and Sell Stock I II III的更多相关文章
- Best Time to Buy and Sell Stock I && II && III
题目1:Best Time to Buy and Sell Stock Say you have an array for which the ith element is the price of ...
- [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 ...
- [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 ...
- [LintCode] 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 (股票买卖时机问题2)
问题: Say you have an array for which the ith element is the price of a given stock on day i. Design a ...
- 123. Best Time to Buy and Sell Stock (三) leetcode解题笔记
123. Best Time to Buy and Sell Stock III Say you have an array for which the ith element is the pric ...
随机推荐
- UML--核心元素之包
包是一种容器,如同文件夹一样. 包是UML非常常用的一个元素,它最主要的作用就是容纳并为其他元素分类.包可以容纳用例.业务实体.类图等,也包含子包. 分包的原则 1.如果将元素分为三个包A.B.C,那 ...
- 移植strace调试工具到arm平台
strace工具是一个非常强大的工具,是调试程序的好工具.要移植到arm平台,就需要使用交叉编译工具编译生成静态链接的可执行文件.具体步骤如下:1.下载 strace-4.5.16 移植str ...
- Textarea - 百度富文本编辑器插件UEditor
UEditor各种实例演示 Ueditor 是百度推出的一款开源在线 HTML 编辑器. 主要特点: 轻量级:代码精简,加载迅速. 定制化:全新的分层理念,满足多元化的需求.采用三层架构:1. 核心层 ...
- tabbar 嵌套 navigation
-------------- 源代码:点击打开链接 ------------------------ AppDelegate.m - (BOOL)application:(UIApplication ...
- SQL 查找 45道练习题
一. 设有一数据库,包括四个表:学生表(Student).课程表(Course).成绩表(Score)以及教师信息表(Teacher).四个表的结构分别如表1-1的表(一)~表( ...
- poj 1017 Packets 贪心
题意:所有货物的高度一样,且其底面积只有六种,分别为1*1 2*2 3*3 4*4 5*5 6*6的,货物的个数依次为p1,p2,p3,p4,p5,p6, 包裹的高度与货物一样,且底面积就为6*6,然 ...
- PHP计算一个目录文件大小方法
<?php $dirfile='../hnb'; /** *计算一个目录文件大小方法 *$dirfile:传入文件目录名 **/ function dirSize($dirfile) { $di ...
- redmine fastcgi常常崩溃的解决方式
最终找到了解决方法,在以下的文件里加入两行就可以: /home/redmine/redmine-2.5.1/public/dispatch.fcgi require 'rubygems' requir ...
- [Git] --no-verify
Somtimes, the project might set the commit message guide line, if your commit doesn't meet the requi ...
- qt超强精美绘图控件 - QCustomPlot一览 及 安装使用教程
1.概述 QCustomPlot 是一个超强超小巧的qt绘图类,非常漂亮,非常易用,只需要加入一个qcustomplot.h和qcustomplot.cpp文件即可使用,远比qwt方便和漂亮,可以自己 ...