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.
- Prepare
- Algorithms
- Greedy
- Priyanka and Toys
- Discussions
Priyanka and Toys
Priyanka and Toys
+ 0 comments Here is problem solution - https://programs.programmingoneonone.com/2021/07/hackerrank-priyanka-and-toys-problem-solution.html
+ 0 comments Python3 Solution
def toys(weights): # Sort the list of weights in non-decreasing order weights.sort() # Initialize the container count container_count = 1 # Set the minimum weight as the threshold threshold = weights[0] for i in range(1, len(weights)): # Check if the weight of current item is greater than threshold + 4 if weights[i] > threshold + 4: # Increase the container count container_count += 1 # Update the threshold to the current item threshold = weights[i] # Return the container count return container_count
-----------------> github
+ 0 comments JavaScript Solution
function toys(w) { let container = 1; const sortedWeights = w.sort((x, y) => x - y); let currentMinimum = sortedWeights[0] + 4; sortedWeights.forEach((value) => { if (value > currentMinimum) { currentMinimum = value + 4; container++; } }); return container; }
+ 0 comments def toys(w): # Write your code here w.sort() thr=w[0] count=1 for i in range(len(w)): if w[i]>thr+4: thr=w[i] count+=1 return count
+ 0 comments cpp
int toys(vector<int> w) { sort(w.begin(),w.end()); int counter=0; for(int i=0;i<w.size();i++){ int temp=w[i]; if(i<w.size()){ while(temp+4>=w[i]){ i++; } } i--; counter++; }
return counter; }
Load more conversations
Sort 440 Discussions, By:
Please Login in order to post a comment