Created
April 16, 2022 13:40
-
-
Save anushshukla/55601a802ae660b7ad3b6b8f6cce5d3f to your computer and use it in GitHub Desktop.
[Draft] Find maximum products which can picked up from pile of products with increasing count of products pick up
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| const removeProductsFromPile = (pileOfProducts, indexStart = 0, endIndex = pileOfProducts.length, maxProducts, reduceBy = 'max') => { | |
| pileOfProducts.map((product, currentProductIndex) => { | |
| const productsLeftInPile = product - 1; | |
| let lastProductsPickCount; | |
| for (let nextProductIndex = currentProductIndex + 1; nextProductIndex < pileOfProducts.length; nextProductIndex++) { | |
| const nextPileOfProduct = product[nextProductIndex]; | |
| if (nextPileOfProduct > product) { | |
| maxProducts = product + nextPileOfProduct; | |
| } | |
| if (nextPileOfProduct < product) { | |
| maxProducts = nextPileOfProduct + nextPileOfProduct - 1; | |
| } | |
| removeProductsFromPile(product) | |
| } | |
| }); | |
| return false; | |
| } | |
| function findMaxProducts(pileOfProducts) { | |
| let maxProducts = 0; | |
| maxProducts = removeProductsFromPile(product, 1); | |
| } | |
| function main() { | |
| console.log('Test case passed:', findMaxProducts([2, 9, 4, 7, 5, 2]) === 16) | |
| } | |
| {/* <ul> | |
| <li>Choose subarray from indices (1, 2) and pick products [2, 9] respectively from each index, which is 11 products.</li> | |
| <li>Choose subarray from indices (1, 4) and pick products [2, 3, 4, 7] , which is 16 products.</li> | |
| <li>Choose subarray from indices (1, 5) and pick products [1, 2, 3, 4, 5], which is 15 products.</li> | |
| </ul> */} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment