Advertisement
Guest User

Untitled

a guest
Feb 6th, 2022
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.41 KB | None | 0 0
  1. public class Solution {
  2.     public int maxProfit(int prices[]) {
  3.         int minprice = Integer.MAX_VALUE;
  4.         int maxprofit = 0;
  5.         for (int i = 0; i < prices.length; i++) {
  6.             if (prices[i] < minprice)
  7.                 minprice = prices[i];
  8.             else if (prices[i] - minprice > maxprofit)
  9.                 maxprofit = prices[i] - minprice;
  10.         }
  11.         return maxprofit;
  12.     }
  13. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement