Best Time to Buy and Sell Stock II In C,CPP,JAVA,PYTHON,C#,JS
Problem Statement: Best Time to Buy and Sell Stock II You are given an integer array prices where prices[i] is the price of a given stock on the i-th day. You can buy and sell the stock as many times as you like (i.e., multiple transactions are allowed). However, you must sell the stock before you buy again. Return the maximum profit you can achieve. Example: Approach In this case, multiple transactions are allowed. A straightforward solution is to buy and sell the stock whenever there is a price increase, since each increase provides an opportunity for profit. Code Implementation C: #include <stdio.h>int maxProfit(int* prices, int pricesSize) { int max_profit = 0; for (int i = 1; i < pricesSize; i++) { if (prices[i] > prices[i – 1]) { max_profit += prices[i] – prices[i – 1]; // add the profit from price increase } } return max_profit;}int main() { int prices[] = {7, 1, 5, 3, 6, 4}; int pricesSize = sizeof(prices) / sizeof(prices[0]); printf(“Max profit: %d\n”, maxProfit(prices, pricesSize)); // Output: 7 return 0;} C++: #include <iostream>#include <vector>using namespace std;int maxProfit(vector<int>& prices) { int max_profit = 0; for (int i = 1; i < prices.size(); i++) { if (prices[i] > prices[i – 1]) { max_profit += prices[i] – prices[i – 1]; // add the profit from price increase } } return max_profit;}int main() { vector<int> prices = {7, 1, 5, 3, 6, 4}; cout << “Max profit: ” << maxProfit(prices) << endl; // Output: 7 return 0;} Java: public class BestTimeToBuyAndSellStockII { public static int maxProfit(int[] prices) { int maxProfit = 0; for (int i = 1; i < prices.length; i++) { if (prices[i] > prices[i – 1]) { maxProfit += prices[i] – prices[i – 1]; // add the profit from price increase } } return maxProfit; } public static void main(String[] args) { int[] prices = {7, 1, 5, 3, 6, 4}; System.out.println(“Max profit: ” + maxProfit(prices)); // Output: 7 }} Python: def maxProfit(prices): max_profit = 0 for i in range(1, len(prices)): if prices[i] > prices[i – 1]: max_profit += prices[i] – prices[i – 1] # add the profit from price increase return max_profit# Example usageprices = [7, 1, 5, 3, 6, 4]print(“Max profit:”, maxProfit(prices)) # Output: 7 C#: using System;public class BestTimeToBuyAndSellStockII { public static int MaxProfit(int[] prices) { int maxProfit = 0; for (int i = 1; i < prices.Length; i++) { if (prices[i] > prices[i – 1]) { maxProfit += prices[i] – prices[i – 1]; // add the profit from price increase } } return maxProfit; } static void Main() { int[] prices = {7, 1, 5, 3, 6, 4}; Console.WriteLine(“Max profit: ” + MaxProfit(prices)); // Output: 7 }} JavaScript: function maxProfit(prices) { let maxProfit = 0; for (let i = 1; i < prices.length; i++) { if (prices[i] > prices[i – 1]) { maxProfit += prices[i] – prices[i – 1]; // add the profit from price increase } } return maxProfit;}// Example usagelet prices = [7, 1, 5, 3, 6, 4];console.log(“Max profit:”, maxProfit(prices)); // Output: 7 Explanation: Summary: This problem can be efficiently solved using a greedy approach, where we accumulate profits from each price increase. This approach runs in O(n) time and uses O(1) space, making it optimal for this problem. It works well when multiple transactions are allowed, as we can capture all upward price movements.
Best Time to Buy and Sell Stock II In C,CPP,JAVA,PYTHON,C#,JS Read More »