Best Time to Buy and Buy Stock

Source: Internet
Author: User

Best Time to Buy and Buy Stock

Problem 1: Best Time to Buy and 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 every one share of the stock), design an algorithm to find the maximum profit.
That is, the maximum benefit of one purchase and one sale.
Public int maxProfit (int [] prices) {assert prices. length> = 0; int min = Integer. MAX_VALUE; int max = 0; // find the smallest value. Save the largest for (int p: prices) {min = Math. min (min, p); max = Math. max (max, p-min);} return max ;}

Question 2: Best Time to Buy and 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 your transactions as you like (ie, buy one and every one share of the stock multiple times ). however, you may not engage in multiple transactions at the same time (ie, you must wait the stock before you buy again ).
The maximum benefit of buying and selling without limit for multiple times
public int maxProfit(int[] prices) {assert prices.length >= 0;int local = 0;int max = 0;for (int i = 1; i < prices.length; i++) {local = prices[i] - prices[i - 1];int x = local > 0 ? local : 0;max += x;}return max;}

Question 3: Best Time to Buy and Buy 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 encrypt the stock before you buy again ).
Maximum benefit of a maximum of two transactions.
Int [] left = new int [prices. length]; // records the maximum profit of prices [0. I]
Int [] right = new int [prices. length]; // records the maximum profit of prices [I .. n]
Finally, sum up to find the maximum benefit.

Public int maxProfit (int [] prices) {if (prices. length <2) return 0; int [] left = new int [prices. length]; // records prices [0 .. the maximum profitint [] right = new int [prices. length]; // records prices [I .. n] maximum profitint left_min = prices [0]; for (int I = 1; I <prices. length; I ++) {left_min = Math. min (left_min, prices [I]); left [I] = Math. max (prices [I]-left_min, left [I-1]);} int right_max = prices [prices. length-1]; for (int I = prices. length-2; I> = 0; I --) {right_max = Math. max (right_max, prices [I]); right [I] = Math. max (right_max-prices [I], right [I + 1]);} int max = 0; for (int I = 1; I <prices. length; I ++) {max = Math. max (max, left [I] + right [I]);} return max ;}

Question 4: Find the index of the array when the maximum stock sales returns is the date when the stock is bought and sold (actually the deformation of Problem 1)

In this case, a two-digit array can be defined to store the index for purchase and sale. When the global benefit changes, update the index.

Public int [] maxProfits (int [] prices) {assert prices. length> = 0; int min = Integer. MAX_VALUE; int [] res = new int [2]; // The final maximum int [] temp = new int [2]; // int max = 0; for (int I = 0; I <prices. length; I ++) {if (min> prices [I]) {min = prices [I]; temp [0] = I ;} if (max <prices [I]-min) {temp [1] = I; max = prices [I]-min; res = Arrays. copyOf (temp, 2) ;}} System. out. println (res [0] + "----" + res [1]); return res ;}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.