Git Product home page Git Product logo

selling-stock's Introduction

DSA TWO

Best Time to Buy and Sell Stock

Given an array prices[] of length N, representing the prices of the stocks on different days. The task is to find the maximum profit possible by buying and selling the stocks on different days when at most one transaction is allowed.

Note: Stock must be bought before being sold

   Input: prices[] = {7, 1, 5, 3, 6, 4}
   Output: 5
   Explanation:
   The lowest price of the stock is on the 2nd day, i.e. price = 1. Starting from the 2nd day, 
   the highest price of the stock is witnessed on the 5th day, i.e. price = 6. 
   Therefore, maximum possible profit = 6 โ€“ 1 = 5. 

   Input: prices[] = {7, 6, 4, 3, 1} 
   Output: 0
   Explanation: Since the array is in decreasing order, no possible way exists to solve the problem.

Implementing solution using Greedy Approach

    To maximize profit it is important that the cost is lowered down, so minimize cost price and then maximize selling price.

    At every step, we will keep track of the minimum buying price of stock encountered so far. Then if the current price of 
    the stock is less than the previous buying price, we will update the buy price, else if the current price of stock is
    greater than the previous buying price, we can sell at this price ot get some profit.

    After iterating over the entire array, return the maximum profit.

Steps to solve the problem

  • Declare a buy variable to store the minimum stock price encountered so far and max_profit to store the maximum profit.
  • Initialize the buy variable to the first element of the prices array.
  • Iterate over the prices array and check if the current price is less than the buy price or not.
    • If the current price is smaller than buy price, then buy on this ith day.
    • If the current price is greater than buy price, then make profit from it and maximize the max_profit
  • Finally, return the max_profit.
    /**
     * Calculates the maximum profit that can be obtained by buying and selling stocks.
     * @param {number[]} prices - Array representing the prices of stocks on different days.
     * @returns {number} - Maximum profit achievable.
     */
    
    function maxProfit(prices) {
        if (prices.length < 2) {
            return 0; // Edge Case: Cannot make a profit with less than two prices.
        }
   
        // Initialize variables
        let buy = prices[0]; // The initial buying price
        let max_profit = 0; // The maximum profit obtained
   
        // Iterate through the prices array starting from the second element
        for (let i = 1; i < prices.length; i++) {
            // Update buying price if the current price is lower
            if (buy > prices[i]) {
                buy = prices[i];
            }
            // Update maximum profit if selling at the current price results in a higher profit
            else if (prices[i] - buy > max_profit) {
                max_profit = prices[i] - buy;
            }
        }
   
        return max_profit
    }
  • Time Complexity: O(2^n), where n is the size of input array prices[]
  • Auxiliary Space: O(N)

Implementation using Dynamic Programming

    The recursive approach can be optimized by storing the states in a 2D dp array of size NX2.
    Here, dp[idx][0] will store the answer of maxProfit[idx, false] and dp[idx][1] will store the
    the answer of maxProfit[idx, true].
    /**
     * Calculates the maximum profit that can be obtained by buying and selling stocks using dynamic programming.
     * @param {number[]} prices - Array representing the prices of stocks on different days.
     * @returns {number} - Maximum profit achievable.
     */

    function maxProfitDP(prices) {
        //Edge case: Cannot make a profit with less than two prices
        if (prices.length < 2) {
            return 0;
        }

        const n = prices.length;
        // 2D DP array to store max profit with 0 and 1 stocks
        const dp = new Array(n).fill(null).map(() => [0, 0]);

        // Initialize variables
        let buy = -prices[0];
        let sell = 0;

        // Loop through prices to calculate max profit at each day
        for (let i = 1; i < n; i++) {
            // Update but to be the maximum between the previous buy and the profit from buying at prices[i]
            buy = Math.max(buy, -prices[i]);
            
            // Update sell to be the maximum between the previous sell and the profit form selling at prices[i]
            sell = Math.max(sell, buy + prices[i]);
        }

        // Return the maximum profit calculated from the last day
        return Math.max(buy, sell);
    }

    // Given Prices
    const prices = [7, 1, 5, 3, 6, 4];
    
    // Function Call
    const ans = max.ProfitDP(prices);

    // Print answer
    console.log(ans);
  • Time Complexity: O(N), where N is the length of the given array.
  • Auxiliary Space: O(N)

selling-stock's People

Contributors

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