Download code/train/JavaScript/0025284_maxProfit.js from Variable-role/sajaniemi_variable_dataset_large: direct link, hf CLI and curl.
- Browser
- Download file 603 Bytes
-
https://huggingface.co/datasets/Variable-role/sajaniemi_variable_dataset_large/resolve/main/code/train/JavaScript/0025284_maxProfit.js
- Command line
-
hf download hf://datasets/Variable-role/sajaniemi_variable_dataset_large/code/train/JavaScript/0025284_maxProfit.js
-
curl -L -o 0025284_maxProfit.js https://huggingface.co/datasets/Variable-role/sajaniemi_variable_dataset_large/resolve/main/code/train/JavaScript/0025284_maxProfit.js
603 Bytes
| // Tests | |
| const prices = [7, 1, 5, 3, 6, 4]; | |
| const prices2 = [7, 6, 4, 3, 1]; | |
| const prices3 = [1, 2]; | |
| /** | |
| * @param {number[]} prices | |
| * @return {number} | |
| */ | |
| function maxProfit(prices) { | |
| const len = prices.length; | |
| let profit = 0; | |
| let min = prices[0]; | |
| let i = 0; | |
| for (i; i < len; i++) { | |
| if (prices[i] < min) min = prices[i]; | |
| profit = Math.max(profit, prices[i] - min) | |
| } | |
| return profit; | |
| }; | |
| console.log('Input: ', prices, 'Output: ', maxProfit(prices)); | |
| console.log('Input: ', prices2, 'Output: ', maxProfit(prices2)); | |
| console.log('Input: ', prices3, 'Output: ', maxProfit(prices3)); | |