Best Time to Buy and Sell Stock

Problem

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.

Constraints:

  • 1 <= prices.length <= 105
  • 0 <= prices[i] <= 104

Solution

I’m not really sure how this one is considered dynamic programming.

Anyway.

Keep track of the minimum price we’ve seen and the most profit we could have. Go through the array and check if we see a lower price. Update max profit as applicable.

class Solution {
    public int maxProfit(int[] prices) {
        var minPrice = prices[0];
        var maxProfit = 0;

        for (var i : prices) {
            minPrice = Math.min(i, minPrice);
            maxProfit = Math.max(i - minPrice, maxProfit);
        }

        return maxProfit;
    }
}

Recent posts from blogs that I like

AI is breaking our proxies for expertise

Mathematicians are broadly not anti-AI. They’re more culturally open to using AI as a tool than, say, artists or writers1. However, now that more and more genuinely prestigious problems have fallen to AI, that might be changing. Almost five thousand mathematicians (including twenty-five Fields medal...

via Sean Goedecke

Generating running routes with GPT-6 Astra and ChatGPT Work

via Simon Willison

Anarchy in paint: the politics of Neo-Impressionism 1

The close group of Neo-Impressionists were also anarchists. This traces their politics in paintings by Paul Signac and Camille Pissarro, including a portrait of Félix Fénéon, later suspected of bombing a restaurant.

via The Eclectic Light Company