class Solution { public: int maximumProfit(vector &prices) { int ans = 0; for (int i = 1; i < prices.size(); i++) { if (prices[i] > prices[i-1]) ans += prices[i] - prices[i-1]; } return ans; } };
from typing import List class Solution: def maximumProfit(self, prices) -> int: ans = 0 for i in range(1, len(prices)): if prices[i] > prices[i-1]: ans += prices[i] - prices[i-1] return ans
Table of Contents
0:00 Problem Statement
0:50 Solution
2:44 Optimised Solution
5:47 Pseudo Code
6:41 Code - Python
7:10 Code - C++
class Solution {
public:
int maximumProfit(vector &prices) {
int ans = 0;
for (int i = 1; i < prices.size(); i++) {
if (prices[i] > prices[i-1])
ans += prices[i] - prices[i-1];
}
return ans;
}
};
from typing import List
class Solution:
def maximumProfit(self, prices) -> int:
ans = 0
for i in range(1, len(prices)):
if prices[i] > prices[i-1]:
ans += prices[i] - prices[i-1]
return ans