Coder Social home page Coder Social logo

stockadvisor's Introduction

StockPrices (aka Maximum Profit)

Maximum Profit

Context:

So I was tasked to write an algorithm that when given an array (or list, for this case) it would identify when the stock value is low enough to buy and then, high enough to sell. By doing do, then we would get the "maximum profit" value.

It also looks like this is an exercise that examiners like to throw in a coding interview so, I thought that sharing might help someone along the way.

Finally, this is no real time analysys solution. For that, I can not think of a possible solution that would not include local min/max and/or ML. But then again, this is a logic exercise.

Possible solution:

The function maxiStockProfit takes an input (e.g.: a daily list of a stock value per hour). It will return the biggest difference between the lowest and the highest value in the list (buying low and selling high).

The algorithm works iterating over a nested for loop. All values in the sequence "i" (the buying points) go over all indexes j>i, (the selling points). For each buying/selling pair (i,j), we calculate the difference (profit) between the prices at the selling and the buying points. list[j]-list[i].

Reference output for the 3 three sample lists will be:

  • values where buy: $2 and sell $11 --> Max Profit: 9
  • values where buy: $9 and sell $10 --> Max Profit: 1
  • values where buy: $45 and sell $460 --> Max Profit: 415

Hope it helps! ๐Ÿ˜Ž.

stockadvisor's People

Contributors

elsauto avatar

Watchers

James Cloos avatar  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.