Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /***
- *Say you have an array for which the ith element is the price of a given stock on day i.
- *Design an algorithm to find the maximum profit. You may complete as many transactions as you like (i.e., buy one and sell one share of *the stock multiple times).
- *Note: You may not engage in multiple transactions at the same time (i.e., you must sell the stock before you buy again).
- ***/
- class Solution {
- public int maxProfit(int[] prices) {
- if(prices.length == 0){
- return 0;
- }
- int maxProfit = 0;
- int min = prices[0];
- for(int i = 1; i < prices.length; i++) {
- min = Math.min(prices[i], min);
- maxProfit = Math.max(maxProfit, prices[i] - min);
- }
- return maxProfit;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement