Below is the implementation of the stock profit maximizer:
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495/** * Find the maximum profit from a single buy and sell transaction. * One-Pass approach. * * @param {number[]} prices - Array of stock prices * @return {number} - Maximum profit */function maxProfit(prices) { if (prices.length < 2) { return 0; } let minPrice = Infinity; let maxProfit = 0; for (let i = 0; i < prices.length; i++) { // Update the minimum price seen so far minPrice = Math.min(minPrice, prices[i]); // Calculate potential profit if we sell at current price const currentProfit = prices[i] - minPrice; // Update maximum profit if current profit is higher maxProfit = Math.max(maxProfit, currentProfit); } return maxProfit;} /** * Find the maximum profit from a single buy and sell transaction. * Kadane's Algorithm approach. * * @param {number[]} prices - Array of stock prices * @return {number} - Maximum profit */function maxProfitKadane(prices) { if (prices.length < 2) { return 0; } let maxCurr = 0; let maxSoFar = 0; for (let i = 1; i < prices.length; i++) { // Calculate price difference const diff = prices[i] - prices[i - 1]; // Update maximum profit ending at current position maxCurr = Math.max(0, maxCurr + diff); // Update maximum profit found so far maxSoFar = Math.max(maxSoFar, maxCurr); } return maxSoFar;} /** * Find the maximum profit from a single buy and sell transaction. * Brute Force approach. * * @param {number[]} prices - Array of stock prices * @return {number} - Maximum profit */function maxProfitBruteForce(prices) { if (prices.length < 2) { return 0; } let maxProfit = 0; // Consider all possible buy and sell days for (let i = 0; i < prices.length - 1; i++) { for (let j = i + 1; j < prices.length; j++) { // Calculate profit for this buy-sell pair const profit = prices[j] - prices[i]; // Update maximum profit if current profit is higher maxProfit = Math.max(maxProfit, profit); } } return maxProfit;} // Test casesconsole.log(maxProfit([7, 1, 5, 3, 6, 4])); // 5console.log(maxProfit([7, 6, 4, 3, 1])); // 0 console.log(maxProfitKadane([7, 1, 5, 3, 6, 4])); // 5console.log(maxProfitKadane([7, 6, 4, 3, 1])); // 0 console.log(maxProfitBruteForce([7, 1, 5, 3, 6, 4])); // 5console.log(maxProfitBruteForce([7, 6, 4, 3, 1])); // 0
Let's break down the implementation:
Implement the stock profit maximizer solution in different programming languages.
Below is the implementation of the stock profit maximizer in different programming languages. Select a language tab to view the corresponding code.
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495/** * Find the maximum profit from a single buy and sell transaction. * One-Pass approach. * * @param {number[]} prices - Array of stock prices * @return {number} - Maximum profit */function maxProfit(prices) { if (prices.length < 2) { return 0; } let minPrice = Infinity; let maxProfit = 0; for (let i = 0; i < prices.length; i++) { // Update the minimum price seen so far minPrice = Math.min(minPrice, prices[i]); // Calculate potential profit if we sell at current price const currentProfit = prices[i] - minPrice; // Update maximum profit if current profit is higher maxProfit = Math.max(maxProfit, currentProfit); } return maxProfit;} /** * Find the maximum profit from a single buy and sell transaction. * Kadane's Algorithm approach. * * @param {number[]} prices - Array of stock prices * @return {number} - Maximum profit */function maxProfitKadane(prices) { if (prices.length < 2) { return 0; } let maxCurr = 0; let maxSoFar = 0; for (let i = 1; i < prices.length; i++) { // Calculate price difference const diff = prices[i] - prices[i - 1]; // Update maximum profit ending at current position maxCurr = Math.max(0, maxCurr + diff); // Update maximum profit found so far maxSoFar = Math.max(maxSoFar, maxCurr); } return maxSoFar;} /** * Find the maximum profit from a single buy and sell transaction. * Brute Force approach. * * @param {number[]} prices - Array of stock prices * @return {number} - Maximum profit */function maxProfitBruteForce(prices) { if (prices.length < 2) { return 0; } let maxProfit = 0; // Consider all possible buy and sell days for (let i = 0; i < prices.length - 1; i++) { for (let j = i + 1; j < prices.length; j++) { // Calculate profit for this buy-sell pair const profit = prices[j] - prices[i]; // Update maximum profit if current profit is higher maxProfit = Math.max(maxProfit, profit); } } return maxProfit;} // Test casesconsole.log(maxProfit([7, 1, 5, 3, 6, 4])); // 5console.log(maxProfit([7, 6, 4, 3, 1])); // 0 console.log(maxProfitKadane([7, 1, 5, 3, 6, 4])); // 5console.log(maxProfitKadane([7, 6, 4, 3, 1])); // 0 console.log(maxProfitBruteForce([7, 1, 5, 3, 6, 4])); // 5console.log(maxProfitBruteForce([7, 6, 4, 3, 1])); // 0
First, understand that we need to find the maximum profit from a single buy and sell transaction, where we must buy before selling.
Initialize variables to track the minimum price seen so far and the maximum profit.
Iterate through the array of prices, updating the minimum price and calculating the potential profit at each step.
Update the maximum profit if the current potential profit is higher.
Handle edge cases such as arrays with fewer than 2 elements, where no profit is possible.
Modify the code to implement an alternative approach and test it with the same examples.
Implement a function that solves the stock profit maximizer problem using a different approach than shown above.
Handle the case where the input array is empty (return 0).
Handle the case where the input array has only one element (return 0).
Handle the case where prices are strictly decreasing (return 0).
Below is the implementation of the stock profit maximizer:
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495/** * Find the maximum profit from a single buy and sell transaction. * One-Pass approach. * * @param {number[]} prices - Array of stock prices * @return {number} - Maximum profit */function maxProfit(prices) { if (prices.length < 2) { return 0; } let minPrice = Infinity; let maxProfit = 0; for (let i = 0; i < prices.length; i++) { // Update the minimum price seen so far minPrice = Math.min(minPrice, prices[i]); // Calculate potential profit if we sell at current price const currentProfit = prices[i] - minPrice; // Update maximum profit if current profit is higher maxProfit = Math.max(maxProfit, currentProfit); } return maxProfit;} /** * Find the maximum profit from a single buy and sell transaction. * Kadane's Algorithm approach. * * @param {number[]} prices - Array of stock prices * @return {number} - Maximum profit */function maxProfitKadane(prices) { if (prices.length < 2) { return 0; } let maxCurr = 0; let maxSoFar = 0; for (let i = 1; i < prices.length; i++) { // Calculate price difference const diff = prices[i] - prices[i - 1]; // Update maximum profit ending at current position maxCurr = Math.max(0, maxCurr + diff); // Update maximum profit found so far maxSoFar = Math.max(maxSoFar, maxCurr); } return maxSoFar;} /** * Find the maximum profit from a single buy and sell transaction. * Brute Force approach. * * @param {number[]} prices - Array of stock prices * @return {number} - Maximum profit */function maxProfitBruteForce(prices) { if (prices.length < 2) { return 0; } let maxProfit = 0; // Consider all possible buy and sell days for (let i = 0; i < prices.length - 1; i++) { for (let j = i + 1; j < prices.length; j++) { // Calculate profit for this buy-sell pair const profit = prices[j] - prices[i]; // Update maximum profit if current profit is higher maxProfit = Math.max(maxProfit, profit); } } return maxProfit;} // Test casesconsole.log(maxProfit([7, 1, 5, 3, 6, 4])); // 5console.log(maxProfit([7, 6, 4, 3, 1])); // 0 console.log(maxProfitKadane([7, 1, 5, 3, 6, 4])); // 5console.log(maxProfitKadane([7, 6, 4, 3, 1])); // 0 console.log(maxProfitBruteForce([7, 1, 5, 3, 6, 4])); // 5console.log(maxProfitBruteForce([7, 6, 4, 3, 1])); // 0
Let's break down the implementation:
Implement the stock profit maximizer solution in different programming languages.
Below is the implementation of the stock profit maximizer in different programming languages. Select a language tab to view the corresponding code.
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495/** * Find the maximum profit from a single buy and sell transaction. * One-Pass approach. * * @param {number[]} prices - Array of stock prices * @return {number} - Maximum profit */function maxProfit(prices) { if (prices.length < 2) { return 0; } let minPrice = Infinity; let maxProfit = 0; for (let i = 0; i < prices.length; i++) { // Update the minimum price seen so far minPrice = Math.min(minPrice, prices[i]); // Calculate potential profit if we sell at current price const currentProfit = prices[i] - minPrice; // Update maximum profit if current profit is higher maxProfit = Math.max(maxProfit, currentProfit); } return maxProfit;} /** * Find the maximum profit from a single buy and sell transaction. * Kadane's Algorithm approach. * * @param {number[]} prices - Array of stock prices * @return {number} - Maximum profit */function maxProfitKadane(prices) { if (prices.length < 2) { return 0; } let maxCurr = 0; let maxSoFar = 0; for (let i = 1; i < prices.length; i++) { // Calculate price difference const diff = prices[i] - prices[i - 1]; // Update maximum profit ending at current position maxCurr = Math.max(0, maxCurr + diff); // Update maximum profit found so far maxSoFar = Math.max(maxSoFar, maxCurr); } return maxSoFar;} /** * Find the maximum profit from a single buy and sell transaction. * Brute Force approach. * * @param {number[]} prices - Array of stock prices * @return {number} - Maximum profit */function maxProfitBruteForce(prices) { if (prices.length < 2) { return 0; } let maxProfit = 0; // Consider all possible buy and sell days for (let i = 0; i < prices.length - 1; i++) { for (let j = i + 1; j < prices.length; j++) { // Calculate profit for this buy-sell pair const profit = prices[j] - prices[i]; // Update maximum profit if current profit is higher maxProfit = Math.max(maxProfit, profit); } } return maxProfit;} // Test casesconsole.log(maxProfit([7, 1, 5, 3, 6, 4])); // 5console.log(maxProfit([7, 6, 4, 3, 1])); // 0 console.log(maxProfitKadane([7, 1, 5, 3, 6, 4])); // 5console.log(maxProfitKadane([7, 6, 4, 3, 1])); // 0 console.log(maxProfitBruteForce([7, 1, 5, 3, 6, 4])); // 5console.log(maxProfitBruteForce([7, 6, 4, 3, 1])); // 0
First, understand that we need to find the maximum profit from a single buy and sell transaction, where we must buy before selling.
Initialize variables to track the minimum price seen so far and the maximum profit.
Iterate through the array of prices, updating the minimum price and calculating the potential profit at each step.
Update the maximum profit if the current potential profit is higher.
Handle edge cases such as arrays with fewer than 2 elements, where no profit is possible.
Modify the code to implement an alternative approach and test it with the same examples.
Implement a function that solves the stock profit maximizer problem using a different approach than shown above.
Handle the case where the input array is empty (return 0).
Handle the case where the input array has only one element (return 0).
Handle the case where prices are strictly decreasing (return 0).