122. Best Time to Buy and Sell Stock II
MediumSolutions in Python, Java, C++, TypeScript
Read the full problem statement on LeetCode and try it first. Below is how to approach it in an interview, followed by a complete answer in each language.
Approach
The problem asks for the maximum profit from buying and selling a stock. We are given an array of stock prices for consecutive days. The key rules are:
Solution
Best Time to Buy and Sell Stock II in Python
class Solution:
def maxProfit(self, prices: List[int]) -> int:
total_profit = 0
# Iterate through the prices starting from the second day.
for i in range(1, len(prices)):
# If the current day's price is higher than the previous day's price,
# we can make a profit.
if prices[i] > prices[i-1]:
# We add this profit to our total. This strategy works because
# we can capture profit from every single day's price increase.
# A single large transaction from a valley to a peak can be decomposed
# into a series of day-to-day profitable transactions.
total_profit += prices[i] - prices[i-1]
return total_profit
Best Time to Buy and Sell Stock II in Java
class Solution {
public int maxProfit(int[] prices) {
int totalProfit = 0;
// Start from the second day (index 1) to compare with the previous day.
for (int i = 1; i < prices.length; i++) {
// Check if we can make a profit by selling today.
// This is possible if today's price is greater than yesterday's price.
if (prices[i] > prices[i - 1]) {
// Add the profit from this single-day transaction.
// The problem allows unlimited transactions, and we can buy and sell on the same day.
// This means we can accumulate profits from all consecutive price increases.
totalProfit += prices[i] - prices[i - 1];
}
}
return totalProfit;
}
}
Best Time to Buy and Sell Stock II in C++
class Solution {
public:
int maxProfit(vector<int>& prices) {
int totalProfit = 0;
// Iterate through the prices starting from the second day (index 1).
for (int i = 1; i < prices.size(); i++) {
// If the price on the current day is higher than the previous day,
// we can make a profit.
if (prices[i] > prices[i-1]) {
// We add the difference to our total profit. This is equivalent to
// buying on day i-1 and selling on day i.
// Since we can do this for every consecutive day where the price increases,
// we can capture all upward trends.
totalProfit += prices[i] - prices[i-1];
}
}
return totalProfit;
}
};
Best Time to Buy and Sell Stock II in TypeScript
function maxProfit(prices: number[]): number {
let totalProfit = 0;
// Iterate from the second day to the end of the array.
for (let i = 1; i < prices.length; i++) {
// If the price on day i is greater than the price on day i-1,
// it represents a profitable transaction.
if (prices[i] > prices[i-1]) {
// We can think of this as buying on day i-1 and selling on day i.
// Since we can perform as many transactions as we like, we can
// accumulate the profit from every positive price change between
// consecutive days.
totalProfit += prices[i] - prices[i-1];
}
}
return totalProfit;
};
More Medium LeetCode answers
Preparing for interviews beyond LeetCode? Practise system design, SQL and behavioral questions with an in-browser runner on KatInterview.
LeetCode is a trademark of LeetCode LLC. KatInterview is not affiliated with LeetCode.