GithubHelp home page GithubHelp logo

snwlprdgthb / maxprofit Goto Github PK

View Code? Open in Web Editor NEW
1.0 1.0 0.0 2 KB

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.

JavaScript 100.00%

maxprofit's Introduction

Stock Trading Max Profit Calculator

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.

Problem Statement

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.

Function Specification

The function maxProfitK(prices, k) takes two parameters:

  • prices: An array of integers where prices[i] denotes the price of the stock on the i-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.

Example

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).

Usage

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}`);

maxprofit's People

Contributors

snwlprdgthb avatar

Stargazers

Aslan Alaev avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.