Best Time to Buy and Sell Stocks - Leetcode

Best Time to Buy and Sell Stocks - Leetcode

ยท

3 min read

Introduction

Grind75 is a curated list of Algorithmic questions from LeetCode that have been very famous from quite a long time. These question have been asked in top tech companies such as Google, Microsoft, Amazon, Facebook, Netflix, Apple, and other top tier companies as well. This series will cover all the question from the list with explanation and code, so you can follow it up.

Question

You are given an array prices where prices[i] is the price of a given stock on the ith day.

You want to maximize your profit by choosing a single day to buy one stock and choosing a different day in the future to sell that stock.

Return the maximum profit you can achieve from this transaction. If you cannot achieve any profit, return 0.

Example 1:

Input: prices = [7,1,5,3,6,4]
Output: 5
Explanation: Buy on day 2 (price = 1) and sell on day 5 (price = 6), profit = 6-1 = 5.
Note that buying on day 2 and selling on day 1 is not allowed because you must buy before you sell.

Example 2:

Input: prices = [7,6,4,3,1]
Output: 0
Explanation: In this case, no transactions are done and the max profit = 0.
Try Solving the problem yourself Before seeing solution

Explanation

Given question is asking for maximum profit, how to get maximum profit ? we need to buy on the day when the prices are least and sell on another day when prices are max. Buy low sell High Easy Peasy profit ๐Ÿค‘.

Scam-1992.jpg

Brute Force Solution -

  • check for all possible combinations.

  • for every element in the array, we need to check for every other element in the array from that element onwards.

Code -

public int maxProfit(int[] prices) {
     int max = 0;
     for(int i = 0; i < prices.length; i++) {
         for(int j = i+1; j < prices.length; j++) {
             int temp = prices[j] - prices[i];
             max = Math.max(temp,max);
         }
     }
     return max;
}

Time complexity -O(N^2)

Space complexity - O(1)

Tabulation - Bottom up DP approach -

  • This can be solved using dynamic programming.

  • We'll use 1D array to store the maximum profit obtained after selling.

  • And maintain a min variable which'll store the minimum value

Code -

public int maxProfit(int[] prices) {
    int n = prices.length;
    int[] dp = new int[n];
    int min = prices[0];
    for(int i = 1; i < n; i++) {
        min = Math.min(min, prices[i]);
        dp[i] = Math.max(dp[i-1], prices[i] - min);
    }
    return dp[n-1];
}

Time complexity - O(N)

Space complexity - O(N)

Kadane's Algorithm -

  • Kadane's Algorithm follows the same steps as the dp solution but here we won't use any extra space.

  • Instead we use profit variable to store the profit, every-time the current profit is more than previous profit, we update our profit variable.

kadane's algorithm.jpg

Code -

public int maxProfit(int[] prices) {
    int min = prices[0];
    int profit = 0;
    for(int i = 1; i < prices.length; i++) {
        min = Math.min(min, prices[i]);
        profit = Math.max(profit, prices[i] - min);
    }
    return profit;
}

Time complexity - O(N)

Space complexity - O(1)

Did you find this article valuable?

Support Aryan Srivastava by becoming a sponsor. Any amount is appreciated!

ย