FastPrepBest Time to Buy and Sell Stock II
Problems/Best Time to Buy and Sell Stock II

Best Time to Buy and Sell Stock II

Groww logoGroww● EasyINTERNONSITE INTERVIEW
QuestionFunctionExamplesConstraints

Problem statement

Source note: An interview report names the unlimited-transactions stock-profit task. This practice contract uses nonnegative daily prices, permits any number of nonoverlapping transactions, and allows holding at most one share at a time.

Given daily prices, return the maximum profit obtainable by any number of buy-then-sell transactions while holding at most one share at a time.

Function

maxProfit(prices: int[]) → int

Examples

Example 1

prices = [7,1,5,3,6,4]return = 7

Take the rises from one to five and from three to six.

Example 2

prices = [1,2,3,4,5]return = 4

The entire increasing run yields four.

Constraints

  • 0 <= prices.length <= 100000
  • 0 <= prices[i] <= 100000

More Groww problems

  • Maximum Sum Desirable SubarrayPHONE SCREEN · Seen Oct 2026
  • Sliding Window MaximumPHONE SCREEN · Seen Sep 2026
  • Minimum Candies by RatingONSITE INTERVIEW · Seen Sep 2026
See Groww hiring insights
Saved
class Solution {
    public int maxProfit(int[] prices) {
      // Write your code here.
    }

}
Checking account…