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系列的更多相关文章

  1. 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 ...

  2. [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 ...

  3. [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 ...

  4. [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 ...

  5. [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 ...

  6. [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 ...

  7. 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 ...

  8. 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 ...

  9. [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 ...

随机推荐

  1. jsp传参 servlet接收中文乱码问题

    在公司实习了8个月,一直都是做android和h5的,但是发现做程序连一点服务都不会该怎么办,所以最近开始学起了java,不知道是不是因为框架学多了,现在看起springmvc框架比以前看起来简单太多 ...

  2. @staticmethod怎么用?

    早上起来写个小demo, 类中写了个方法, pycharm给这个方法加上了莫名其妙的波浪线, 对于一个有代码洁癖的人来说, 完全不能忍, 来看看为什么. 问题重现 pycharm的提示 上面说了, 这 ...

  3. C#在textBox中输出一个数组

    //将数组输出到文本框测试 for(i=0;i<arr.Length-1;i++){ this.textBox1.Text=this.textBox1.Text+arr[i]; }

  4. 微信小程序终于审核过了

    终于,我做的微信小程序审核结束了,虽然被退回来两次,但是第三次还是审核通过了! 加油骚年,相信自己!! 有什么问题可以评论告诉我!!

  5. Java基础——继承和多态

    面向对象的编程允许从已经存在的类中定义新的类,这称为继承. 面向过程的范式重点在于方法的设计,而面向对象的范式将数据和方法结合在对象中.面向对象范式的软件设计着重于对象以及对象上的操作.面向对象的方法 ...

  6. (数据科学学习手札01)Python与R基本数据结构之异同

    Python 1.列表(list) list1 = [i for i in range(10)] list1[0, 1, 2, 3, 4, 5, 6, 7, 8, 9] 特点:可遍历,可索引,可切片 ...

  7. DESCRIBEFIELD

    実行時データ型識別.略語は RTTI です.プログラム実行時にデータ型を識別して処理を行う仕組みです.. DESCRIBE FIELD命令を使用 DESCRIBE FIELD命令を使用して.変数のデー ...

  8. Android开发——告诉你Adapter应该写在Activity里面还是外面

    0. 前言 本文转载自AItsuki的博客. 首先说明一下为什么要写这么一篇博客:最近看了一些其他人的项目,发现很多项目的做法是建立一个专门存放Adapter类的Package包,也有的项目干脆直接都 ...

  9. c/c++ 数组传参

    在c/c++中,在进行数组传参时,数组的元素个数默认是不作为实参传入调用函数,也就是说c/c++ 不允许向函数传递一个完整的数组作为参数 实例: 1.形式参数是一个指针,实参包括数组长度: 1 voi ...

  10. Python的logging模块、os模块、commands模块与sys模块

    一.logging模块 import logging logging.debug('This is debug message') logging.info('This is info message ...