Tap here to switch tabs
Problem
Submissions

Problem: Best Time to Buy and Sell Stock V

Try to solve the Best Time to Buy and Sell Stock V problem.

Statement

You are given an integer array prices where prices[i] is the price of a given stock on day i, and an integer k.

You may complete at most k transactions. Each transaction can be one of the following two types:

  1. Normal transaction: Buy the stock on day i and sell it on day j with i < j, achieving a profit of prices[j] −- prices[i].

  2. Short selling transaction: Sell the stock on day i and buy it back on day j with i < j, achieving a profit of prices[i] −- prices[j].

You cannot engage in multiple transactions simultaneously. That is, you must finish your current transaction before initiating a new one.

Return the maximum profit you can achieve.

Constraints:

  • 2≤2 \leq prices.length ≤103\leq 10^3

  • 1≤1 \leq prices[i] ≤109\leq 10^9

  • 1≤1 \leq k ≤\leq prices.length /2/ 2

⋮
Tap here to switch tabs
Problem
Submissions

Problem: Best Time to Buy and Sell Stock V

Try to solve the Best Time to Buy and Sell Stock V problem.

Statement

You are given an integer array prices where prices[i] is the price of a given stock on day i, and an integer k.

You may complete at most k transactions. Each transaction can be one of the following two types:

  1. Normal transaction: Buy the stock on day i and sell it on day j with i < j, achieving a profit of prices[j] −- prices[i].

  2. Short selling transaction: Sell the stock on day i and buy it back on day j with i < j, achieving a profit of prices[i] −- prices[j].

You cannot engage in multiple transactions simultaneously. That is, you must finish your current transaction before initiating a new one.

Return the maximum profit you can achieve.

Constraints:

  • 2≤2 \leq prices.length ≤103\leq 10^3

  • 1≤1 \leq prices[i] ≤109\leq 10^9

  • 1≤1 \leq k ≤\leq prices.length /2/ 2