LoginSignup
0
0

More than 1 year has passed since last update.

Leetcode 121. Best Time to Buy and Sell Stock

Posted at

class Solution {
    public int maxProfit(int[] prices) {
        int minimal = Integer.MAX_VALUE;
        int result = 0;
        for (int i = 0; i < prices.length; i++) {
            if (prices[i] < minimal) {
                minimal = prices[i];
                continue;
            }
            result = Math.max(prices[i] - minimal, result);
        }
        return result;
    }
}
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0