Hackearth's Data Structures solutions( Strange Game, Maximize the Earning, Pepper and Contiguous Even Subarray ) :
Problem 10: Strange Game
Solution: (in c++)
( please guys before moving to the solution try it yourself at least 3-4 times , if you really wanna become a good coder)
Here what we have to do is , increase the value of the alice's cards until each of his card value becomes greater than the maximum value of the card bob is having (so that bob has no longer a single card by which he can win a turn and alice victory is assured)
For example : alice's cards are --> 8 9 10 23 4
bob's cards are --> 7 9 2 3 13
to ensure alice wins , by the use of algorithm(to increase a number by one using k seconds) his final cards values should look like this --> 14 14 14 23 14
so our final answer will be sum of the total increments of the card values of alice * k
Problem 11: Maximize the Earning
Solution: (in c++)
( please guys before moving to the solution try it yourself at least 3-4 times , if you really wanna become a good coder)
I used a simple logic here. From front of the buildings, starting from the first building(our eyes at first building) we can only see the next building only if it is bigger than the previous building(our eyes currently stuck on). If there's a bigger building ahead, then our eyes will move and stick to that bigger building and we will compare the next buildings from it until another bigger building comes and we stick our eyes there. We will increase the value of count by one every time our eyes sticks to a new building...counting front building too as our eyes were first stuck there or you can say it is always visible as it is in the front. For those who don't know, this is also called a greedy algorithm approach. Assume our eyes is the maxi variable and now you can do this problem easily.
Comments
Post a Comment