Day 5 of 30-Day LeetCode Challenge

Say you have an array prices 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 {
int maxProfit(vector<int>& prices) {
int result = 0;
for(int i=0;i<prices.size();++i){
int num = prices[i];
int j = i+1;
num = prices[j];

result += prices[j-1]-prices[i];
i = j-1;
return result;




We never really grow up, we only learn how to act in public

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store