0
0
DSA Typescriptprogramming~30 mins

Buy and Sell Stocks All Variants in DSA Typescript - Build from Scratch

Choose your learning style9 modes available
Buy and Sell Stocks All Variants
📖 Scenario: You are working as a financial analyst assistant. You have daily stock prices and want to find the best ways to buy and sell stocks to maximize profit. This project will guide you through different variants of the 'Buy and Sell Stocks' problem.
🎯 Goal: Build a TypeScript program that calculates maximum profit from stock prices using different buying and selling rules.
📋 What You'll Learn
Create an array called prices with exact daily stock prices
Create a variable called maxTransactions to limit the number of buy-sell pairs
Write a function maxProfitOneTransaction to find max profit with only one buy and one sell
Write a function maxProfitUnlimitedTransactions to find max profit with unlimited transactions
Write a function maxProfitKTransactions to find max profit with at most maxTransactions transactions
Print the results of all three functions
💡 Why This Matters
🌍 Real World
Stock traders and financial analysts use these algorithms to decide when to buy and sell stocks to maximize profit.
💼 Career
Understanding these variants helps in roles like quantitative analyst, financial software developer, and data scientist working with market data.
Progress0 / 4 steps
1
Create the stock prices array
Create an array called prices with these exact values: [7, 1, 5, 3, 6, 4]
DSA Typescript
Hint

Use const prices: number[] = [7, 1, 5, 3, 6, 4]; to create the array.

2
Set the maximum number of transactions
Create a variable called maxTransactions and set it to 2
DSA Typescript
Hint

Use const maxTransactions: number = 2; to set the limit.

3
Write functions for different buy-sell variants
Write three functions: maxProfitOneTransaction(prices: number[]): number to find max profit with one transaction, maxProfitUnlimitedTransactions(prices: number[]): number for unlimited transactions, and maxProfitKTransactions(prices: number[], k: number): number for at most k transactions. Use the variable names exactly as given.
DSA Typescript
Hint

Use loops and variables exactly as shown to calculate profits for each variant.

4
Print the results of all three functions
Print the results of maxProfitOneTransaction(prices), maxProfitUnlimitedTransactions(prices), and maxProfitKTransactions(prices, maxTransactions) using console.log with exact labels: "Max profit with one transaction:", "Max profit with unlimited transactions:", and "Max profit with at most 2 transactions:" respectively.
DSA Typescript
Hint

Use console.log with exact text and call the functions with correct arguments.