Prompt:

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

You may choose a single day to buy one NeetCoin and choose a different day in the future to sell it.

Return the maximum profit you can achieve. You may choose to not make any transactions, in which case the profit would be 0.

Example 1:

Input: prices = [10,1,5,6,7,1]
 
Output: 6

Explanation: Buy prices[1] and sell prices[4], profit = 7 - 1 = 6.

Example 2:

Input: prices = [10,8,7,5,2]
 
Output: 0

Explanation: No profitable transactions can be made, thus the max profit is 0.

Constraints:

  • 1 <= prices.length <= 100
  • 0 <= prices[i] <= 100

Personal Solution:

class Solution {
public:
    int maxProfit(vector<int>& prices) 
    {
        if(prices.size() == 0 || prices.size() == 1) return 0;
 
        int left = 0;
        int right = 0;
        int outPut = 0;
 
        while(right < prices.size() && left < prices.size())
        {
            
            if(prices[left] > prices[right])
            {
                //there is a better buy price
                left = right;
                //continue;
            }
            if (prices[left] < prices[right])
            {
                //update price
                int difference = prices[right] - prices[left];
                if(difference > outPut)
                {
                    outPut = difference;
                }
            }
 
            //always increment the right
            right++;
        }
        return outPut;
    }
};

Explanation:

So basically the way that my own answer works is that you start with two different pointers left and right that each start at the beginning of the array. You then basically increment the right one ALWAYS, and this is because each day you want to see if you have a better day of buying your stock. The thing is that you want to see if you can get the right value as high as possible and your left value as low as possible. So you iterate over with the right, and if you find a value that the right is pointing to, and it is lower than your current lowest, you make that your new lowest aka your left.

Then once you have that, you increment over again, and you check to see if the difference between your right aka largest val, and your left aka smallest val has changed at all. Doing this, you keep going basically until your right pointer reaches the end of the array, at which point you know you have the largest possible different.


Best Solution:

class Solution {
public:
    int maxProfit(vector<int>& prices) {
        //dp approach
        int day = INT_MAX;
        int ans = 0;
        for(int i = 0;i<prices.size();i++){
            day = min(day,prices[i]);
            ans = max(ans,prices[i] - day);
        }
        return ans;
    }
};

Explanation:

This one is actually pretty logical. Basically you start with the largest possible value for you day, aka technically how much the largest cost for you could be. You then compare this value with the current index of the array, and if you find a smaller value this becomes your new best buying point.

You then subtract this smallest buying point from the current same index that you are on. If the value is greater than zero, it becomes the new value. This allows you to iteratively check what the best value in place is.