Advertisement
exmkg

1-1

Jan 14th, 2025 (edited)
38
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.40 KB | None | 0 0
  1. class Solution {
  2.     public int maxProfit(int[] prices) {
  3.         int lowest_price_so_far = prices[0];
  4.         int highest_profit = 0;
  5.         for (int todays_price : prices) {
  6.             highest_profit = Math.max(highest_profit, todays_price - lowest_price_so_far);
  7.             lowest_price_so_far = Math.min(lowest_price_so_far, todays_price);
  8.         }
  9.         return highest_profit;
  10.     }
  11. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement