Showing posts with label HackerRank::Algorithms::Greedy. Show all posts
Showing posts with label HackerRank::Algorithms::Greedy. Show all posts

Sunday, July 26, 2015

HackerRank::Algorithms::Greedy::Flowers

 Basically the task says the more a person buys the more expensive the next purchase will be so it's simple enough to understand that each of you should start buying one by one in decreasing order starting from the most expensive flower.



HackerRank::Algorithms::Greedy::Largest Permutation

  We are aiming to build the following sequence, N, N-1, N-2, ... .If we have at least one swap, then we should use it in order to bring the number N to front regardless of the other numbers' positions. Then if we have some swaps left we should bring the Number N-1 to the second position, and then the number N-2 to the third position and so on.



HackerRank::Algorithms::Greedy::Priyanka and Toys

  Actually there is no such a thing as optimal way of choosing the toys here ( more or less). Let's sort them out by the order of their weights. Now for the one which has the lowest weight, we need to buy that in any case, then we might get some bonus items after buying them, then the same goes for the remaining set of toys, we need to buy the one with the lowest weight in any case, and we might get a bonus if we are lucky, then the same thing again until we buy all of them.



Friday, July 24, 2015

HackerRank::Algorithms::Greedy::Two Arrays

   Greedy again, which means we need to be as greedy as we can and in this case greediness would be spending as little "resources" as possible in order to save the best for the last. For every element from A we need to find a pair from B where their sum will be more or equal than K, so fro every I, we need to try to find the smallest B which will satisfy the condition, by picking this way we will find a valid solution for sure (if the solution exists) , if we can't find anything than simply the answer is NO>


HackerRank::Algorithms::Greedy::Mark and Toys

   Just because we care only about the quantity of the items we need to start buying them from the cheapest one, the we should buy the second cheapest and so on until we either buy all of them or run out of money.


HackerRank::Algorithms::Greedy::Jim and the Orders

Simply sort all the orders by the time of their completion and print the corresponding customer for each order from the earliest completion time to the latest.