Codility Minimum cost to achieve target sum

Hi all,

I am new to leetcode. I am an experienced professional however my past experience has been with embedded software and low level firmware programming in C. Never got the chance to work with algos and stuff. I have to admit that my graduation was not in CS or Software so I struggle in areas such as algorithms and data structures. Also throughout my career, I only switched job once so I am not used to hiring processes lately.

Recently I gave an online test for a recruiter. There were three questions out of which I could not solve one problem. Later I found that it was supposed to be solved by dynamic programming (something I never heard before). I watched some videos on dynamic programming but I am still not able to solve that problem. Kindly note I managed to find a brute force solution and it works fine but I am looking for help to solve it with DP. Here is the problem:

There are N houses (numbered from 0 to N-1) in the Green District. Until now, all of the houses have been using energy from the power plant. The energy demand of the K-th house is A[K] units. The residents of the Green District have decided that they should switch to renewable energy sources and want the total demand for energy from the power plant in the district to become non-positive. In other words, the sum of energy demands over all houses should become less than or equal to zero. For this purpose they are going to install solar panels on the roofs of some of the houses. One of two types of solar panels can be installed on each of the houses: 
• The first type costs X. Installing this type on a house generates the exact amount of energy that the house demands. After the installation, the house does not need to draw energy from the power plant anymore (its energy demand becomes 0). 
• The second type casts Y. It generates twice as much energy as the house demands. The surplus is transferred into the network and can be used by other houses inside the district. It means that after installing this type of panel on the roof of the K-th house, instead of having an A[K] energy demand, the house produces A[K] energy (which is the equivalent of having a -A[K] energy demand).

The cost of installation of each kind of solar panels is the same for all houses. 
What is the minimum cost to make the total demand for power plant energy in the district non-positive?

Write a function: 

class Solution { public int solution(int[] A, int x, int Y); } 

that, given an array A of N integers, denoting the energy demand for each of the houses, an integer X and an integer Y, returns the minimum cast to make the total demand for power plant energy in the Green District non-positive. 

Examples: 
1. Given A = [5, 3, 8, 3, 2], X = 2 and Y = 5, the function should return 7. Non-positive power plant demand can be achieved by installing two solar panels: A panel of the first type on house number 0, which will reduce the demand of A[0] from 5 to 0 for a cost of 2, and a panel of the second type on A[2], changing its demand of 8 to a production of 8 units for a cost of 5. The total cost of the installations is 2 + 5 = 7. After these two installations, the total energy demand is equal to 0 = 0 + 3-8 +3 + 2).
2. Given A = [4, 2, 7]X = 4 and Y = 100, the function should return 12. We can install a solar panel of the first type on each of the houses, therefore reducing their energy demand to 0. The total cost of the installations is 4 + 4 + 4 = 12. After these three installations, the total energy demand is equal to 0 as A = [0, 0, 0). 
3. Given A = [2,2,1,2,2), X = 2 and Y = 3, the function should return 8. Non-positive power plant energy demand can be achieved with three installations: a solar panel of the second type on A[1]. changing its demand from 2 to a production of 2 for a cost of 3, a solar panel of the second type on A[3], changing its demand from 2 to a production of 2 for a cost of 3 and a solar panel of the first type on A[4], reducing its demand from 2 to 0 for a cost of 2. The total cost of the installations is 3 + 3 + 2 = 8. After these three installations, the total energy demand is equal to -1 as A = 12,-2, 1, -2,0). 
4. Given A = [4, 1, 5, 3), X = 5 and Y = 2, the function should return 4. Non-positive power plant energy demand can be achieved by installing two solar panels of the second type: one on A[O] and one on A[3]. The total cost of the installations is 2 + 2 = 4. After these two installations, the total energy demand is equal to -1 as A = (-4, 1 5,-3).

If someone is interested I can share my brute force code but essentially its trying out all possible combinations and find the minimum cost i.e. :

  • Apply panel X to all houses, keep record of minimum panels required to achieve zero sum and their cost.
  • Apply panel Y to all houses, keep record of minimum panels required to achieve zero sum and their cost.
  • Iterate over all houses, at each iteration, apply panel X on one house and Y over all others. Keep record of minimum number of both panels X and Y required to acheive zero sum and their combined cost.
  • Iterate over all houses, at each iteration, this time apply panel Y on one house and X over all others. Keep record of minimum number of both panel X and Y required to achieve zero sum and their combined cost.
  • Finally, I return the minimum cost we found from all the above combinations.
Comments (12)