GithubHelp home page GithubHelp logo

subgiu / max-profit-stock-dynamic-programming Goto Github PK

View Code? Open in Web Editor NEW
0.0 1.0 0.0 5 KB

The stock has a price each day, what's the max profit is it possible to gain buying and selling a stock? It shows a solution with O(n) as time complexity.

Java 100.00%
dynamic-programming timecomplexity

max-profit-stock-dynamic-programming's Introduction

The maximum profit selling stock using dynamic programming

The stock has a price each day, what's the max profit is it possible to gain buying and selling the stock?
It shows a solution with O(n) as time complexity. It is implemented in Java.

You have an array in which the i'th element is the price of a stock on day 'i'. What is the maximum profit you can gain doing at most 1 transaction (buy once and sell once)?
Note that you cannot sell a stock before you buy one.

Example: array = [8 , 1 , 2 , 4 , 6 , 3]
                             0   1   2   3   4   5

The maximum profit is 5. If you buy on day 1 = 1 and sell on day 4 = 6.

A possible approach the would come to mind is to calculate for all elements combination and keep the highest profit.

Pseudo Code:

var highestProfit: 0;
For i = 0 to array.length
  j = i + 1;
  For j to array.length
    highestProfit = max(highestProfit, array[j] - array[i]);

We will get a time complexity like O(n ^ 2).

How to get a solution with O(n) as time complexity?
It is possible to use dynamic programming, identifying the logic that is possible to reuse. Let's take a look:

Some possibilities:

  1. Buy on day 0=8 and sell on day 2=2 getting a loss of 6 units.
  2. Buy on day 1=1 and sell on day 3=4 getting a profit of 3 units.
  3. Buy on day 4=6 and sell on day 5=3 getting a loss of 3 units.
  4. Buy on day 1=1 and sell on day 4=6 getting a profit of 5 units.

Important observations:

  • It going to have a profit just if the buying price is lower than the selling price, in array words, the "i" element must be greater than the previous "i". Like in possibilities 2 and 4.
  • The maximum profit is related to buy with the minimum price and sell it someday. In array words, it means that the profit related to "i" is the profit between "i" and the minimum price before i. Like in the possibility 4.

Pseudo Code:

var maxProfit = N/A; //Start with the minimum possible value
var minPrice = array[0];

For i = 1 to array.length
   minPrice = min(minPrice, array[i]); //Keep the minimum price until i
   if(prices[i] - minPrice > maxProfit)
       maxProfit = prices[i] - minPrice;

max-profit-stock-dynamic-programming's People

Contributors

subgiu avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.