Maximize Repeated One-Share Stock Trading Profit

Quick Overview

Maximize unlimited one-share trading profit by summing positive daily gains under the original same-day rules.

Maximize Repeated One-Share Stock Trading Profit

Company: Google

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Other

### Problem Find the maximum total profit from trading one stock across the daily prices. You may make as many purchases and sales as useful, but may hold at most one share at any moment. Buying and selling, including repeated sell/buy operations, are allowed on the same day while respecting that holding limit. You may also make no transactions. ```cpp int maxProfit(vector<int>& prices); ``` Return the maximum profit, rather than a transaction schedule. ### Examples ```text Input: prices = [9,2,8,1,6] Output: 11 ``` Buy at 2 and sell at 8, then buy at 1 and sell at 6: profits 6 and 5. ```text Input: prices = [2,4,7,7,9] Output: 7 ``` Holding from price 2 to price 9 achieves the same profit as collecting its positive daily increases. ### Constraints - There are between 1 and 30000 daily prices. - Every price lies between 0 and 10000, inclusive. - Never hold more than one share, including during same-day operations.

Overview: Maximize unlimited one-share trading profit by summing positive daily gains under the original same-day rules.

Community answers

Answer by Coderka14

#include using namespace std; class Solution { public: int maxProfit(vector& prices) { int profit = 0; int n = prices.size(); for (int i = 1; i < n; i++) { if (prices[i] > prices[i - 1]) { profit += prices[i] - prices[i - 1]; } } return profit; } };
Google logo
Google
Date not reported
mediumSoftware EngineerOtherCoding & Algorithms
0
0

Problem

Find the maximum total profit from trading one stock across the daily prices. You may make as many purchases and sales as useful, but may hold at most one share at any moment. Buying and selling, including repeated sell/buy operations, are allowed on the same day while respecting that holding limit. You may also make no transactions.

int maxProfit(vector<int>& prices);

Return the maximum profit, rather than a transaction schedule.

Examples

Input: prices = [9,2,8,1,6]
Output: 11

Buy at 2 and sell at 8, then buy at 1 and sell at 6: profits 6 and 5.

Input: prices = [2,4,7,7,9]
Output: 7

Holding from price 2 to price 9 achieves the same profit as collecting its positive daily increases.

Constraints

  • There are between 1 and 30000 daily prices.
  • Every price lies between 0 and 10000, inclusive.
  • Never hold more than one share, including during same-day operations.