Mark and Toys Discussions | Algorithms | HackerRank
We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
publicstaticintmaximumToys(List<Integer>prices,intk){// Sort the toy prices in ascending orderCollections.sort(prices);intmaximumToys=0;// Iterate through the sorted pricesfor(intprice:prices){if(k>=price){// Check if the current toy can be bought within the budgetmaximumToys++;k-=price;// Deduct the price of the toy from the budget}else{break;// Stop when the budget is insufficient for the next toy}}returnmaximumToys;}
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Mark and Toys
You are viewing a single comment's thread. Return to all comments →
Java: