Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * One Pass.
- *
- * Time Complexity: O(N)
- *
- * Space Complexity: O(1)
- *
- * N = Length of prices array.
- */
- class Solution {
- public int maxProfit(int[] prices) {
- if (prices == null || prices.length <= 1) {
- return 0;
- }
- int totalProfit = 0;
- for (int i = 1; i < prices.length; i++) {
- if (prices[i - 1] < prices[i]) {
- totalProfit += prices[i] - prices[i - 1];
- }
- }
- return totalProfit;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement