This repository contains a Node.js script designed to solve a specific problem related to stock trading. The task is to calculate the maximum profit achievable by completing at most k
transactions based on given stock prices over a period of days.
You are given an array prices
of integers representing the stock prices for each day over a period of n
days. The goal is to write a Node.js function maxProfitK(prices, k)
that computes the maximum profit possible with at most k
transactions.
The function maxProfitK(prices, k)
takes two parameters:
prices
: An array of integers whereprices[i]
denotes the price of the stock on thei
-th day.k
: An integer representing the maximum number of transactions allowed.
It returns:
- An integer representing the maximum profit achievable by completing at most
k
transactions.
For example, given prices = [3, 2, 6, 5, 0, 3]
and k = 2
, the function should return 7
, which is the maximum profit that can be obtained by buying at day 1 (prices[1] = 2
) and selling at day 2 (prices[2] = 6
), and then buying at day 4 (prices[4] = 0
) and selling at day 5 (prices[5] = 3
).
To use the maxProfitK
function, simply call it with an array of stock prices and the maximum number of transactions allowed. Ensure Node.js is installed on your machine.
const maxProfitK = require('./maxProfitK'); // Replace with your actual file path or npm package name
const prices = [3, 2, 6, 5, 0, 3];
const k = 2;
const maxProfit = maxProfitK(prices, k);
console.log(`Maximum profit with at most ${k} transactions: ${maxProfit}`);